Skip to content

Commit

Permalink
Add internal API for .osu files
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Jun 30, 2024
1 parent 242ff98 commit e1f0d32
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
23 changes: 21 additions & 2 deletions app/adapters/osu_api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from app.common_models import GameMode

osu_api_v1_http_client = httpx.AsyncClient(
base_url="https://old.ppy.sh/api/",
base_url="https://old.ppy.sh/",
timeout=httpx.Timeout(15),
)

Expand Down Expand Up @@ -68,7 +68,7 @@ async def fetch_one_beatmap(
try:
osu_api_v1_key = random.choice(settings.OSU_API_V1_API_KEYS_POOL)
response = await osu_api_v1_http_client.get(
"get_beatmaps",
"api/get_beatmaps",
params={
"k": osu_api_v1_key,
**({"b": beatmap_id} if beatmap_id else {"h": beatmap_md5}),
Expand Down Expand Up @@ -100,3 +100,22 @@ async def fetch_one_beatmap(
},
)
raise


async def fetch_beatmap_osu_file_data(beatmap_id: int) -> bytes | None:
try:
response = await osu_api_v1_http_client.get(f"osu/{beatmap_id}")
if response.status_code == 404:
return None
if response.status_code == 403:
raise ValueError("osu api is down") from None
response.raise_for_status()
return response.read()
except Exception:
logging.exception(
"Failed to fetch beatmap osu file from osu! API v1",
extra={
"beatmap_id": beatmap_id,
},
)
raise
2 changes: 2 additions & 0 deletions app/api/internal/v1/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from fastapi import APIRouter

from . import akatsuki
from . import osu_api_v1
from . import osu_api_v2

v1_router = APIRouter()

v1_router.include_router(akatsuki.router)
v1_router.include_router(osu_api_v2.router)
v1_router.include_router(osu_api_v1.router)
23 changes: 23 additions & 0 deletions app/api/internal/v1/osu_api_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from fastapi import APIRouter
from fastapi import Response

from app.adapters import osu_api_v1

router = APIRouter(tags=["osu Files"])


@router.get("/api/osu-api/v1/osu-files/{beatmap_id}")
async def download_beatmap_osu_file(beatmap_id: int) -> Response:
beatmap_osu_file_data = await osu_api_v1.fetch_beatmap_osu_file_data(beatmap_id)
# TODO: consider at which points in beatmaps-service we should update
# the .osu file that is currently saved in wasabi s3 storage.
if beatmap_osu_file_data is not None:
return Response(
beatmap_osu_file_data,
media_type="application/octet-stream",
headers={
"Content-Disposition": f"attachment; filename={beatmap_id}.osu",
},
)

return Response(status_code=404)

0 comments on commit e1f0d32

Please sign in to comment.