Skip to content

Commit

Permalink
rate limits!
Browse files Browse the repository at this point in the history
  • Loading branch information
BlazeWasHere committed Jan 22, 2022
1 parent ec44f0c commit 187ccde
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions syn/utils/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,19 @@ class CoingeckoIDS(Enum):
@redis_cache(filter=lambda res: res != 0)
def get_historic_price(_id: CoingeckoIDS,
date: str,
currency: str = "usd") -> Decimal:
# Assume we got `date` as yyyy-mm-dd and we need as dd-mm-yyyy.
date = datetime.strptime(date, '%Y-%m-%d').strftime('%d-%m-%Y')
currency: str = "usd",
sleep: bool = False) -> Decimal:
date = dateutil.parser.parse(date).strftime('%d-%m-%Y')

# CG rate limits us @ 10-50 r/m, let's hope this makes us not trigger it.
if POPULATE_CACHE:
if POPULATE_CACHE or sleep:
time.sleep(randint(5, 20))

r = requests.get(COINGECKO_HISTORIC_URL.format(_id.value, date))

if r.status_code == 429:
# TODO(blaze): have a bailout.
time.sleep(randint(0, 5))
return get_historic_price(_id, date, currency)
return get_historic_price(_id, date, currency, sleep=True)
else:
r = r.json(use_decimal=True)

Expand Down

0 comments on commit 187ccde

Please sign in to comment.