Skip to content

Commit

Permalink
Add xkcd module (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
conormag94 authored and swapagarwal committed Dec 4, 2016
1 parent 5e6fcf7 commit e322d31
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'video',
'weather',
'wiki',
'xkcd',
]

# List of modules that send data personalized to the user
Expand Down
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])

0 comments on commit e322d31

Please sign in to comment.