Skip to content

Commit e322d31

Browse files
conormag94swapagarwal
authored andcommitted
Add xkcd module (#123)
1 parent 5e6fcf7 commit e322d31

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

modules/src/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'video',
2121
'weather',
2222
'wiki',
23+
'xkcd',
2324
]
2425

2526
# List of modules that send data personalized to the user

modules/src/xkcd.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import requests
2+
from templates.text import TextTemplate
3+
import json
4+
import config
5+
6+
def process(input, entities=None):
7+
output = {}
8+
try:
9+
r = requests.get('http://xkcd.com/info.0.json')
10+
data = r.json()
11+
number = data['num']
12+
title = data['title']
13+
link = data['img']
14+
15+
output['input'] = input
16+
output['output'] = TextTemplate('Number: ' + number + '\nTitle: ' + title + '\nLink: ' + link).get_message()
17+
output['success'] = True
18+
except:
19+
error_message = 'Error retrieving latest XKCD'
20+
ouput['error_message'] = TextTemplate(error_message).get_message()
21+
output['success'] = False
22+
return output

modules/tests/test_xkcd.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import modules
2+
3+
def test_xkcd():
4+
assert('xkcd' == modules.process_query('Show me the latest xkcd')[0])
5+
assert('xkcd' == modules.process_query('current xkcd')[0])
6+
assert('xkcd' == modules.process_query('show me an xkcd')[0])
7+
assert('xkcd' != modules.process_query('tell me a joke')[0])

0 commit comments

Comments
 (0)