Skip to content

Commit

Permalink
update fda calendar cron job
Browse files Browse the repository at this point in the history
  • Loading branch information
MuslemRahimi committed Dec 26, 2024
1 parent c0eda95 commit cc1df55
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 48 deletions.
70 changes: 23 additions & 47 deletions app/cron_fda_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,30 @@

# 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():

Expand All @@ -40,54 +43,27 @@ 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 []


async def run():
data = await get_data()
await save_json(data)
if len(data) > 0:
await save_json(data)

if __name__ == "__main__":
try:
Expand Down
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit cc1df55

Please sign in to comment.