bugfixing: consider only symbol that exist in db

This commit is contained in:
MuslemRahimi 2024-07-05 12:27:03 +02:00
parent fd6e850f89
commit 7598913cb3

View File

@ -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