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 numpy as np
import pandas as pd import pandas as pd
import ujson import ujson
import orjson
import aiohttp import aiohttp
import pytz import pytz
import redis import redis
@ -894,6 +895,7 @@ async def economic_calendar():
headers={"Content-Encoding": "gzip"} headers={"Content-Encoding": "gzip"}
) )
@app.get("/earnings-calendar") @app.get("/earnings-calendar")
async def earnings_calendar(): async def earnings_calendar():
@ -905,19 +907,15 @@ async def earnings_calendar():
media_type="application/json", media_type="application/json",
headers={"Content-Encoding": "gzip"} headers={"Content-Encoding": "gzip"}
) )
try: try:
with open(f"json/earnings-calendar/calendar.json", 'r') as file: with open(f"json/earnings-calendar/calendar.json", 'rb') as file:
res = ujson.load(file) res = orjson.loads(file.read())
except: except:
res = [] res = []
res = orjson.dumps(res)
res = ujson.dumps(res).encode('utf-8')
compressed_data = gzip.compress(res) compressed_data = gzip.compress(res)
redis_client.set(cache_key, compressed_data) redis_client.set(cache_key, compressed_data)
redis_client.expire(cache_key, 3600 * 24) # Set cache expiration time to 1 day redis_client.expire(cache_key, 3600 * 24) # Set cache expiration time to 1 day
return StreamingResponse( return StreamingResponse(
io.BytesIO(compressed_data), io.BytesIO(compressed_data),
media_type="application/json", media_type="application/json",
@ -3111,4 +3109,4 @@ async def get_clinical_trial(data:TickerData):
io.BytesIO(compressed_data), io.BytesIO(compressed_data),
media_type="application/json", media_type="application/json",
headers={"Content-Encoding": "gzip"} 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}" 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: async with session.get(url) as response:
data = await response.json() 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] #filtered_data = [entry for entry in filtered_data if entry]
print(filtered_data)
for entry in filtered_data: for entry in filtered_data:
try: try:
symbol = entry['symbol'] symbol = entry['symbol']
@ -301,7 +301,7 @@ async def get_earnings_calendar(con, symbols):
print(e) print(e)
if symbol is None or symbol not in seen_symbols: 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: if item['date'] == earnings_date:
#print(symbol, item['date'], earnings_date) #print(symbol, item['date'], earnings_date)
unique_data.append(item) unique_data.append(item)
@ -1170,6 +1170,11 @@ async def save_json_files():
crypto_symbols = [row[0] for row in crypto_cursor.fetchall()] 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) data = await get_most_shorted_stocks(con)
with open(f"json/most-shorted-stocks/data.json", 'w') as file: with open(f"json/most-shorted-stocks/data.json", 'w') as file:
ujson.dump(data, file) ujson.dump(data, file)
@ -1183,10 +1188,6 @@ async def save_json_files():
data = await get_magnificent_seven(con) data = await get_magnificent_seven(con)
with open(f"json/magnificent-seven/data.json", 'w') as file: with open(f"json/magnificent-seven/data.json", 'w') as file:
ujson.dump(data, 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) data = await get_ipo_calendar(con, symbols)
with open(f"json/ipo-calendar/data.json", 'w') as file: 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) stock_screener_data = await get_stock_screener(con,symbols)
with open(f"json/stock-screener/data.json", 'w') as file: with open(f"json/stock-screener/data.json", 'w') as file:
ujson.dump(stock_screener_data, file) ujson.dump(stock_screener_data, file)
con.close() con.close()
@ -1262,4 +1263,4 @@ try:
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(save_json_files()) loop.run_until_complete(save_json_files())
except Exception as e: except Exception as e:
print(e) print(e)