Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated gist API in help #1257

Merged
merged 1 commit into from
Mar 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 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,58 +50,48 @@ 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 = []

name_length = max(6, max(len(k) for k in bot.command_groups.keys()))
for category, cmds in collections.OrderedDict(sorted(bot.command_groups.items())).items():
category = category.upper().ljust(name_length)
cmds = set(cmds) # remove duplicates
cmds = ' '.join(cmds)
msg = category + ' ' + cmds
indent = ' ' * (name_length + 2)
# 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