forked from drsm79/err-xkcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xkcd.py
47 lines (43 loc) · 1.37 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
import logging
import re
import requests
from errbot import BotPlugin, botcmd
from bs4 import BeautifulSoup
class ShowXkcd(BotPlugin):
def get_image_and_wisdom(self, url):
response = requests.get(url)
soup = BeautifulSoup(response.text)
img = soup.first('div', {'id': 'comic'}).first('img')
return img['src'], img['title']
def callback_message(self, conn, mess):
body = mess.getBody().lower()
search = re.search("(?P<url>https?://[www.]?[^\s]+)", body)
if search:
logging.info('found xkcd link')
url = search.group("url")
logging.info('xkcd url: %s' % url)
image, wisdom = self.get_image_and_wisdom(url)
self.send(
mess.getFrom(),
image,
message_type='groupchat'
)
self.send(
mess.getFrom(),
'"%s"' % wisdom,
message_type='groupchat'
)
@botcmd
def xkcd(self, mess, args):
random_url = 'http://dynamic.xkcd.com/random/comic/'
image, wisdom = self.get_image_and_wisdom(random_url)
self.send(
mess.getFrom(),
image,
message_type='groupchat'
)
self.send(
mess.getFrom(),
'"%s"' % wisdom,
message_type='groupchat'
)