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 asyncio
import aiohttp import aiohttp
import os import os
import sqlite3
from tqdm import tqdm
from dotenv import load_dotenv from dotenv import load_dotenv
import requests import requests
@ -30,6 +32,13 @@ async def get_quote_of_stocks(ticker):
return {} return {}
async def get_data(): 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: try:
response = requests.request("GET", url, headers=headers, params=querystring) response = requests.request("GET", url, headers=headers, params=querystring)
data = ujson.loads(response.text)['fda'] data = ujson.loads(response.text)['fda']
@ -37,34 +46,35 @@ async def get_data():
extracted_data = [] extracted_data = []
# Iterate over the original data to extract required fields # Iterate over the original data to extract required fields
for entry in data: for entry in tqdm(data):
try: try:
symbol = entry['companies'][0]['securities'][0]['symbol'] symbol = entry['companies'][0]['securities'][0]['symbol']
name = entry['companies'][0]['name'] if symbol in stock_symbols:
drug_name = entry['drug']['name'].capitalize() name = entry['companies'][0]['name']
indication = entry['drug']['indication_symptom'] drug_name = entry['drug']['name'].capitalize()
outcome = entry['outcome'] indication = entry['drug']['indication_symptom']
source_type = entry['source_type'] outcome = entry['outcome']
status = entry['status'] source_type = entry['source_type']
target_date = entry['target_date'] status = entry['status']
target_date = entry['target_date']
changes_percentage = round((await get_quote_of_stocks(symbol))[0]['changesPercentage'] ,2)
changes_percentage = round((await get_quote_of_stocks(symbol))[0]['changesPercentage'] ,2)
# Create a new dictionary with the extracted information # Create a new dictionary with the extracted information
new_entry = { new_entry = {
'symbol': symbol, 'symbol': symbol,
'name': name, 'name': name,
'drugName': drug_name, 'drugName': drug_name,
'indication': indication, 'indication': indication,
'outcome': outcome, 'outcome': outcome,
'sourceType': source_type, 'sourceType': source_type,
'status': status, 'status': status,
'targetDate': target_date, 'targetDate': target_date,
'changesPercentage': changes_percentage 'changesPercentage': changes_percentage
} }
# Append the new dictionary to the new list # Append the new dictionary to the new list
extracted_data.append(new_entry) extracted_data.append(new_entry)
except: except:
pass pass