Files
g0v0-server/app/fetcher/beatmap_raw.py
MingxuanGame 0be3e903d4 feat(performance-point): switch performance calculator to performance-server (#80)
* feat(config): make `performance_server` as default calculator

* deploy(docker): use osu-performance-server

* docs(readme): add ruleset download instructions

* chore(dev): update development environment

* feat(dev): update development environment setup and service startup order

* fix(deps): move `rosu-pp-py` to `project.optional-dependencies`

* feat(beatmap): handle deleted beatmaps

* feat(performance-server): add a long timeout for calculation

* feat(recalculate): enhance CLI arguments for performance, leaderboard, and rating recalculations with CSV output support

* fix(recalculate): resolve reviews

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix(beatmapsync): resolve too long line

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-09 01:59:09 +08:00

56 lines
1.7 KiB
Python

from app.log import fetcher_logger
from ._base import BaseFetcher
from httpx import AsyncClient, HTTPError
from httpx._models import Response
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}",
]
logger = fetcher_logger("BeatmapRawFetcher")
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"get_beatmap_raw: <y>{req_url}</y>")
resp = await self._request(req_url)
if resp.status_code >= 400:
continue
if not resp.text:
continue
return resp.text
raise HTTPError("Failed to fetch beatmap")
async def _request(self, url: str) -> Response:
async with AsyncClient(timeout=15) as client:
response = await client.get(
url,
)
return response
async def get_or_fetch_beatmap_raw(self, redis: redis.Redis, beatmap_id: int) -> str:
from app.config import settings
cache_key = f"beatmap:{beatmap_id}:raw"
cache_expire = settings.beatmap_cache_expire_hours * 60 * 60
# 检查缓存
if await redis.exists(cache_key):
content = await redis.get(cache_key)
if content:
# 延长缓存时间
await redis.expire(cache_key, cache_expire)
return content
# 获取并缓存
raw = await self.get_beatmap_raw(beatmap_id)
await redis.set(cache_key, raw, ex=cache_expire)
return raw