optimize market mover files
This commit is contained in:
parent
947c030a72
commit
8f3840aadb
@ -322,30 +322,40 @@ async def run():
|
||||
|
||||
|
||||
market_status = check_market_hours()
|
||||
print(market_status)
|
||||
if market_status == 0:
|
||||
try:
|
||||
with open(f"json/market-movers/data.json", 'r') as file:
|
||||
data = ujson.load(file)
|
||||
market_movers = {'gainers': data['gainers']['1D'][:5], 'losers': data['losers']['1D'][:5]}
|
||||
with open(f"json/market-movers/markethours/gainers.json", 'r') as file:
|
||||
gainers = ujson.load(file)
|
||||
with open(f"json/market-movers/markethours/losers.json", 'r') as file:
|
||||
losers = ujson.load(file)
|
||||
market_movers = {'gainers': gainers['1D'][:5], 'losers': losers['1D'][:5]}
|
||||
except:
|
||||
market_movers = {}
|
||||
elif market_status == 1:
|
||||
try:
|
||||
with open(f"json/market-movers/premarket.json", 'r') as file:
|
||||
with open(f"json/market-movers/premarket/gainers.json", 'r') as file:
|
||||
data = ujson.load(file)
|
||||
gainers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage']} for item in data['gainers'][:5]]
|
||||
losers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage']} for item in data['losers'][:5]]
|
||||
market_movers={'gainers': gainers, 'losers': losers}
|
||||
gainers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage']} for item in data[:5]]
|
||||
|
||||
with open(f"json/market-movers/premarket/losers.json", 'r') as file:
|
||||
data = ujson.load(file)
|
||||
losers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage']} for item in data[:5]]
|
||||
|
||||
market_movers={'gainers': gainers, 'losers': losers}
|
||||
except:
|
||||
market_movers = {}
|
||||
elif market_status == 2:
|
||||
try:
|
||||
with open(f"json/market-movers/afterhours.json", 'r') as file:
|
||||
with open(f"json/market-movers/afterhours/gainers.json", 'r') as file:
|
||||
data = ujson.load(file)
|
||||
gainers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage']} for item in data['gainers'][:5]]
|
||||
losers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage']} for item in data['losers'][:5]]
|
||||
market_movers={'gainers': gainers, 'losers': losers}
|
||||
gainers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage']} for item in data[:5]]
|
||||
|
||||
with open(f"json/market-movers/afterhours/losers.json", 'r') as file:
|
||||
data = ujson.load(file)
|
||||
losers = [{ 'symbol': item['symbol'], 'name': item['name'], 'price': item['price'], 'changesPercentage': item['changesPercentage']} for item in data[:5]]
|
||||
|
||||
market_movers={'gainers': gainers, 'losers': losers}
|
||||
|
||||
except:
|
||||
market_movers = {}
|
||||
|
||||
|
||||
@ -409,8 +409,9 @@ try:
|
||||
|
||||
|
||||
data = asyncio.run(get_gainer_loser_active_stocks(symbols))
|
||||
with open(f"json/market-movers/data.json", 'w') as file:
|
||||
file.write(orjson.dumps(data).decode("utf-8"))
|
||||
for category in data.keys():
|
||||
with open(f"json/market-movers/markethours/{category}.json", 'w') as file:
|
||||
file.write(orjson.dumps(data[category]).decode("utf-8"))
|
||||
|
||||
data = asyncio.run(get_historical_data())
|
||||
with open(f"json/mini-plots-index/data.json", 'w') as file:
|
||||
@ -418,11 +419,13 @@ try:
|
||||
|
||||
data = asyncio.run(get_pre_after_market_movers(symbols))
|
||||
if market_status == 1:
|
||||
with open(f"json/market-movers/premarket.json", 'w') as file:
|
||||
file.write(orjson.dumps(data).decode("utf-8"))
|
||||
for category in data.keys():
|
||||
with open(f"json/market-movers/premarket/{category}.json", 'w') as file:
|
||||
file.write(orjson.dumps(data[category]).decode("utf-8"))
|
||||
elif market_status == 2:
|
||||
with open(f"json/market-movers/afterhours.json", 'w') as file:
|
||||
file.write(orjson.dumps(data).decode("utf-8"))
|
||||
for category in data.keys():
|
||||
with open(f"json/market-movers/afterhours/{category}.json", 'w') as file:
|
||||
file.write(orjson.dumps(data[category]).decode("utf-8"))
|
||||
|
||||
con.close()
|
||||
except Exception as e:
|
||||
|
||||
40
app/main.py
40
app/main.py
@ -207,6 +207,13 @@ async def openapi(username: str = Depends(get_current_username)):
|
||||
class TickerData(BaseModel):
|
||||
ticker: str
|
||||
|
||||
class GeneralData(BaseModel):
|
||||
params: str
|
||||
|
||||
class ParamsData(BaseModel):
|
||||
params: str
|
||||
category: str
|
||||
|
||||
class MarketNews(BaseModel):
|
||||
newsType: str
|
||||
|
||||
@ -320,8 +327,6 @@ class HistoricalDate(BaseModel):
|
||||
class OptionsWatchList(BaseModel):
|
||||
optionsIdList: list
|
||||
|
||||
class ParamsData(BaseModel):
|
||||
params: str
|
||||
|
||||
# Replace NaN values with None in the resulting JSON object
|
||||
def replace_nan_inf_with_none(obj):
|
||||
@ -609,9 +614,10 @@ async def get_similar_etfs(data: TickerData, api_key: str = Security(get_api_key
|
||||
return result
|
||||
|
||||
|
||||
@app.get("/market-movers")
|
||||
async def get_market_movers(api_key: str = Security(get_api_key)):
|
||||
cache_key = f"get-market-movers"
|
||||
@app.post("/market-movers")
|
||||
async def get_market_movers(data: GeneralData, api_key: str = Security(get_api_key)):
|
||||
params = data.params
|
||||
cache_key = f"market-movers-{params}"
|
||||
cached_result = redis_client.get(cache_key)
|
||||
if cached_result:
|
||||
return StreamingResponse(
|
||||
@ -620,7 +626,7 @@ async def get_market_movers(api_key: str = Security(get_api_key)):
|
||||
headers={"Content-Encoding": "gzip"}
|
||||
)
|
||||
try:
|
||||
with open(f"json/market-movers/data.json", 'rb') as file:
|
||||
with open(f"json/market-movers/markethours/{params}.json", 'rb') as file:
|
||||
res = orjson.loads(file.read())
|
||||
except:
|
||||
res = []
|
||||
@ -3890,7 +3896,8 @@ async def get_statistics(data: FilterStockList, api_key: str = Security(get_api_
|
||||
@app.post("/pre-after-market-movers")
|
||||
async def get_statistics(data: ParamsData, api_key: str = Security(get_api_key)):
|
||||
params = data.params
|
||||
cache_key = f"pre-after-market-movers-{params}"
|
||||
category = data.category
|
||||
cache_key = f"pre-after-market-movers-{category}-{params}"
|
||||
cached_result = redis_client.get(cache_key)
|
||||
if cached_result:
|
||||
return StreamingResponse(
|
||||
@ -3899,19 +3906,12 @@ async def get_statistics(data: ParamsData, api_key: str = Security(get_api_key))
|
||||
headers={"Content-Encoding": "gzip"}
|
||||
)
|
||||
|
||||
if params == 'premarket':
|
||||
try:
|
||||
with open(f"json/market-movers/premarket.json", 'rb') as file:
|
||||
res = orjson.loads(file.read())
|
||||
except:
|
||||
res = {'gainers': [], 'losers': []}
|
||||
elif params == 'afterhours':
|
||||
try:
|
||||
with open(f"json/market-movers/afterhours.json", 'rb') as file:
|
||||
res = orjson.loads(file.read())
|
||||
except:
|
||||
res = {'gainers': [], 'losers': []}
|
||||
|
||||
try:
|
||||
with open(f"json/market-movers/{category}/{params}.json", 'rb') as file:
|
||||
res = orjson.loads(file.read())
|
||||
except:
|
||||
res = {'gainers': [], 'losers': []}
|
||||
|
||||
data = orjson.dumps(res)
|
||||
compressed_data = gzip.compress(data)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user