fix(beatmap): always retry when status_code >= 400

This commit is contained in:
MingxuanGame
2025-08-17 18:14:34 +00:00
parent d37cb44c91
commit 1bc4687a0e
2 changed files with 5 additions and 7 deletions

View File

@@ -0,0 +1,44 @@
from __future__ import annotations
from ._base import BaseFetcher
from httpx import AsyncClient, HTTPError
from httpx._models import Response
from loguru import logger
import redis.asyncio as redis
urls = [
"https://osu.ppy.sh/osu/{beatmap_id}",
"https://osu.direct/api/osu/{beatmap_id}",
"https://catboy.best/osu/{beatmap_id}",
]
class BeatmapRawFetcher(BaseFetcher):
async def get_beatmap_raw(self, beatmap_id: int) -> str:
for url in urls:
req_url = url.format(beatmap_id=beatmap_id)
logger.opt(colors=True).debug(
f"<blue>[BeatmapRawFetcher]</blue> get_beatmap_raw: <y>{req_url}</y>"
)
resp = await self._request(req_url)
if resp.status_code >= 400:
continue
return resp.text
raise HTTPError("Failed to fetch beatmap")
async def _request(self, url: str) -> Response:
async with AsyncClient() as client:
response = await client.get(
url,
)
return response
async def get_or_fetch_beatmap_raw(
self, redis: redis.Redis, beatmap_id: int
) -> str:
if await redis.exists(f"beatmap:{beatmap_id}:raw"):
return await redis.get(f"beatmap:{beatmap_id}:raw") # pyright: ignore[reportReturnType]
raw = await self.get_beatmap_raw(beatmap_id)
await redis.set(f"beatmap:{beatmap_id}:raw", raw, ex=60 * 60 * 24)
return raw