fixing the stock function. the old provider colsed off their api. New api not as feature rich but it works

This commit is contained in:
luke 2020-03-20 18:55:58 -07:00
parent 51e101ffef
commit ef40a0f7cb

View File

@ -1,4 +1,6 @@
from datetime import date
import requests
import os
def parse_share(msg):
if len(msg.split()) > 1:
@ -12,15 +14,10 @@ def parse_share(msg):
return '```Please input at least one valid share: !stock [share_name]```'
def get_stock(share_name):
token = os.getenv('stock_api_key')
request_string="https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={}&interval=5min&apikey={}".format(share_name, token)
request = requests.get(request_string).json()
target_date = request['Meta Data']['3. Last Refreshed']
blob = request['Time Series (Daily)'][target_date]
request = requests.get("https://api.iextrading.com/1.0/stock/{}/quote".format(share_name)).json()
return '```Company Name: {}\nSymbol: {}\nSector: {}\n\nOpen: ${}\nCurrently: ${}\nChange: ${}\nChange percent {}%```'.format(
request['companyName'],
request['symbol'],
request['sector'],
request['open'],
request['latestPrice'],
request['change'],
request['changePercent']
)
return "```Symbol: {}\nOpen: {}\nClose: {}\nHigh: {}\nLow: {}\nVolume: {}```".format(share_name.upper(), blob['1. open'], blob['4. close'], blob['2. high'], blob['3. low'], blob['5. volume'])