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

Add xkcd module to display most recent xkcd comic #123

Merged
merged 1 commit into from
Dec 4, 2016
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
'video',
'weather',
'wiki',
'xkcd',
]
22 changes: 22 additions & 0 deletions modules/src/xkcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import requests
from templates.text import TextTemplate
import json
import config

def process(input, entities=None):
output = {}
try:
r = requests.get('http://xkcd.com/info.0.json')
data = r.json()
number = data['num']
title = data['title']
link = data['img']

output['input'] = input
output['output'] = TextTemplate('Number: ' + number + '\nTitle: ' + title + '\nLink: ' + link).get_message()
output['success'] = True
except:
error_message = 'Error retrieving latest XKCD'
ouput['error_message'] = TextTemplate(error_message).get_message()
output['success'] = False
return output
7 changes: 7 additions & 0 deletions modules/tests/test_xkcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import modules

def test_xkcd():
assert('xkcd' == modules.process_query('Show me the latest xkcd')[0])
assert('xkcd' == modules.process_query('current xkcd')[0])
assert('xkcd' == modules.process_query('show me an xkcd')[0])
assert('xkcd' != modules.process_query('tell me a joke')[0])