From 390e5fa2850d6b0978de98302e6e8c24e92f3b29 Mon Sep 17 00:00:00 2001 From: Conor Maguire Date: Tue, 11 Oct 2016 23:24:13 +0100 Subject: [PATCH] Add xkcd module to display most recent xkcd comic --- 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 e47d4310..520600dc 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -17,4 +17,5 @@ 'video', 'weather', 'wiki', + 'xkcd', ] 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])