change to yahoo finance and iex cloud public key

This commit is contained in:
Justin 2023-05-29 16:49:09 +08:00 committed by GitHub
parent 44f6d1ca3a
commit 3d3cdaea81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,49 +87,37 @@ def updateUpdate(NY_time):
def updateStocks(api_key, logf):
try:
max_stocks = 200
f = open('csv/stocks_settings.json', 'r')
all_stocks_settings = json.load(f)
f.close()
stock_info = all_stocks_settings['symbols']
symbols = list(stock_info.keys())
# def updateStocks(api_key, logf):
# try:
# f = open('csv/stocks_settings.json', 'r')
# all_stocks_settings = json.load(f)
# f.close()
# stock_info = all_stocks_settings['symbols']
# symbols = list(stock_info.keys())
url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/stocks?symbols='
# url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/stocks?symbols='
for symbol in symbols:
url += symbol + ','
# for symbol in symbols:
# url += symbol + ','
url += '&apiKey=' + api_key
response = requests.get(url)
data = response.json()
# stock_info = {}
if len(data) > 0:
for symbol in symbols:
for stock in data:
if stock['symbol'] == symbol:
stock_info[stock['symbol']] = {'current': stock['price'], 'change': stock['change_since'], 'percent_change':stock['percent']}
all_stocks_settings['symbols'] = stock_info
f = open('csv/stocks_settings.json', 'w+')
json.dump(all_stocks_settings, f)
f.close()
# url += '&apiKey=' + api_key
# response = requests.get(url)
# data = response.json()
except:
pass
# # stock_info = {}
# if len(data) > 0:
# for symbol in symbols:
# for stock in data:
# if stock['symbol'] == symbol:
# stock_info[stock['symbol']] = {'current': stock['price'], 'change': stock['change_since'], 'percent_change':stock['percent']}
# all_stocks_settings['symbols'] = stock_info
# f = open('csv/stocks_settings.json', 'w+')
# json.dump(all_stocks_settings, f)
# f.close()
# except:
# pass
#logf = open('log.txt', "a")
#exc_type, exc_obj, exc_tb = sys.exc_info()
#fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
@ -157,6 +145,88 @@ def getCookiesnCrumb():
with open('crumb.txt', 'w') as f:
f.write(crumb)
def updateStocks(api_key, logf):
try:
f = open('csv/stocks_settings.json', 'r')
all_stocks_settings = json.load(f)
f.close()
stock_info = all_stocks_settings['symbols']
symbols = list(stock_info.keys())
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
# url = 'https://bm7p954xoh.execute-api.us-east-2.amazonaws.com/default/ScriptsAPI/stocks?symbols='
url1 = 'https://query1.finance.yahoo.com/v7/finance/quote?fields=regularMarketPrice,regularMarketChangePercent,regularMarketChange&region=US&lang=en-US&symbols='
url2 = 'https://cloud.iexapis.com/v1/stock/market/batch?&types=quote&token=pk_aff870df1a984daa9dd43c71801c1936&symbols='
url = random.choice([url1, url2])
for symbol in symbols:
url += symbol + ','
# url += '&apiKey=' + api_key
if 'cloud.iexapis.com' in url:
response = requests.get(url, headers=headers)
data = response.json()
if len(data) > 0:
for symbol in symbols:
stock_info[data[symbol]['quote']['symbol']] = {'current': data[symbol]['quote']['latestPrice'], 'change': data[symbol]['quote']['change'], 'percent_change':data[symbol]['quote']['changePercent'] * 100}
all_stocks_settings['symbols'] = stock_info
f = open('csv/stocks_settings.json', 'w+')
json.dump(all_stocks_settings, f)
f.close()
elif 'query1.finance.yahoo.com/v7' in url:
response = requests.get(url, headers=headers)
data = response.json()
if "'error': {'code'" in str(data):
while True:
try:
with open('session.txt', 'rb') as f:
session = pickle.load(f)
with open('crumb.txt', 'r') as f:
crumb = f.read()
except:
getCookiesnCrumb()
with open('session.txt', 'rb') as f:
session = pickle.load(f)
with open('crumb.txt', 'r') as f:
crumb = f.read()
params = {'crumb': crumb}
data = session.get(url, headers=headers, params=params).json()
if "'error': {'code'" not in str(data):
break
else:
getCookiesnCrumb()
time.sleep(5)
# stock_info = {}
if len(data) > 0:
for symbol in symbols:
for stock in data['quoteResponse']['result']:
if stock['symbol'] == symbol:
stock_info[stock['symbol']] = {'current': stock['regularMarketPrice'], 'change': stock['regularMarketChange'], 'percent_change':stock['regularMarketChangePercent']}
all_stocks_settings['symbols'] = stock_info
f = open('csv/stocks_settings.json', 'w+')
json.dump(all_stocks_settings, f)
f.close()
except:
pass
#logf = open('log.txt', "a")
#exc_type, exc_obj, exc_tb = sys.exc_info()
#fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
#logf.write(str(e))
#logf.write('. file: ' + fname)
#logf.write('. line: ' + str(exc_tb.tb_lineno))
#logf.write('. type: ' + str(exc_type))
#logf.write('\n ' + "".join(traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2])))
#logf.close()
def updateStocksPrePost(api_key, logf):
try: