From cd29b06cce13cd15a1befa8f86fe96cd6e16d959 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Fri, 16 Aug 2024 13:39:38 +0200 Subject: [PATCH] add hedge fund cron job --- app/create_institute_db.py | 2 +- app/cron_hedge_funds.py | 7 +++---- app/cron_options_bubble.py | 2 +- app/main.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/app/create_institute_db.py b/app/create_institute_db.py index 0b3ab24..2e5264d 100755 --- a/app/create_institute_db.py +++ b/app/create_institute_db.py @@ -62,7 +62,7 @@ crypto_con.close() load_dotenv() api_key = os.getenv('FMP_API_KEY') -quarter_date = '2024-03-31' +quarter_date = '2024-06-30' if os.path.exists("backup_db/institute.db"): diff --git a/app/cron_hedge_funds.py b/app/cron_hedge_funds.py index d7a86fe..37cdacc 100644 --- a/app/cron_hedge_funds.py +++ b/app/cron_hedge_funds.py @@ -46,7 +46,7 @@ def best_hedge_funds(con): 'performancePercentage3year': row[6] } for row in best_performing_ciks] - with open(f"{frontend_json_url}/best-hedge-funds.json", 'w') as file: + with open(f"json/hedge-funds/best-hedge-funds.json", 'w') as file: json.dump(res_list, file) @@ -68,7 +68,7 @@ def worst_hedge_funds(con): 'performancePercentage3year': row[6] } for row in worst_performing_ciks] - with open(f"{frontend_json_url}/worst-hedge-funds.json", 'w') as file: + with open(f"json/hedge-funds/worst-hedge-funds.json", 'w') as file: json.dump(res_list, file) @@ -92,7 +92,7 @@ def all_hedge_funds(con): sorted_res_list = sorted(res_list, key=lambda x: x['marketValue'], reverse=True) - with open(f"{frontend_json_url}/all-hedge-funds.json", 'w') as file: + with open(f"json/hedge-funds/all-hedge-funds.json", 'w') as file: json.dump(sorted_res_list, file) @@ -127,7 +127,6 @@ def spy_performance(): # Get the close price for the found or closest date close_price = round(df[df['date'] == target_date]['close'].values[0],2) data.append({'date': original_date, 'price': close_price}) - print(data) diff --git a/app/cron_options_bubble.py b/app/cron_options_bubble.py index c10a7b2..5132db7 100755 --- a/app/cron_options_bubble.py +++ b/app/cron_options_bubble.py @@ -42,7 +42,7 @@ def options_bubble_data(chunk): start_date_str = start_date.strftime('%Y-%m-%d') res_list = [] - for page in range(0, 100): + for page in range(0, 500): try: data = fin.options_activity(company_tickers=company_tickers, page=page, pagesize=500, date_from=start_date_str, date_to=end_date_str) data = ujson.loads(fin.output(data))['option_activity'] diff --git a/app/main.py b/app/main.py index c2c07d7..d1c87b0 100755 --- a/app/main.py +++ b/app/main.py @@ -1400,6 +1400,7 @@ async def get_hedge_funds_data(data: GetCIKData, api_key: str = Security(get_api 'summary': orjson.loads(row[11]), } for row in cik_data] + res_json = orjson.dumps(res[0]) compressed_data = gzip.compress(res_json) @@ -1413,6 +1414,36 @@ async def get_hedge_funds_data(data: GetCIKData, api_key: str = Security(get_api ) +@app.get("/all-hedge-funds") +async def get_all_hedge_funds_data(api_key: str = Security(get_api_key)): + + cache_key = f"all-hedge-funds" + cached_result = redis_client.get(cache_key) + if cached_result: + return StreamingResponse( + io.BytesIO(cached_result), + media_type="application/json", + headers={"Content-Encoding": "gzip"} + ) + + try: + with open(f"json/hedge-funds/all-hedge-funds.json", 'rb') as file: + res = orjson.loads(file.read()) + except: + res = [] + + res = orjson.dumps(res) + compressed_data = gzip.compress(res) + + redis_client.set(cache_key, compressed_data) + redis_client.expire(cache_key, 3600 * 3600) # Set cache expiration time to Infinity + + return StreamingResponse( + io.BytesIO(compressed_data), + media_type="application/json", + headers={"Content-Encoding": "gzip"} + ) + @app.get("/searchbar-data") async def get_stock(api_key: str = Security(get_api_key)): cache_key = f"searchbar-data"