From 9e1db51001687bbf735cbf470f697e83fe5a6285 Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Wed, 18 May 2016 21:05:44 +0000 Subject: [PATCH 01/14] add news feature --- config.py | 1 + modules/src/__init__.py | 1 + modules/src/news.py | 39 ++++++++++++++++++++++++++++++++++++++ modules/tests/test_news.py | 7 +++++++ 4 files changed, 48 insertions(+) create mode 100644 modules/src/news.py create mode 100644 modules/tests/test_news.py diff --git a/config.py b/config.py index 56293741..6e75e7d8 100644 --- a/config.py +++ b/config.py @@ -4,3 +4,4 @@ GOODREADS_ACCESS_TOKEN = '' WORDS_API_KEY = '' JOKES_SOURCE_FILE = 'modules/src/jokes.json' +NYT_API_KEY = '' diff --git a/modules/src/__init__.py b/modules/src/__init__.py index 0988d343..5901f957 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/news.py b/modules/src/news.py new file mode 100644 index 00000000..e238f74c --- /dev/null +++ b/modules/src/news.py @@ -0,0 +1,39 @@ +import config +import requests +import json +from templates.button import GenericTemplate + +NYT_API_KEY = os.environ.get('NYT_API_KEY', config.NYT_API_KEY) + +def process(input, entities=None): + output = {} + try: + topic = str(entities.get('news')[0].get('topic')) + 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': 'View on Web'} ] + 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 + + + + + + + + + + diff --git a/modules/tests/test_news.py b/modules/tests/test_news.py new file mode 100644 index 00000000..a975c230 --- /dev/null +++ b/modules/tests/test_news.py @@ -0,0 +1,7 @@ +import modules + +def test_news(): + assert('news' == modules.process_query("United States Election")[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 From 662b3fd8861b8cf6945b721b94e2786a24cd4f09 Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Wed, 18 May 2016 21:32:55 +0000 Subject: [PATCH 02/14] fixed bug in test_news --- modules/tests/test_news.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/tests/test_news.py b/modules/tests/test_news.py index a975c230..039ddb95 100644 --- a/modules/tests/test_news.py +++ b/modules/tests/test_news.py @@ -4,4 +4,4 @@ def test_news(): assert('news' == modules.process_query("United States Election")[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 + assert('news' != modules.process_query("something random")[0]) \ No newline at end of file From 2bead5d0fdf9182cc8eaebf43adccffb0ae4851d Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Wed, 18 May 2016 21:42:29 +0000 Subject: [PATCH 03/14] damn found it, final fix --- modules/src/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/src/__init__.py b/modules/src/__init__.py index 5901f957..419f9bf7 100644 --- a/modules/src/__init__.py +++ b/modules/src/__init__.py @@ -8,7 +8,7 @@ 'help', 'joke', 'movie', - 'news' + 'news', 'quote', 'request', 'wiki', From e5bd6538167fcd196b673fc4c6723084e233f6f5 Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Wed, 18 May 2016 21:52:01 +0000 Subject: [PATCH 04/14] fuxkkkkkk --- modules/src/news.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/src/news.py b/modules/src/news.py index e238f74c..64a7cc9b 100644 --- a/modules/src/news.py +++ b/modules/src/news.py @@ -9,7 +9,7 @@ def process(input, entities=None): output = {} try: topic = str(entities.get('news')[0].get('topic')) - api_response = requests.get(url='https://api.nytimes.com/svc/search/v2/articlesearch.json', params={'api-key': NYT_API_KEY, 'q': topic ) + 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() From b7c18925a533dd89f4cc445dfe879aa4eaebfd23 Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Wed, 18 May 2016 21:55:54 +0000 Subject: [PATCH 05/14] Travis CI is wild ! --- modules/src/news.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/src/news.py b/modules/src/news.py index 64a7cc9b..a7e06d61 100644 --- a/modules/src/news.py +++ b/modules/src/news.py @@ -1,7 +1,7 @@ import config import requests import json -from templates.button import GenericTemplate +from templates.generic import GenericTemplate NYT_API_KEY = os.environ.get('NYT_API_KEY', config.NYT_API_KEY) From 6763bb08130e88d03b282f7a118ccf82a91aa95f Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Wed, 18 May 2016 21:59:07 +0000 Subject: [PATCH 06/14] smhhh --- modules/src/news.py | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/src/news.py b/modules/src/news.py index a7e06d61..1b0093c6 100644 --- a/modules/src/news.py +++ b/modules/src/news.py @@ -1,3 +1,4 @@ +import os import config import requests import json From a1cd053bc27a2904aad6e83093b475a5f020e493 Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Wed, 18 May 2016 22:05:05 +0000 Subject: [PATCH 07/14] might work --- modules/tests/test_news.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/tests/test_news.py b/modules/tests/test_news.py index 039ddb95..0721a76b 100644 --- a/modules/tests/test_news.py +++ b/modules/tests/test_news.py @@ -1,7 +1,7 @@ import modules def test_news(): - assert('news' == modules.process_query("United States Election")[0]) + 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 From 533e1dc645effe120c989e14949619417a7974ee Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Wed, 18 May 2016 22:17:32 +0000 Subject: [PATCH 08/14] final change, this should work --- modules/src/news.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/src/news.py b/modules/src/news.py index 1b0093c6..015c3d12 100644 --- a/modules/src/news.py +++ b/modules/src/news.py @@ -6,10 +6,10 @@ NYT_API_KEY = os.environ.get('NYT_API_KEY', config.NYT_API_KEY) -def process(input, entities=None): +def process(input, entities): output = {} try: - topic = str(entities.get('news')[0].get('topic')) + 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') From 7ea4bed0fb014e8d2e3a1b3cc079074c146003ab Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Wed, 18 May 2016 23:38:36 +0000 Subject: [PATCH 09/14] well,yeah --- modules/src/news.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/src/news.py b/modules/src/news.py index 015c3d12..cc12a527 100644 --- a/modules/src/news.py +++ b/modules/src/news.py @@ -18,7 +18,7 @@ def process(input, entities): 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 + image_url = news[i].get('multimedia')[0].get('url') if len(news.get('multimedia')) >= 1 else None subtitle = news[i].get('snippet') buttons = [ {'title': 'View on Web'} ] template.add_element(title=title, item_url=item_url, image_url=image_url, subtitle=subtitle, buttons=buttons) From 6db0d33671365d0bcf779c858788a3b9f50a178d Mon Sep 17 00:00:00 2001 From: Swapnil Agarwal Date: Sat, 28 May 2016 09:56:01 +0000 Subject: [PATCH 10/14] Update progress + feature requests --- README.md | 15 +++++++++++---- data/jokes.json | 25 ++++++++++++------------- modules/src/book.py | 2 +- modules/src/help.py | 4 ++-- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7fc8945f..7b8159b5 100644 --- a/README.md +++ b/README.md @@ -30,18 +30,22 @@ There are a lot of features that I've planned for JARVIS. Feel free to add to th - [x] Book Rating ([#11](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/11)) - [x] Movie Rating -- [ ] Anime Rating -- [ ] News -- [ ] Weather ([#13](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/13)) +- [x] Anime Rating ([#41](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/41)) +- [ ] News ([#48](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/48)) +- [ ] Weather ([#28](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/28)) - [x] Currency Conversion ([#18](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/18)) - [x] Random Quote ([#21](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/21)) -- [ ] Random Fact +- [x] Random Fact ([#45](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/45)) - [x] Random Joke - [x] Dictionary ([#1](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/1)) - [x] Wikipedia Summary ([#9](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/9)) - [ ] Lyrics Search - [ ] URL Shortener - [ ] Expand URL +- [ ] Ping ([#30](https://github.com/swapagarwal/JARVIS-on-Messenger/pull/30)) +- [ ] Restaurant Rating ([#31](https://github.com/swapagarwal/JARVIS-on-Messenger/issues/31)) +- [ ] Calculator ([#46](https://github.com/swapagarwal/JARVIS-on-Messenger/issues/46)) +- [ ] Time ([#52](https://github.com/swapagarwal/JARVIS-on-Messenger/issues/52)) Some advanced features: @@ -55,6 +59,7 @@ Some advanced features: ├── modules/ # home for various features ├── modules/src/ # code goes here ├── modules/tests/ # tests go here +├── data/ # home for shared data ├── templates/ # for sending structured messages ├── CONTRIBUTING.md # contributing guidelines └── jarvis.py # the main bot @@ -75,6 +80,8 @@ JARVIS is at your service [here](http://m.me/J.A.R.V.I.S.on.Messenger). Currentl `anything you want book` `random quote` `usd to eur rate` +`tell me a fact` +`death note anime` More examples can be found [here](https://github.com/swapagarwal/JARVIS-on-Messenger/tree/master/modules/tests). ### Local Development / Testing diff --git a/data/jokes.json b/data/jokes.json index b0e84722..4297bf00 100644 --- a/data/jokes.json +++ b/data/jokes.json @@ -1,16 +1,15 @@ { - "description": "Format:\n\t\t\t\n (OR) \n. The \"\t\t\t\" splits the joke as question & answer (if it's Q&A type) and \n is used for new line.", - "jokes": [ - "What happens to a frog's car when it breaks down?\t\t\tIt gets toad away.", - "Why was six scared of seven?\t\t\tBecause seven \"ate\" nine.", - "How do astronomers organize a party?\t\t\tThey planet.", - "Want to hear a Potassium joke?\t\t\tK.", - "A photon walks into a hotel. The desk clerk says, \"Welcome to our hotel. Can we help you with your luggage?\"\t\t\tThe photon says, \"No thanks,\", I'm traveling light.", - "What did the 30 degree angle say to the 90 degree angle?\t\t\t\"You think you're always right!\"", - "Wife: \"I look fat. Can you give me a compliment?\"\t\t\tHusband: \"You have perfect eyesight.\"", - "I changed my password to \"incorrect\". So whenever I forget what it is the computer will say \"Your password is incorrect\".", - "When an employment application asks who is to be notified in case of emergency, I always write, \"A very good doctor\".", - "If you think nobody cares whether you're alive, try missing a couple of payments.", - "I hate when I am about to hug someone really sexy and my face hits the mirror." + '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.', + 'How do astronomers organize a party?\nThey planet.', + 'Want to hear a Potassium joke?\nK.', + 'A photon walks into a hotel. The desk clerk says, "Welcome to our hotel. Can we help you with your luggage?"\nThe photon says, "No thanks, I\'m traveling light."', + 'What did the 30 degree angle say to the 90 degree angle?\n"You think you\'re always right!"', + 'Wife: "I look fat. Can you give me a compliment?"\nHusband: "You have perfect eyesight."', + 'I changed my password to "incorrect". So whenever I forget what it is, the computer will say "Your password is incorrect".', + 'When an employment application asks who is to be notified in case of emergency, I always write, "A very good doctor".', + 'If you think nobody cares whether you\'re alive, try missing a couple of payments.', + 'I hate when I am about to hug someone really sexy and my face hits the mirror.', ] } diff --git a/modules/src/book.py b/modules/src/book.py index 9716b549..726ddbf1 100644 --- a/modules/src/book.py +++ b/modules/src/book.py @@ -18,7 +18,7 @@ def process(input, entities): description = book_node.find('description').text average_rating = book_node.find('average_rating').text link = book_node.find('link').text - goodreads_attribution = "- Powered by Goodreads" + goodreads_attribution = '- Powered by Goodreads' template = TextTemplate() template.set_text('Title: ' + title + '\nDescription: ' + description) diff --git a/modules/src/help.py b/modules/src/help.py index ae149c81..16fa75ad 100644 --- a/modules/src/help.py +++ b/modules/src/help.py @@ -1,8 +1,8 @@ from templates.text import TextTemplate def process(input, entities=None): - help = '''Hi there! I'm Jarvis, your personal assistant.\nTell me things like the following:\n - - define comfort\n - iron man 2 movie plot\n - tell me a joke\n - wiki html\n - anything you want book\n - random quote\n - usd to eur rate\n + help = '''Hi there! I'm Jarvis, your personal assistant.\nTell me things like:\n + - define comfort\n - iron man 2 movie plot\n - tell me a joke/quote/fact\n - wiki html\n - anything you want book\n - usd to eur rate\n - death note anime\n I'm always learning, so do come back and say hi from time to time!\nHave a nice day.''' output = { 'input': input, From 08c7effa057c18c8e11fda701ac88b0a5c6f3cc0 Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Sat, 28 May 2016 18:28:12 +0000 Subject: [PATCH 11/14] anime: format rating to 2 decimal places --- modules/src/anime.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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) From 7ba45c393691178acd6757947e519ddeb81a1bc5 Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Sat, 28 May 2016 19:13:02 +0000 Subject: [PATCH 12/14] sweet --- modules/src/news.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/modules/src/news.py b/modules/src/news.py index cc12a527..b6016f50 100644 --- a/modules/src/news.py +++ b/modules/src/news.py @@ -19,22 +19,12 @@ def process(input, entities): 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': 'View on Web'} ] + subtitle = "Data provided by The New York Times" + buttons = [ {'title': 'View on Web', '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 - - - - - - - - - - + return output \ No newline at end of file From e5a6310f8347ecf53912ba8dfbb31a7afbaa803e Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Sat, 28 May 2016 19:23:59 +0000 Subject: [PATCH 13/14] done --- modules/src/news.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/src/news.py b/modules/src/news.py index b6016f50..f65aef53 100644 --- a/modules/src/news.py +++ b/modules/src/news.py @@ -19,8 +19,8 @@ def process(input, entities): 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 = "Data provided by The New York Times" - buttons = [ {'title': 'View on Web', 'url':item_url} ] + 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() From 3f5a1a653b9cee3576acda53bbbb6abe75b185e7 Mon Sep 17 00:00:00 2001 From: Raymond Akornor Date: Sat, 28 May 2016 19:23:59 +0000 Subject: [PATCH 14/14] done --- modules/src/news.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/src/news.py b/modules/src/news.py index b6016f50..f65aef53 100644 --- a/modules/src/news.py +++ b/modules/src/news.py @@ -19,8 +19,8 @@ def process(input, entities): 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 = "Data provided by The New York Times" - buttons = [ {'title': 'View on Web', 'url':item_url} ] + 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()