From 1c83d8337da5eb28c83946aabe926d75c7fea699 Mon Sep 17 00:00:00 2001 From: FacistFuhrer Date: Fri, 3 Nov 2017 20:53:59 -0400 Subject: [PATCH 1/2] I win --- app.py | 5 ++++ data.py | 1 + services/__init__.py | 1 + services/stock/__init__.py | 0 services/stock/stock.py | 54 +++++++++++++++++++++++++++++++++++++ services/stock/stock.pyc | Bin 0 -> 1057 bytes 6 files changed, 61 insertions(+) create mode 100644 services/stock/__init__.py create mode 100644 services/stock/stock.py create mode 100644 services/stock/stock.pyc diff --git a/app.py b/app.py index 78c7b8f..d20dd9f 100644 --- a/app.py +++ b/app.py @@ -18,6 +18,8 @@ def eval(cmd, input=None): return shuttle.eval(cmd['args']) elif cmd['service'] == 'W': ## Weather return weather.eval(input) + elif cmd['stock'] == 'M': + return stock.eval(input) else: return "ERROR 42: service not recognized" @@ -33,6 +35,8 @@ def special(incoming): body = laundry.special elif incoming.upper() == "WEATHER": body = weather.special + elif incoming.upper() == "STOCK": + body = stock.special elif incoming.upper() == "DEMO": ## welcome/instructions body = 'Thanks for using Harvard Now!\n' @@ -45,6 +49,7 @@ 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' + body += 'If you want to snag it, type in a stock symbol. Thanks!\n' return body ## main function diff --git a/data.py b/data.py index 2fdf85b..9c54dc1 100755 --- a/data.py +++ b/data.py @@ -142,4 +142,5 @@ {'service': 'S', 'args':{'endpoint': 'route', 'routeid': '4007610' , 'label': 'Quad Stadium Express Shuttle Route'}, 'tags':['QUAD', 'STADIUM', 'EXPRESS', 'SHUTTLE', 'ROUTE']}, {'service': 'S', 'args':{'endpoint': 'route', 'routeid': '4007650' , 'label': 'Allston Campus Express Shuttle Route'}, 'tags':['ALLSTON', 'CAMPUS', 'EXPRESS', 'SHUTTLE', 'ROUTE']}, {'service': 'W', 'args':{}, 'tags':['WEATHER']} + {'serivce': 'M', 'args':{}, 'tags':['STOCK']} ] diff --git a/services/__init__.py b/services/__init__.py index f598ef5..ae87b89 100755 --- a/services/__init__.py +++ b/services/__init__.py @@ -1,3 +1,4 @@ from laundry import laundry from shuttle import shuttle from weather import weather +from stock import stock diff --git a/services/stock/__init__.py b/services/stock/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/services/stock/stock.py b/services/stock/stock.py new file mode 100644 index 0000000..93d5488 --- /dev/null +++ b/services/stock/stock.py @@ -0,0 +1,54 @@ +import urllib2, urllib +import re +from bs4 import BeautifulSoup + +############################# +## Weather Function ## +############################# + +def getWeatherData(input): + url = 'https://finance.google.com/finance?q=' + url += input + hdr = {'User-Agent': 'Chrome'} + req = urllib2.Request(url,headers=hdr) + website = urllib2.urlopen(req) + soup = BeautifulSoup(website.read(), 'html.parser') + + try: + card = soup.find("div", {"id":"price-panel"}).find("span").find("span").string + + card = '$' + card + + return card + + #label = card + '\n' if card.h3 is not None else '' + + # overview = card.img.attrs['title'] + '\n' if card.img is not None and card.img.has_attr('title') else '' + # tempInFarenheit = 'Temp: ' + card.find_all(class_='wob_t')[0].text.encode('unicode-escape').replace(r'\xb0','') + '\n' if len(card.find_all(class_='wob_t')) > 0 else '' + # humidity = card.find_all(text=re.compile('Humidity'))[0] + '\n' if len(card.find_all(text=re.compile('Humidity'))) > 0 else '' + # wind = card.find_all(text=re.compile('Wind'))[0].parent.text if len(card.find_all(text=re.compile('Wind'))) > 0 else '' + + # body = label + # body += overview + # body += tempInFarenheit + # body += humidity + # body += wind + except Exception, e: + print str(e) + return "Could not find stock data. Are you sure you gave a proper stock symbol?" + + #return body + +############################ +## Top-Level ## +############################ + +def makeSpecial(): + s = 'To get the stock, type in the stock symbol.' + return s + +## return proper format to use for getting weather +special = makeSpecial() + +def eval(input): + return getWeatherData(input) diff --git a/services/stock/stock.pyc b/services/stock/stock.pyc new file mode 100644 index 0000000000000000000000000000000000000000..51dd43089872367b3ca273807c3475a82267a02d GIT binary patch literal 1057 zcmZ`%-)q!B5S~qP>0Rwgf|6{oRefaZJN`D>?`%5hQ879N4C?}d;VMSAQ$$K>Q z1bdYGR0K2)NM$~Kg+q_BKtM=3qzf=>l=Xz^bI?f?B7ymZ=P*)$ZV2;QC#^F_Z9b{m zI%RqdtA}-fWxv6|cco~Fm*_E&fgGstX@BLBBRZBtBwu=Tq14J3(xau1Pg(`!d|C!{ z9Iz-Ty6|Y#qh*hdAvu(AP}-+`956Ltw`&m6Dx_sdZ{MLflV}}^4()x|g>$uivb%d^ z%A`#7cwSZWT#wVLxTId3K6Cuj2aBdEv}M;1RyRAZ=DKu_e{`XfOgGlvka$Htu9F5g zpcqoknecUEQoU0rrOq8U%VaLVX2C=pLjtu;o}1Z2!LdGVwe1psuWDV!xJTmFAfo`t z&P1JcjVb3{C@#Dy>(+?^ILp4sIz^2>l>}e(%o?XV8R(mUG-uRXB)Gf8;kG)hl=CNc3xNJNF!~-yixCDpx0qpx~ zgrFmY97(3_kvp&T$duRA$etIoDj#>kWa`-1{|V(j6v>I6)H*duzRh>J*sM~flf7BN zl`;$SpO;uN7iRazK7j%R8kQaCg!1$-gIr1wl`XOF|&`P vnUW&j;)^^4wi6uR0lrQr= Date: Fri, 3 Nov 2017 20:56:40 -0400 Subject: [PATCH 2/2] I love HCS --- services/stock/stock.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/services/stock/stock.py b/services/stock/stock.py index 93d5488..c33d671 100644 --- a/services/stock/stock.py +++ b/services/stock/stock.py @@ -3,10 +3,10 @@ from bs4 import BeautifulSoup ############################# -## Weather Function ## +## Stock Function ## ############################# -def getWeatherData(input): +def getStockData(input): url = 'https://finance.google.com/finance?q=' url += input hdr = {'User-Agent': 'Chrome'} @@ -21,18 +21,6 @@ def getWeatherData(input): return card - #label = card + '\n' if card.h3 is not None else '' - - # overview = card.img.attrs['title'] + '\n' if card.img is not None and card.img.has_attr('title') else '' - # tempInFarenheit = 'Temp: ' + card.find_all(class_='wob_t')[0].text.encode('unicode-escape').replace(r'\xb0','') + '\n' if len(card.find_all(class_='wob_t')) > 0 else '' - # humidity = card.find_all(text=re.compile('Humidity'))[0] + '\n' if len(card.find_all(text=re.compile('Humidity'))) > 0 else '' - # wind = card.find_all(text=re.compile('Wind'))[0].parent.text if len(card.find_all(text=re.compile('Wind'))) > 0 else '' - - # body = label - # body += overview - # body += tempInFarenheit - # body += humidity - # body += wind except Exception, e: print str(e) return "Could not find stock data. Are you sure you gave a proper stock symbol?" @@ -51,4 +39,4 @@ def makeSpecial(): special = makeSpecial() def eval(input): - return getWeatherData(input) + return getStockData(input)