update earnings calendar && introduce orjson

This commit is contained in:
MuslemRahimi 2024-07-12 15:47:00 +02:00
parent ca9b940d9c
commit 49bb27cede
2 changed files with 16 additions and 17 deletions

View File

@ -11,6 +11,7 @@ from datetime import datetime, timedelta
import numpy as np
import pandas as pd
import ujson
import orjson
import aiohttp
import pytz
import redis
@ -894,6 +895,7 @@ async def economic_calendar():
headers={"Content-Encoding": "gzip"}
)
@app.get("/earnings-calendar")
async def earnings_calendar():
@ -905,19 +907,15 @@ async def earnings_calendar():
media_type="application/json",
headers={"Content-Encoding": "gzip"}
)
try:
with open(f"json/earnings-calendar/calendar.json", 'r') as file:
res = ujson.load(file)
with open(f"json/earnings-calendar/calendar.json", 'rb') as file:
res = orjson.loads(file.read())
except:
res = []
res = ujson.dumps(res).encode('utf-8')
res = orjson.dumps(res)
compressed_data = gzip.compress(res)
redis_client.set(cache_key, compressed_data)
redis_client.expire(cache_key, 3600 * 24) # Set cache expiration time to 1 day
return StreamingResponse(
io.BytesIO(compressed_data),
media_type="application/json",
@ -3111,4 +3109,4 @@ async def get_clinical_trial(data:TickerData):
io.BytesIO(compressed_data),
media_type="application/json",
headers={"Content-Encoding": "gzip"}
)
)

View File

@ -265,9 +265,9 @@ async def get_earnings_calendar(con, symbols):
url = f"https://financialmodelingprep.com/api/v3/earning_calendar?from={start_date}&to={end_date}&apikey={api_key}"
async with session.get(url) as response:
data = await response.json()
filtered_data = [{k: v for k, v in stock.items() if stock['symbol'] in symbols and '.' not in stock['symbol']} for stock in data]
filtered_data = [{k: v for k, v in stock.items() if stock['symbol'] in symbols and '.' not in stock['symbol'] and '-' not in stock['symbol']} for stock in data]
#filtered_data = [entry for entry in filtered_data if entry]
print(filtered_data)
for entry in filtered_data:
try:
symbol = entry['symbol']
@ -301,7 +301,7 @@ async def get_earnings_calendar(con, symbols):
print(e)
if symbol is None or symbol not in seen_symbols:
#bug in fmp endpoint. Double check that earnigns date is the same as in quote endpoint
#bug in fmp endpoint. Double check that earnings date is the same as in quote endpoint
if item['date'] == earnings_date:
#print(symbol, item['date'], earnings_date)
unique_data.append(item)
@ -1170,6 +1170,11 @@ async def save_json_files():
crypto_symbols = [row[0] for row in crypto_cursor.fetchall()]
earnings_list = await get_earnings_calendar(con,symbols)
with open(f"json/earnings-calendar/calendar.json", 'w') as file:
ujson.dump(earnings_list, file)
data = await get_most_shorted_stocks(con)
with open(f"json/most-shorted-stocks/data.json", 'w') as file:
ujson.dump(data, file)
@ -1183,10 +1188,6 @@ async def save_json_files():
data = await get_magnificent_seven(con)
with open(f"json/magnificent-seven/data.json", 'w') as file:
ujson.dump(data, file)
earnings_list = await get_earnings_calendar(con,symbols)
with open(f"json/earnings-calendar/calendar.json", 'w') as file:
ujson.dump(earnings_list, file)
data = await get_ipo_calendar(con, symbols)
with open(f"json/ipo-calendar/data.json", 'w') as file:
@ -1250,7 +1251,7 @@ async def save_json_files():
stock_screener_data = await get_stock_screener(con,symbols)
with open(f"json/stock-screener/data.json", 'w') as file:
ujson.dump(stock_screener_data, file)
con.close()
@ -1262,4 +1263,4 @@ try:
loop = asyncio.get_event_loop()
loop.run_until_complete(save_json_files())
except Exception as e:
print(e)
print(e)