From ef40a0f7cb98af4a1d845982cc997a5706439173 Mon Sep 17 00:00:00 2001 From: luke Date: Fri, 20 Mar 2020 18:55:58 -0700 Subject: [PATCH] fixing the stock function. the old provider colsed off their api. New api not as feature rich but it works --- app/stock.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/app/stock.py b/app/stock.py index f8c71c16..3409ce74 100644 --- a/app/stock.py +++ b/app/stock.py @@ -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']) \ No newline at end of file