Fixing stock as yahoo deprecated their endpoints, fixes #37
This commit is contained in:
parent
e9ad31fcc8
commit
72a0330c39
@ -7,4 +7,3 @@ pyowm
|
|||||||
requests
|
requests
|
||||||
wikipedia
|
wikipedia
|
||||||
wolframalpha
|
wolframalpha
|
||||||
yahoo-finance
|
|
||||||
|
21
app/stock.py
21
app/stock.py
@ -1,21 +1,24 @@
|
|||||||
import yahoo_finance
|
import requests
|
||||||
|
|
||||||
def parse_share(msg):
|
def parse_share(msg):
|
||||||
if len(msg.split()) > 1:
|
if len(msg.split()) > 1:
|
||||||
print(msg.split()[1:])
|
|
||||||
try:
|
try:
|
||||||
res = ''
|
res = ''
|
||||||
for s in msg.split()[1:]:
|
for s in msg.split()[1:]:
|
||||||
res = '{}\n\n{}'.format(res, get_stock(s))
|
res = '{}\n\n{}'.format(res, get_stock(s))
|
||||||
except yahoo_finance.YQLResponseMalformedError:
|
except:
|
||||||
res = '```Please input valid shares: !stock [share_name]```'
|
res = '```Please input valid shares: !stock [share_name]```'
|
||||||
return res
|
return res
|
||||||
return '```Please input at least one valid share: !stock [share_name]```'
|
return '```Please input at least one valid share: !stock [share_name]```'
|
||||||
|
|
||||||
def get_stock(share_name):
|
def get_stock(share_name):
|
||||||
share = yahoo_finance.Share(share_name)
|
|
||||||
open_price = share.get_open()
|
url = "https://api.iextrading.com/1.0/stock/{}/quote".format(share_name)
|
||||||
curr_price = share.get_price()
|
request = requests.get(url).json()
|
||||||
change = float(curr_price) - float(open_price)
|
|
||||||
return '```Stock: {}\n\nOpen: ${}\nCurrently: ${}\nChange: ${}```'.format(
|
open_price = request['open']
|
||||||
share_name.upper(), open_price, curr_price, change)
|
curr_price = request['latestPrice']
|
||||||
|
change = request['change']
|
||||||
|
changePercent = request['changePercent']
|
||||||
|
return '```Company Name: {}\nSymbol: {}\nSector: {}\n\nOpen: ${}\nCurrently: ${}\nChange: ${}\nChange percent {}%```'.format(
|
||||||
|
request['companyName'], request['symbol'], request['sector'], open_price, curr_price, change, changePercent)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user