-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxkcd.py
85 lines (61 loc) · 1.98 KB
/
xkcd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import logging
import random
import re
from Legobot.Lego import Lego
import requests
logger = logging.getLogger(__name__)
class XKCD(Lego):
def listening_for(self, message):
text = message.get('text')
if text and text.startswith('!xkcd'):
return True
return False
def handle(self, message):
text = message['text']
logger.debug(f'Handling message:\n{text}')
comic_id = self._get_comic_id(text)
response = self._get_comic(comic_id)
if response:
opts = self.build_reply_opts(message)
self.reply(message, response, opts)
def _call_xkcd_api(self, comic_id=None):
if comic_id:
url = f'https://xkcd.com/{comic_id}/info.0.json'
else:
url = 'https://xkcd.com/info.0.json'
call = requests.get(url)
if call.status_code == 200:
return call.json()
else:
logger.error(
f'Error on GET {url}: {call.status_code}: {call.text}')
return None
def _get_comic(self, comic_id):
response = None
comic = self._call_xkcd_api(comic_id)
if comic:
response = 'xkcd #{}: {} {}'.format(
comic['num'],
comic['alt'],
comic['img']
)
return response
def _get_comic_id(self, text):
comic_id = None
params = re.split(r'\s+', text)
if len(params) > 1:
comic_id = params[1]
if comic_id in ('r', 'random'):
comic_id = self._get_random_comic_id()
return comic_id
def _get_random_comic_id(self):
comic_id = 1337
latest = self._call_xkcd_api()
if latest:
latest_id = latest['num']
comic_id = random.randint(1, latest_id) # nosec
return comic_id
def get_name(self):
return 'xkcd'
def get_help(self):
return 'Fetch an xkcd. Usage: !xkcd [r|random|<int>]'