From 861e04c311151d024b7392997b64910ecb05a7c4 Mon Sep 17 00:00:00 2001 From: Jonathan Chu Date: Thu, 15 Nov 2018 15:19:10 -0500 Subject: [PATCH 1/3] Added headlines feature --- app.py | 6 +++++- data.py | 3 +++ services/__init__.py | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 07b7609..385d058 100644 --- a/app.py +++ b/app.py @@ -23,6 +23,8 @@ def eval(cmd, input=None): return wordOfTheDay.eval() elif cmd['service'] == 'MBTA': return MBTA.eval(cmd['args']) + elif cmd['service'] == 'H': + return headlines.eval(cmd['args']) else: return "ERROR 42: service not recognized" @@ -40,6 +42,8 @@ def special(incoming): body = weather.special elif incoming.upper() == "MBTA": body = MBTA.special + elif incoming.upper() == 'HEADLINES': + body = headlines.special elif incoming.upper() == "DEMO": ## welcome/instructions body = 'Thanks for using Harvard Now!\n' @@ -64,7 +68,7 @@ def response(): if request.method == "GET": incoming = request.args.get("phrase") - if incoming is None: + if incoming is None: resp = twilio.twiml.Response() resp.message(special("DEMO")) return str(resp) diff --git a/data.py b/data.py index e338a06..a08c324 100755 --- a/data.py +++ b/data.py @@ -270,4 +270,7 @@ {'service': 'MBTA', 'args': {'pg': ['orange/ONST', 'green/north']}, 'tags': ['MBTA', 'SUBWAY', 'T', 'SCHEDULE', 'LINE', 'ORANGE', 'NORTH', 'STATION', 'GREEN']}, {'service': 'MBTA', 'args': {'pg': ['blue/BGOV', 'green/gover']}, 'tags': ['MBTA', 'SUBWAY', 'T', 'SCHEDULE', 'LINE', 'BLUE', 'GOVERNMENT', 'CENTER', 'GREEN']}, {'service': 'MBTA', 'args': {'pg': ['orange/OHAY', 'green/haecl']}, 'tags': ['MBTA', 'SUBWAY', 'T', 'SCHEDULE', 'LINE', 'ORANGE', 'HAYMARKET', 'GREEN']}, + {'service': 'H', 'args': {'url': 'https://www.thecrimson.com/', 'publication': 'Crimson'}, 'tags': ['HEADLINES', 'CRIMSON', 'HARVARD']}, + {'service': 'H', 'args': {'url': 'https://news.harvard.edu/gazette/', 'publication': 'Gazette'}, 'tags': ['HEADLINES', 'GAZETTE', 'HARVARD']}, + {'service': 'H', 'args': {'url': 'https://hbr.org/', 'publication': 'HBR'}, 'tags': ['HEADLINES', 'HBR', 'HARVARD', 'BUSINESS', 'REVIEW']} ] diff --git a/services/__init__.py b/services/__init__.py index 5bbee48..60aa9cd 100644 --- a/services/__init__.py +++ b/services/__init__.py @@ -3,3 +3,4 @@ from weather import weather from wordOfTheDay import wordOfTheDay from MBTA import MBTA +from headlines import headlines From 28bb98feac5648bb9a28fd72d8930509352cba4c Mon Sep 17 00:00:00 2001 From: Jonathan Chu Date: Thu, 15 Nov 2018 15:20:18 -0500 Subject: [PATCH 2/3] Forgot to add new files --- services/headlines/__init__ .py | 0 services/headlines/headlines.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100755 services/headlines/__init__ .py create mode 100644 services/headlines/headlines.py diff --git a/services/headlines/__init__ .py b/services/headlines/__init__ .py new file mode 100755 index 0000000..e69de29 diff --git a/services/headlines/headlines.py b/services/headlines/headlines.py new file mode 100644 index 0000000..5b48f3f --- /dev/null +++ b/services/headlines/headlines.py @@ -0,0 +1,33 @@ +import urllib2, urllib +from bs4 import BeautifulSoup + +def headlines(url_string): + # String containing all headlines from main page + headlines = '' + + # Set up headline parser + website = urllib2.urlopen(url_string) + soup = BeautifulSoup(website.read(), 'html.parser') + headline_tag = soup.find('a', href=True) + + # Add first headline + headline = headline_tag.get_text() + headlines += headline + '\n' + + # Iterate through all headlines + next_h = headline.next_sibling + while next_h != None: + headlines += next_h + '\n' + nexth = headline.next_sibling + + return headlines + +def makeSpecial(): + s = 'To get the headlines from a particular publication, use the format \'headlines publication\'.' + return s + +## Default +special = makeSpecial() + +def eval(cmd): + return "Today's headlines from The " + cmd['publication'] + ':\n' + headlines(cmd['url']) From 02af1e1c0e2ce30377a60a018ad8a732ca4fd04f Mon Sep 17 00:00:00 2001 From: Jonathan Chu Date: Thu, 15 Nov 2018 15:25:19 -0500 Subject: [PATCH 3/3] Edit --- services/headlines/headlines.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/services/headlines/headlines.py b/services/headlines/headlines.py index 5b48f3f..ea682e8 100644 --- a/services/headlines/headlines.py +++ b/services/headlines/headlines.py @@ -5,20 +5,14 @@ def headlines(url_string): # String containing all headlines from main page headlines = '' - # Set up headline parser + # Parse all headlines website = urllib2.urlopen(url_string) soup = BeautifulSoup(website.read(), 'html.parser') - headline_tag = soup.find('a', href=True) + headline_tags = soup.find_all('a', href=True) - # Add first headline - headline = headline_tag.get_text() - headlines += headline + '\n' - - # Iterate through all headlines - next_h = headline.next_sibling - while next_h != None: - headlines += next_h + '\n' - nexth = headline.next_sibling + # Add all headlines + for tag in headline_tags: + headlines += tag.get_text() + '\n' return headlines