diff --git a/config.py b/config.py index 207e3db4..6b0afc66 100644 --- a/config.py +++ b/config.py @@ -3,4 +3,5 @@ WIT_AI_ACCESS_TOKEN = 'IKJJJYYVR3X672DHFVS7U7C4L2MQSS2P' GOODREADS_ACCESS_TOKEN = '' WORDS_API_KEY = '' +NYT_API_KEY = '' JOKES_SOURCE_FILE = 'data/jokes.json' diff --git a/data/jokes.json b/data/jokes.json index 957b1d3b..62f1010d 100644 --- a/data/jokes.json +++ b/data/jokes.json @@ -1,4 +1,5 @@ { + "jokes": [ "What happens to a frog's car when it breaks down?\nIt gets toad away.", "Why was six scared of seven?\nBecause seven \"ate\" nine.", diff --git a/modules/src/__init__.py b/modules/src/__init__.py index 0988d343..419f9bf7 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -8,6 +8,7 @@ 'help', 'joke', 'movie', + 'news', 'quote', 'request', 'wiki', diff --git a/modules/src/anime.py b/modules/src/anime.py index 711bad37..366694e7 100644 --- a/modules/src/anime.py +++ b/modules/src/anime.py @@ -12,7 +12,9 @@ def process(input, entities): template = TextTemplate() template.set_text('Title: ' + data[0]['title'] + '\nSynopsis: ' + data[0]['synopsis']) - template.set_post_text('\nCommunity Rating: ' + str(data[0]['community_rating']) + '\nStatus: ' + data[0]['status']) + #only want to see 2 decimal places + rating = {0:.2f}.format(data[0]['community_rating']) + template.set_post_text('\nCommunity Rating: ' + str(rating) + '\nStatus: ' + data[0]['status']) text = template.get_text() template = ButtonTemplate(text) diff --git a/modules/src/news.py b/modules/src/news.py new file mode 100644 index 00000000..f65aef53 --- /dev/null +++ b/modules/src/news.py @@ -0,0 +1,30 @@ +import os +import config +import requests +import json +from templates.generic import GenericTemplate + +NYT_API_KEY = os.environ.get('NYT_API_KEY', config.NYT_API_KEY) + +def process(input, entities): + output = {} + try: + topic = str(entities['news'][0]['value']) + api_response = requests.get(url='https://api.nytimes.com/svc/search/v2/articlesearch.json', params={'api-key': NYT_API_KEY, 'q': topic}) + news_dict = json.loads(api_response.text) + news = news_dict['response'].get('docs') + template = GenericTemplate() + if news: + for i in range(0,min(2,len(news))): + title = news[i].get('headline').get('main') + item_url = news[i].get('web_url') + image_url = news[i].get('multimedia')[0].get('url') if len(news.get('multimedia')) >= 1 else None + subtitle = news[i].get('snippet') + buttons = [ {'title': "Data provided by The New York Times", 'url':item_url} ] + template.add_element(title=title, item_url=item_url, image_url=image_url, subtitle=subtitle, buttons=buttons) + output['input'] = input + output['output'] = template.get_message() + output['success'] = True + except: + output['success'] = False + return output \ No newline at end of file diff --git a/modules/tests/test_news.py b/modules/tests/test_news.py new file mode 100644 index 00000000..0721a76b --- /dev/null +++ b/modules/tests/test_news.py @@ -0,0 +1,7 @@ +import modules + +def test_news(): + assert('news' == modules.process_query("what's the latest on the facebook case")[0]) + assert('news' == modules.process_query("What's happening around the world")[0]) + assert('news' == modules.process_query("What's the latest on fashion news")[0]) + assert('news' != modules.process_query("something random")[0]) \ No newline at end of file