Skip to content

Commit

Permalink
Add news module (#147)
Browse files Browse the repository at this point in the history
* Add new feature for latest news

* Adding main module and test module

* Bug Fixes related to search query

* Review changes
  • Loading branch information
harshitdx29 authored and swapagarwal committed Jan 3, 2017
1 parent c5fb166 commit 59beb1c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
TIME_ZONE_DB_API_KEY = '<<TIME_ZONE_DB_API_KEY>>'
WORDS_API_KEY = '<<WORDS_API_KEY>>'
YOUTUBE_DATA_API_KEY = '<<YOUTUBE_DATA_API_KEY>>'
NEWS_API_KEY = '<<NEWS_API_KEY>>'
1 change: 1 addition & 0 deletions modules/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'joke',
'movie',
'music',
'news',
'quote',
'request',
'time',
Expand Down
35 changes: 35 additions & 0 deletions modules/src/news.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions modules/tests/test_news.py
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit 59beb1c

Please sign in to comment.