bugfixing analyst insight
This commit is contained in:
parent
ad413dd223
commit
0d393d93c6
@ -29,22 +29,36 @@ async def save_json(symbol, data):
|
||||
async with aiofiles.open(f"json/analyst/insight/{symbol}.json", 'w') as file:
|
||||
await file.write(ujson.dumps(data))
|
||||
|
||||
# Fetch analyst insights for a specific ticker
|
||||
async def get_analyst_insight(session, ticker):
|
||||
#there is a bug in benzinga api where the latest data can be at the second element instead the first
|
||||
#solution check which date is the latest by comparing these two
|
||||
res_dict = {}
|
||||
try:
|
||||
querystring = {"token": benzinga_api_key, "symbols": ticker}
|
||||
async with session.get(url, params=querystring) as response:
|
||||
output = await response.json()
|
||||
if 'analyst-insights' in output and output['analyst-insights']:
|
||||
output = output['analyst-insights'][0]
|
||||
res_dict = {
|
||||
'insight': output['analyst_insights'],
|
||||
'id': output['id'],
|
||||
'date': datetime.strptime(output['date'], "%Y-%m-%d").strftime("%b %d, %Y")
|
||||
}
|
||||
except:
|
||||
pass
|
||||
if 'analyst-insights' in output and len(output['analyst-insights']) >= 2:
|
||||
insight_1, insight_2 = output['analyst-insights'][:2]
|
||||
|
||||
# Parse dates for comparison
|
||||
date_1 = datetime.strptime(insight_1['date'], "%Y-%m-%d")
|
||||
date_2 = datetime.strptime(insight_2['date'], "%Y-%m-%d")
|
||||
|
||||
# Choose the insight with the latest date
|
||||
latest_insight = insight_1 if date_1 >= date_2 else insight_2
|
||||
elif 'analyst-insights' in output and output['analyst-insights']:
|
||||
latest_insight = output['analyst-insights'][0] # Only one insight available
|
||||
else:
|
||||
return res_dict # No insights available
|
||||
|
||||
# Populate res_dict with the latest insight data
|
||||
res_dict = {
|
||||
'insight': latest_insight['analyst_insights'],
|
||||
'id': latest_insight['id'],
|
||||
'date': datetime.strptime(latest_insight['date'], "%Y-%m-%d").strftime("%b %d, %Y")
|
||||
}
|
||||
except Exception as e:
|
||||
print(f"Error fetching analyst insight: {e}")
|
||||
return res_dict
|
||||
|
||||
# Summarize insights using OpenAI
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user