update fda calendar cron job
This commit is contained in:
parent
c0eda95820
commit
cc1df55b08
@ -9,27 +9,30 @@ import requests
|
|||||||
|
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
load_dotenv()
|
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"
|
url = "https://api.benzinga.com/api/v2.1/calendar/fda"
|
||||||
querystring = {"token":benzinga_api_key}
|
querystring = {"token":benzinga_api_key}
|
||||||
headers = {"accept": "application/json"}
|
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):
|
async def save_json(data):
|
||||||
with open(f"json/fda-calendar/data.json", 'w') as file:
|
with open(f"json/fda-calendar/data.json", 'w') as file:
|
||||||
ujson.dump(data, 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():
|
async def get_data():
|
||||||
|
|
||||||
@ -40,46 +43,18 @@ async def get_data():
|
|||||||
stock_symbols = [row[0] for row in cursor.fetchall()]
|
stock_symbols = [row[0] for row in cursor.fetchall()]
|
||||||
con.close()
|
con.close()
|
||||||
try:
|
try:
|
||||||
response = requests.request("GET", url, headers=headers, params=querystring)
|
response = requests.get(url, headers=headers)
|
||||||
data = ujson.loads(response.text)['fda']
|
data = response.json()['data']
|
||||||
# New list to store the extracted information
|
res_list = []
|
||||||
extracted_data = []
|
for item in data:
|
||||||
|
|
||||||
# Iterate over the original data to extract required fields
|
|
||||||
for entry in tqdm(data):
|
|
||||||
try:
|
try:
|
||||||
symbol = entry['companies'][0]['securities'][0]['symbol']
|
symbol = item['ticker']
|
||||||
if symbol in stock_symbols:
|
if symbol in stock_symbols:
|
||||||
name = entry['companies'][0]['name']
|
res_list.append({**item})
|
||||||
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)
|
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Output the new list
|
return data
|
||||||
return extracted_data
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error fetching data: {e}")
|
print(f"Error fetching data: {e}")
|
||||||
return []
|
return []
|
||||||
@ -87,6 +62,7 @@ async def get_data():
|
|||||||
|
|
||||||
async def run():
|
async def run():
|
||||||
data = await get_data()
|
data = await get_data()
|
||||||
|
if len(data) > 0:
|
||||||
await save_json(data)
|
await save_json(data)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@ -3469,7 +3469,7 @@ async def get_market_maker(api_key: str = Security(get_api_key)):
|
|||||||
res = []
|
res = []
|
||||||
|
|
||||||
redis_client.set(cache_key, orjson.dumps(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
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user