Skip to content

Commit

Permalink
Merge pull request #1184 from rommapp/romm-1176
Browse files Browse the repository at this point in the history
[ROMM-1176] Fetch video ID and place in carousel
  • Loading branch information
gantoine authored Sep 9, 2024
2 parents 515b352 + fc85fcb commit 97496d1
Show file tree
Hide file tree
Showing 12 changed files with 579 additions and 409 deletions.
1 change: 1 addition & 0 deletions backend/endpoints/responses/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class RomSchema(BaseModel):

# Metadata fields
first_release_date: int | None
youtube_video_id: str | None
alternative_names: list[str]
genres: list[str]
franchises: list[str]
Expand Down
1 change: 1 addition & 0 deletions backend/endpoints/tests/test_rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_update_rom(rename_file_mock, get_rom_by_id_mock, client, access_token,
"dlcs": "[]",
"companies": '[{"id": 203227, "company": {"id": 70, "name": "Nintendo"}}, {"id": 203307, "company": {"id": 766, "name": "Retro Studios"}}]',
"first_release_date": 1675814400,
"youtube_video_id": "dQw4w9WgXcQ",
"remasters": "[]",
"remakes": "[]",
"expanded_games": "[]",
Expand Down
26 changes: 22 additions & 4 deletions backend/handler/metadata/igdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class IGDBMetadata(TypedDict):
total_rating: str
aggregated_rating: str
first_release_date: int | None
youtube_video_id: str | None
genres: list[str]
franchises: list[str]
alternative_names: list[str]
Expand All @@ -82,9 +83,12 @@ class IGDBRom(TypedDict):
igdb_metadata: NotRequired[IGDBMetadata]


def extract_metadata_from_igdb_rom(rom: dict) -> IGDBMetadata:
def extract_metadata_from_igdb_rom(
rom: dict, video_id: str | None = None
) -> IGDBMetadata:
return IGDBMetadata(
{
"youtube_video_id": video_id,
"total_rating": str(round(rom.get("total_rating", 0.0), 2)),
"aggregated_rating": str(round(rom.get("aggregated_rating", 0.0), 2)),
"first_release_date": rom.get("first_release_date", None),
Expand Down Expand Up @@ -185,6 +189,7 @@ def __init__(self) -> None:
self.games_fields = GAMES_FIELDS
self.search_endpoint = f"{self.BASE_URL}/search"
self.search_fields = SEARCH_FIELDS
self.video_endpoint = f"{self.BASE_URL}/game_videos"
self.pagination_limit = 200
self.twitch_auth = TwitchAuth()
self.headers = {
Expand All @@ -202,7 +207,6 @@ async def wrapper(*args):

return wrapper

# trunk-ignore(ruff/ASYNC109): timeout is used for request
async def _request(self, url: str, data: str, timeout: int = 120) -> list:
httpx_client = ctx_httpx_client.get()
try:
Expand Down Expand Up @@ -424,6 +428,13 @@ async def get_rom(self, file_name: str, platform_igdb_id: int) -> IGDBRom:
if not rom:
return fallback_rom

# Get the video ID for the game
video_ids = await self._request(
self.video_endpoint,
f'fields video_id; where game={rom["id"]};',
)
video_id = pydash.get(video_ids, "[0].video_id", None)

return IGDBRom(
igdb_id=rom["id"],
slug=rom["slug"],
Expand All @@ -438,7 +449,7 @@ async def get_rom(self, file_name: str, platform_igdb_id: int) -> IGDBRom:
)
for s in rom.get("screenshots", [])
],
igdb_metadata=extract_metadata_from_igdb_rom(rom),
igdb_metadata=extract_metadata_from_igdb_rom(rom, video_id),
)

@check_twitch_token
Expand All @@ -455,6 +466,13 @@ async def get_rom_by_id(self, igdb_id: int) -> IGDBRom:
if not rom:
return IGDBRom(igdb_id=None)

# Get the video ID for the game
video_ids = await self._request(
self.video_endpoint,
f'fields video_id; where game={rom["id"]};',
)
video_id = pydash.get(video_ids, "[0].video_id", None)

return IGDBRom(
igdb_id=rom["id"],
slug=rom["slug"],
Expand All @@ -469,7 +487,7 @@ async def get_rom_by_id(self, igdb_id: int) -> IGDBRom:
)
for s in rom.get("screenshots", [])
],
igdb_metadata=extract_metadata_from_igdb_rom(rom),
igdb_metadata=extract_metadata_from_igdb_rom(rom, video_id),
)

@check_twitch_token
Expand Down
Loading

0 comments on commit 97496d1

Please sign in to comment.