From 7598913cb3cadc0125d1bfa153a6bc60290d409b Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Fri, 5 Jul 2024 12:27:03 +0200 Subject: [PATCH] bugfixing: consider only symbol that exist in db --- app/cron_fda_calendar.py | 60 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/app/cron_fda_calendar.py b/app/cron_fda_calendar.py index 9cf29bd..ed06cf7 100644 --- a/app/cron_fda_calendar.py +++ b/app/cron_fda_calendar.py @@ -2,6 +2,8 @@ import ujson import asyncio import aiohttp import os +import sqlite3 +from tqdm import tqdm from dotenv import load_dotenv import requests @@ -30,6 +32,13 @@ async def get_quote_of_stocks(ticker): return {} async def get_data(): + + con = sqlite3.connect('stocks.db') + cursor = con.cursor() + cursor.execute("PRAGMA journal_mode = wal") + cursor.execute("SELECT DISTINCT symbol FROM stocks") + 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'] @@ -37,34 +46,35 @@ async def get_data(): extracted_data = [] # Iterate over the original data to extract required fields - for entry in data: + for entry in tqdm(data): try: symbol = entry['companies'][0]['securities'][0]['symbol'] - 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) + 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) + # 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) except: pass