From cc1df55b08af6f6236944080a5b3772c14651f68 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Thu, 26 Dec 2024 14:48:24 +0100 Subject: [PATCH] update fda calendar cron job --- app/cron_fda_calendar.py | 70 +++++++++++++--------------------------- app/main.py | 2 +- 2 files changed, 24 insertions(+), 48 deletions(-) diff --git a/app/cron_fda_calendar.py b/app/cron_fda_calendar.py index f12ad73..ea734ec 100644 --- a/app/cron_fda_calendar.py +++ b/app/cron_fda_calendar.py @@ -9,27 +9,30 @@ import requests # Load environment variables load_dotenv() -benzinga_api_key = os.getenv('benzinga_api_key') -fmp_api_key = os.getenv('FMP_API_KEY') +''' +benzinga_api_key = os.getenv('benzinga_api_key') url = "https://api.benzinga.com/api/v2.1/calendar/fda" querystring = {"token":benzinga_api_key} headers = {"accept": "application/json"} +''' +api_key = os.getenv('UNUSUAL_WHALES_API_KEY') + +url = "https://api.unusualwhales.com/api/market/fda-calendar" + +headers = { + "Accept": "application/json, text/plain", + "Authorization": api_key +} + + async def save_json(data): with open(f"json/fda-calendar/data.json", 'w') as file: ujson.dump(data, file) -async def get_quote_of_stocks(ticker): - async with aiohttp.ClientSession() as session: - url = f"https://financialmodelingprep.com/api/v3/quote/{ticker}?apikey={fmp_api_key}" - async with session.get(url) as response: - if response.status == 200: - return await response.json() - else: - return {} async def get_data(): @@ -40,46 +43,18 @@ async def get_data(): stock_symbols = [row[0] for row in cursor.fetchall()] con.close() try: - response = requests.request("GET", url, headers=headers, params=querystring) - data = ujson.loads(response.text)['fda'] - # New list to store the extracted information - extracted_data = [] - - # Iterate over the original data to extract required fields - for entry in tqdm(data): + response = requests.get(url, headers=headers) + data = response.json()['data'] + res_list = [] + for item in data: try: - symbol = entry['companies'][0]['securities'][0]['symbol'] + symbol = item['ticker'] if symbol in stock_symbols: - name = entry['companies'][0]['name'] - drug_name = entry['drug']['name'].capitalize() - indication = entry['drug']['indication_symptom'] - outcome = entry['outcome'] - source_type = entry['source_type'] - status = entry['status'] - target_date = entry['target_date'] - - changes_percentage = round((await get_quote_of_stocks(symbol))[0]['changesPercentage'] ,2) - - # Create a new dictionary with the extracted information - new_entry = { - 'symbol': symbol, - 'name': name, - 'drugName': drug_name, - 'indication': indication, - 'outcome': outcome, - 'sourceType': source_type, - 'status': status, - 'targetDate': target_date, - 'changesPercentage': changes_percentage - } - - # Append the new dictionary to the new list - extracted_data.append(new_entry) + res_list.append({**item}) except: pass - - # Output the new list - return extracted_data + + return data except Exception as e: print(f"Error fetching data: {e}") return [] @@ -87,7 +62,8 @@ async def get_data(): async def run(): data = await get_data() - await save_json(data) + if len(data) > 0: + await save_json(data) if __name__ == "__main__": try: diff --git a/app/main.py b/app/main.py index 68d68e9..868559d 100755 --- a/app/main.py +++ b/app/main.py @@ -3469,7 +3469,7 @@ async def get_market_maker(api_key: str = Security(get_api_key)): res = [] redis_client.set(cache_key, orjson.dumps(res)) - redis_client.expire(cache_key, 3600*3600) # Set cache expiration time to 1 day + redis_client.expire(cache_key, 60*15) # Set cache expiration time to 1 day return res