From e322d312525545d1993fe9bda1c5d78f1407095e Mon Sep 17 00:00:00 2001 From: Conor Maguire Date: Sun, 4 Dec 2016 14:09:21 +0000 Subject: [PATCH] Add xkcd module (#123) --- modules/src/__init__.py | 1 + modules/src/xkcd.py | 22 ++++++++++++++++++++++ modules/tests/test_xkcd.py | 7 +++++++ 3 files changed, 30 insertions(+) create mode 100644 modules/src/xkcd.py create mode 100644 modules/tests/test_xkcd.py diff --git a/modules/src/__init__.py b/modules/src/__init__.py index f31e4e03..9c19814e 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -20,6 +20,7 @@ 'video', 'weather', 'wiki', + 'xkcd', ] # List of modules that send data personalized to the user diff --git a/modules/src/xkcd.py b/modules/src/xkcd.py new file mode 100644 index 00000000..8fa43bf5 --- /dev/null +++ b/modules/src/xkcd.py @@ -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 diff --git a/modules/tests/test_xkcd.py b/modules/tests/test_xkcd.py new file mode 100644 index 00000000..8bab0d9e --- /dev/null +++ b/modules/tests/test_xkcd.py @@ -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])