Skip to content

Commit

Permalink
Merge pull request #1257 from hekel/master
Browse files Browse the repository at this point in the history
Replace deprecated gist API in help with ptpb (for now)
  • Loading branch information
dgw authored Mar 27, 2018
2 parents bf8f97e + 4ea4d81 commit 7cde9a8
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions sopel/modules/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
help.py - Sopel Help Module
Copyright 2008, Sean B. Palmer, inamidst.com
Copyright © 2013, Elad Alfassa, <elad@fedoraproject.org>
Copyright © 2018, Adam Erdman, pandorah.org
Licensed under the Eiffel Forum License 2.
http://sopel.chat
Expand All @@ -11,8 +12,6 @@

import textwrap
import collections
import json

import requests

from sopel.logger import get_logger
Expand Down Expand Up @@ -51,8 +50,8 @@ def help(bot, trigger):
# actually creating the list first. Maybe worth storing the link and a
# heuristic in config, too, so it persists across restarts. Would need a
# command to regenerate, too...
if 'command-gist' in bot.memory and bot.memory['command-gist'][0] == len(bot.command_groups):
url = bot.memory['command-gist'][1]
if 'command-list' in bot.memory and bot.memory['command-list'][0] == len(bot.command_groups):
url = bot.memory['command-list'][1]
else:
bot.say("Hang on, I'm creating a list.")
msgs = []
Expand All @@ -67,43 +66,32 @@ def help(bot, trigger):
# Honestly not sure why this is a list here
msgs.append('\n'.join(textwrap.wrap(msg, subsequent_indent=indent)))

url = create_gist(bot, '\n\n'.join(msgs))
url = create_list(bot, '\n\n'.join(msgs))
if not url:
return
bot.memory['command-gist'] = (len(bot.command_groups), url)
bot.memory['command-list'] = (len(bot.command_groups), url)
bot.say("I've posted a list of my commands at {} - You can see "
"more info about any of these commands by doing .help "
"<command> (e.g. .help time)".format(url))


def create_gist(bot, msg):
payload = {
'description': 'Command listing for {}@{}'.format(bot.nick, bot.config.core.host),
'public': 'true',
'files': {
'commands.txt': {
"content": msg,
},
},
}
def create_list(bot, msg):
msg = 'Command listing for {}@{}\n\n'.format(bot.nick, bot.config.core.host) + msg
payload = { "content": msg }
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}

try:
result = requests.post('https://api.github.com/gists',
data=json.dumps(payload))
result = requests.post('https://ptpb.pw/', json=payload, headers=headers)
except requests.RequestException:
bot.say("Sorry! Something went wrong.")
logger.exception("Error posting commands gist")
return
if not result.status_code != '201':
bot.say("Sorry! Something went wrong.")
logger.error("Error %s posting commands gist: %s",
result.status_code, result.text)
logger.exception("Error posting commands")
return
result = result.json()
if 'html_url' not in result:
if 'url' not in result:
bot.say("Sorry! Something went wrong.")
logger.error("Invalid result %s", result)
return
return result['html_url']
return result['url']


@rule('$nick' r'(?i)help(?:[?!]+)?$')
Expand Down

0 comments on commit 7cde9a8

Please sign in to comment.