Skip to content

Commit

Permalink
bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
MuslemRahimi committed Jan 22, 2025
1 parent 171708b commit 12c37c1
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions app/cron_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,32 +373,57 @@ def calculate_percentage_change(target):

async def get_latest_wiim():
url = "https://api.benzinga.com/api/v2/news"
querystring = {"token": benzinga_api_key,"dateFrom":yesterday,"dateTo":today,"sort":"created:desc", "pageSize": 1000, "channels":"WIIM"}
querystring = {
"token": benzinga_api_key,
"dateFrom": yesterday,
"dateTo": today,
"sort": "updated:desc",
"pageSize": 1000,
"channels": "WIIM"
}
max_retries = 3
retry_delay = 2 # seconds
res_list = []

async with aiohttp.ClientSession() as session:

async with session.get(url, params=querystring, headers=headers) as response:
data = ujson.loads(await response.text())

for item in data:
try:
if len(item['stocks']) == 1:
item['ticker'] = item['stocks'][0].get('name',None)
for attempt in range(max_retries):
async with aiohttp.ClientSession() as session:
async with session.get(url, params=querystring, headers=headers) as response:
if response.status != 200:
await asyncio.sleep(retry_delay)
continue

data = ujson.loads(await response.text())
for item in data:
try:
if len(item['stocks']) == 1:
item['ticker'] = item['stocks'][0].get('name', None)

with open(f"json/quote/{item['ticker']}.json","r") as file:
quote_data = ujson.load(file)
item['marketCap'] = quote_data.get('marketCap',None)

res_list.append({'date': item['created'], 'text': item['title'], 'marketCap': item['marketCap'],'ticker': item['ticker']})
except:
pass
res_list = sorted(
res_list,
key=lambda item: datetime.strptime(item['date'], '%a, %d %b %Y %H:%M:%S %z'),
reverse=True
)

with open(f"json/quote/{item['ticker']}.json", "r") as file:
quote_data = ujson.load(file)
item['marketCap'] = quote_data.get('marketCap', None)

res_list.append({
'date': item['created'],
'text': item['title'],
'marketCap': item['marketCap'],
'ticker': item['ticker']
})
except:
pass

if res_list:
break # Exit retry loop if data is fetched successfully

if res_list:
break
else:
await asyncio.sleep(retry_delay)

res_list = sorted(
res_list,
key=lambda item: datetime.strptime(item['date'], '%a, %d %b %Y %H:%M:%S %z'),
reverse=True
)
return res_list[:10]

async def run():
Expand Down

0 comments on commit 12c37c1

Please sign in to comment.