diff --git a/app.py b/app.py index 07b7609..50cacf0 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'] == 'YELP': + return yelp.eval(input) else: return "ERROR 42: service not recognized" @@ -52,6 +54,8 @@ def special(incoming): body += 'Sending part of a name gives all information associated with that name.\n' body += 'For example sending Quad will give information about the shuttle stop Quad and the shuttle' body += 'route Quad Yard Express and sending Quincy laundry will give all the laundry rooms in Quincy.\n' + elif incoming.upper() == 'YELP': + body = yelp.special return body ## main function diff --git a/data.py b/data.py index e338a06..0a860a8 100755 --- a/data.py +++ b/data.py @@ -270,4 +270,5 @@ {'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': 'YELP', 'args':{}, 'tags':['WEATHER']}, ] diff --git a/services/__init__.py b/services/__init__.py index 5bbee48..ac96c8a 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 yelp import yelp diff --git a/services/yelp/__init__.py b/services/yelp/__init__.py new file mode 100644 index 0000000..065d020 --- /dev/null +++ b/services/yelp/__init__.py @@ -0,0 +1 @@ +special \ No newline at end of file diff --git a/services/yelp/f.html b/services/yelp/f.html new file mode 100644 index 0000000..34c897f --- /dev/null +++ b/services/yelp/f.html @@ -0,0 +1 @@ +https://www.yelp.com/search?find_desc=clover&find_loc=harvard%20yard diff --git a/services/yelp/yelp.py b/services/yelp/yelp.py new file mode 100644 index 0000000..ef613cb --- /dev/null +++ b/services/yelp/yelp.py @@ -0,0 +1,64 @@ +import urllib2, urllib +import re +from bs4 import BeautifulSoup +from datetime import date +import calendar + +def getYelpData(input) : + try: + #construct search url + url = 'https://www.yelp.com/search?find_desc=' + urllib.quote_plus(input.strip()) + '&find_loc=harvard%20yard' + hdr = {'User-Agent': 'Chrome'} + req = urllib2.Request(url,headers=hdr) + website = urllib2.urlopen(req) + + #construct business url based on search results + tempurl = "https://www.yelp.com" + re.search("href=\"(/biz.+?)\"", website.read()).group(1) + hdr = {'User-Agent': 'Chrome'} + req = urllib2.Request(tempurl,headers=hdr) + website = urllib2.urlopen(req) + + soup = BeautifulSoup(website.read(), 'html.parser') + + #find business hours + bizhourstag = soup.find(class_ = "biz-hours") + bizhours = "" + try: + bizhours = (calendar.day_name[date.today().weekday()] + + ": " + + bizhourstag.findChildren("span", class_ = "nowrap", recursive=True)[0].string.strip() + + " - " + + bizhourstag.findChildren("span",class_ = "nowrap", recursive=True)[1].string.strip() + + "\n") + except Exception,e: + pass + + #find business address + addresstag = soup.find_all("address") + address = "" if (not addresstag or not addresstag[1]) else addresstag[1].string.strip() + "\n" + + #find business name + nametag = (soup.find("h1", class_ = "biz-page-title embossed-text-white shortenough") + or + soup.find("h1", class_ = "biz-page-title embossed-text-white")) + name = "" if not nametag else nametag.string.strip() + "\n" + + #find business phone + phonetag = soup.find_all(class_ = "biz-phone") + phone = "" if not phonetag else phonetag[0].string.strip() + "\n" + + #construct return value. + body = (name or "") + (address or "") + (bizhours or "") + (phone or "") + tempurl + return body + except Exception, e: + print(e) + return "Unable to find business." + + + +special = "Usage: yelp (business name). Provides phone number, yelp url, and today's hours." + +def eval(input): + return getYelpData(input) + +print getYelpData("cafe") \ No newline at end of file