Skip to content

Commit

Permalink
try again sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
deeleeramone committed May 23, 2024
1 parent 6dd2757 commit aaf65cc
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
# pylint: disable=unused-argument

import asyncio
import json
from datetime import datetime, timedelta
from typing import Any, Dict, List, Literal, Optional
from warnings import warn

from openbb_core.provider.abstract.fetcher import Fetcher
from openbb_core.provider.standard_models.calendar_earnings import (
CalendarEarningsData,
CalendarEarningsQueryParams,
)
from openbb_core.provider.utils.helpers import amake_request
from openbb_seeking_alpha.utils.helpers import date_range
from openbb_seeking_alpha.utils.helpers import HEADERS, date_range
from pydantic import Field, field_validator


Expand Down Expand Up @@ -89,6 +91,7 @@ async def aextract_data(
for date in date_range(query.start_date, query.end_date)
]
currency = "USD" if query.country == "us" else "CAD"
messages: List = []

async def get_date(date, currency):
"""Get date for one date."""
Expand All @@ -97,13 +100,22 @@ async def get_date(date, currency):
f"filter%5Bselected_date%5D={date}"
f"&filter%5Bwith_rating%5D=false&filter%5Bcurrency%5D={currency}"
)
response = await amake_request(url=url)
data = response.get("data") # type: ignore
if data:
results.extend(data)
response = await amake_request(url=url, headers=HEADERS)
# Try again if the response is blocked.
if "blockScript" in response:
response = await amake_request(url=url, headers=HEADERS)
if "blockScript" in response:
message = json.dumps(response)
messages.append(message)
warn(message)
if "data" in response:
results.extend(response.get("data"))

await asyncio.gather(*[get_date(date, currency) for date in dates])

if not results:
raise RuntimeError(f"Error with the Seeking Alpha request -> {messages}")

return results

@staticmethod
Expand Down

0 comments on commit aaf65cc

Please sign in to comment.