There is a rate limit :(

This commit is contained in:
Pascal P. 2026-08-10 07:51:53 +02:00
parent 57062255bf
commit c80a93b60f
2 changed files with 31 additions and 17 deletions

View File

@ -5,7 +5,7 @@ from pathlib import Path
# from sqlalchemy import create_engine
from .lib.utils import EveryRunRotator
from .services.wiki_masters import WikiMastersClient, UserSession
from .services.wiki_masters import WikiMastersClient, UserSession, ExceptionRateLimited
from .services.discord import DiscordClient
from dotenv import load_dotenv
from datetime import datetime, timedelta, timezone
@ -60,22 +60,28 @@ class App:
)
if packs_remaining == 10 or (packs_remaining == 9 and next_pack_in < timedelta(seconds=20)):
packs_remaining, packs_last_regen_at, cards = user.pull()
logger.info("{} pulled {}", d.username, ", ".join([card.name for card in cards]))
content = ""
for card in cards:
content += "- %s [%s](%s)\n" % (card.rarity.icon, card.name, card.url)
DiscordClient.send_message(content)
try:
packs_remaining, packs_last_regen_at, cards = user.pull()
logger.info("{} pulled {}", d.username, ", ".join([card.name for card in cards]))
content = ""
for card in cards:
content += "- %s [%s](%s)\n" % (card.rarity.icon, card.name, card.url)
DiscordClient.send_message(content)
next_pack_in = WikiMastersClient.ONE_PACK_TIME - (
datetime.now(tz=timezone.utc) - packs_last_regen_at
)
next_run = datetime.now() + (
next_pack_in
+ WikiMastersClient.ONE_PACK_TIME * (9 - packs_remaining)
- timedelta(seconds=10)
)
next_pack_in = WikiMastersClient.ONE_PACK_TIME - (
datetime.now(tz=timezone.utc) - packs_last_regen_at
)
next_run = datetime.now() + (
next_pack_in
+ WikiMastersClient.ONE_PACK_TIME * (9 - packs_remaining)
- timedelta(seconds=10)
)
except ExceptionRateLimited as err:
logger.warning("Pull is rate-limited retring at {}", err.retry_date)
next_run = err.retry_date
except Exception as err:
logger.exception("Pull failed for {}", username)
DiscordClient.send_message(f"**Could not pull: {str(err)}**")
logger.info(
"Schedule for {}",

View File

@ -127,7 +127,10 @@ class WikiMastersClient:
logger.error(
"error while opening [{}] {}", response.status_code, response.content
)
raise Exception("Could not pull :'(")
if response.status_code == 429:
raise ExceptionRateLimited("Could not pull (rate limited)", datetime.fromisoformat(response.json()["retry_after"]))
else:
raise Exception("Could not pull :'(")
pull_data = response.json()
@ -151,3 +154,8 @@ class WikiMastersClient:
logger.debug("logged user {}", username)
return UserSession(username, sb_client)
class ExceptionRateLimited(Exception):
def __init__(self, message, retry_in: datetime):
super().__init__(message)
self.retry_date = retry_in