Skip to content

Commit

Permalink
Count 404s as success
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyui committed Jun 27, 2024
1 parent 383a3d1 commit 019d6e3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 18 deletions.
12 changes: 4 additions & 8 deletions app/adapters/osu_mirrors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
from datetime import datetime
from typing import TypeGuard

from app.adapters.osu_mirrors.backends import AbstractBeatmapMirror
from app.adapters.osu_mirrors.backends.mino import MinoMirror
Expand All @@ -24,8 +23,8 @@
)


def is_valid_zip_file(content: bytes | None) -> TypeGuard[bytes]:
return content is not None and content.startswith(ZIP_FILE_HEADER)
def is_valid_zip_file(content: bytes) -> bool:
return content.startswith(ZIP_FILE_HEADER)


async def fetch_beatmap_zip_data(beatmapset_id: int) -> bytes | None:
Expand Down Expand Up @@ -61,8 +60,7 @@ async def fetch_beatmap_zip_data(beatmapset_id: int) -> bytes | None:
beatmap_zip_data: bytes | None = None
try:
beatmap_zip_data = await mirror.fetch_beatmap_zip_data(beatmapset_id)

if not is_valid_zip_file(beatmap_zip_data):
if beatmap_zip_data is not None and not is_valid_zip_file(beatmap_zip_data):
raise ValueError("Received bad osz2 data from mirror")
except Exception as exc:
ended_at = datetime.now()
Expand Down Expand Up @@ -114,9 +112,7 @@ async def fetch_beatmap_zip_data(beatmapset_id: int) -> bytes | None:
"mirror_weight": mirror.weight,
"beatmapset_id": beatmapset_id,
"ms_elapsed": ms_elapsed,
"data_size": (
len(beatmap_zip_data) if beatmap_zip_data is not None else None
),
"data_size": len(beatmap_zip_data) if beatmap_zip_data is not None else 0,
},
)
return beatmap_zip_data
4 changes: 4 additions & 0 deletions app/adapters/osu_mirrors/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import httpx


class MirrorRequestError(Exception):
pass


class AbstractBeatmapMirror(ABC):
name: ClassVar[str]
base_url: ClassVar[str]
Expand Down
7 changes: 5 additions & 2 deletions app/adapters/osu_mirrors/backends/gatari.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from app.adapters.osu_mirrors.backends import AbstractBeatmapMirror
from app.adapters.osu_mirrors.backends import MirrorRequestError


class GatariMirror(AbstractBeatmapMirror):
Expand All @@ -14,11 +15,13 @@ async def fetch_beatmap_zip_data(self, beatmapset_id: int) -> bytes | None:
f"{self.base_url}/d/{beatmapset_id}",
follow_redirects=True,
)
if response.status_code == 404:
return None
response.raise_for_status()
return response.read()
except Exception:
except Exception as exc:
logging.warning(
"Failed to fetch beatmap from gatari.pw",
exc_info=True,
)
return None
raise MirrorRequestError() from exc
7 changes: 5 additions & 2 deletions app/adapters/osu_mirrors/backends/mino.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import httpx

from app.adapters.osu_mirrors.backends import AbstractBeatmapMirror
from app.adapters.osu_mirrors.backends import MirrorRequestError


class MinoMirror(AbstractBeatmapMirror):
Expand All @@ -16,11 +17,13 @@ async def fetch_beatmap_zip_data(self, beatmapset_id: int) -> bytes | None:
f"{self.base_url}/d/{beatmapset_id}",
timeout=httpx.Timeout(None, connect=2),
)
if response.status_code == 404:
return None
response.raise_for_status()
return response.read()
except Exception:
except Exception as exc:
logging.warning(
"Failed to fetch beatmap from catboy.best",
exc_info=True,
)
return None
raise MirrorRequestError() from exc
7 changes: 5 additions & 2 deletions app/adapters/osu_mirrors/backends/nerinyan.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from app.adapters.osu_mirrors.backends import AbstractBeatmapMirror
from app.adapters.osu_mirrors.backends import MirrorRequestError


class NerinyanMirror(AbstractBeatmapMirror):
Expand All @@ -13,11 +14,13 @@ async def fetch_beatmap_zip_data(self, beatmapset_id: int) -> bytes | None:
response = await self.http_client.get(
f"{self.base_url}/d/{beatmapset_id}",
)
if response.status_code == 404:
return None
response.raise_for_status()
return response.read()
except Exception:
except Exception as exc:
logging.warning(
"Failed to fetch beatmap from nerinyan.moe",
exc_info=True,
)
return None
raise MirrorRequestError() from exc
7 changes: 5 additions & 2 deletions app/adapters/osu_mirrors/backends/osu_direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import httpx

from app.adapters.osu_mirrors.backends import AbstractBeatmapMirror
from app.adapters.osu_mirrors.backends import MirrorRequestError


class OsuDirectMirror(AbstractBeatmapMirror):
Expand All @@ -16,11 +17,13 @@ async def fetch_beatmap_zip_data(self, beatmapset_id: int) -> bytes | None:
f"{self.base_url}/d/{beatmapset_id}",
timeout=httpx.Timeout(None, connect=2),
)
if response.status_code == 404:
return None
response.raise_for_status()
return response.read()
except Exception:
except Exception as exc:
logging.warning(
"Failed to fetch beatmap from osu!direct",
exc_info=True,
)
return None
raise MirrorRequestError() from exc
7 changes: 5 additions & 2 deletions app/adapters/osu_mirrors/backends/ripple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from app.adapters.osu_mirrors.backends import AbstractBeatmapMirror
from app.adapters.osu_mirrors.backends import MirrorRequestError


class RippleMirror(AbstractBeatmapMirror):
Expand All @@ -13,11 +14,13 @@ async def fetch_beatmap_zip_data(self, beatmapset_id: int) -> bytes | None:
response = await self.http_client.get(
f"{self.base_url}/d/{beatmapset_id}",
)
if response.status_code == 404:
return None
response.raise_for_status()
return response.read()
except Exception:
except Exception as exc:
logging.warning(
"Failed to fetch beatmap from ripple.moe",
exc_info=True,
)
return None
raise MirrorRequestError() from exc
5 changes: 5 additions & 0 deletions app/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from enum import StrEnum


class ErrorCode(StrEnum):
FAILED_TO_FETCH_BEATMAPSET_OSZ2 = "failed_to_fetch_beatmapset_osz2"

0 comments on commit 019d6e3

Please sign in to comment.