diff --git a/config.py b/config.py index 5e5e656a..1a3d470a 100644 --- a/config.py +++ b/config.py @@ -16,3 +16,4 @@ TIME_ZONE_DB_API_KEY = '<>' WORDS_API_KEY = '<>' YOUTUBE_DATA_API_KEY = '<>' +NEWS_API_KEY = '<>' diff --git a/modules/src/__init__.py b/modules/src/__init__.py index 9c19814e..0150ada8 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -13,6 +13,7 @@ 'joke', 'movie', 'music', + 'news', 'quote', 'request', 'time', diff --git a/modules/src/news.py b/modules/src/news.py new file mode 100644 index 00000000..d9b51ee9 --- /dev/null +++ b/modules/src/news.py @@ -0,0 +1,35 @@ +import requests +import config +import os +from templates.generic import * +from templates.text import TextTemplate + +NEWS_API_KEY = os.environ.get('NEWS_API_KEY', config.NEWS_API_KEY) + +def process(input, entities=None): + output = {} + source = 'google-news' + try: + r = requests.get('https://newsapi.org/v1/articles?source=' + source + '&apiKey=' + NEWS_API_KEY) + data = r.json() + assert(len(data["articles"]) > 0) + template = GenericTemplate() + for article in data['articles']: + title = article['title'] + description = article['description'] + url = article['url'] + buttons = ButtonTemplate() + buttons.add_web_url('Powered by NewsAPI', 'https://newsapi.org/') + template.add_element(title=title, item_url=url, subtitle=description, buttons=buttons.get_buttons()) + output['input'] = input + output['output'] = template.get_message() + output['success'] = True + except: + error_message = 'I couldn\'t perform that action.' + error_message += '\nPlease ask me something else, like:' + error_message += '\n - latest news' + error_message += '\n - world news' + error_message += '\n - news' + output['error_msg'] = TextTemplate(error_message).get_message() + output['success'] = False + return output diff --git a/modules/tests/test_news.py b/modules/tests/test_news.py new file mode 100644 index 00000000..3dc43dbb --- /dev/null +++ b/modules/tests/test_news.py @@ -0,0 +1,7 @@ +import modules + +def test_news(): + assert('news' == modules.process_query('news')[0]) + assert('news' == modules.process_query('latest news')[0]) + assert('news' == modules.process_query('world news')[0]) + assert('news' != modules.process_query('something random')[0])