From 97edbbdcf9dfc74e8845cfd734650450ac6bd0d8 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Tue, 20 May 2025 16:50:14 -0400 Subject: [PATCH] complete launchbox handler --- backend/alembic/versions/0041_launchbox_id.py | 56 ++++ backend/endpoints/rom.py | 27 +- backend/handler/metadata/launchbox_handler.py | 271 +++++++++--------- backend/handler/scan_handler.py | 48 +++- backend/models/rom.py | 5 + frontend/src/__generated__/index.ts | 1 + ...ser_from_invite_api_users_register_post.ts | 11 + 7 files changed, 275 insertions(+), 144 deletions(-) create mode 100644 backend/alembic/versions/0041_launchbox_id.py create mode 100644 frontend/src/__generated__/models/Body_create_user_from_invite_api_users_register_post.ts diff --git a/backend/alembic/versions/0041_launchbox_id.py b/backend/alembic/versions/0041_launchbox_id.py new file mode 100644 index 000000000..b4efb7a4f --- /dev/null +++ b/backend/alembic/versions/0041_launchbox_id.py @@ -0,0 +1,56 @@ +"""empty message + +Revision ID: 0041_launchbox_id +Revises: 0040_migrate_assets_paths +Create Date: 2025-05-20 22:39:16.993191 + +""" + +import sqlalchemy as sa +from alembic import op +from sqlalchemy.dialects import postgresql + +revision = "0041_launchbox_id" +down_revision = "0040_migrate_assets_paths" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + with op.batch_alter_table("rom_user", schema=None) as batch_op: + batch_op.drop_column("ra_metadata") + + with op.batch_alter_table("roms", schema=None) as batch_op: + batch_op.add_column(sa.Column("launchbox_id", sa.Integer(), nullable=True)) + batch_op.add_column( + sa.Column( + "launchbox_metadata", + sa.JSON().with_variant( + postgresql.JSONB(astext_type=sa.Text()), "postgresql" + ), + nullable=True, + ) + ) + batch_op.create_index("idx_roms_launchbox_id", ["launchbox_id"], unique=False) + batch_op.create_index("idx_roms_ra_id", ["ra_id"], unique=False) + batch_op.create_index("idx_roms_sgdb_id", ["sgdb_id"], unique=False) + + +def downgrade() -> None: + + with op.batch_alter_table("roms", schema=None) as batch_op: + batch_op.drop_index("idx_roms_sgdb_id") + batch_op.drop_index("idx_roms_ra_id") + batch_op.drop_index("idx_roms_launchbox_id") + batch_op.drop_column("launchbox_metadata") + batch_op.drop_column("launchbox_id") + + with op.batch_alter_table("rom_user", schema=None) as batch_op: + batch_op.add_column( + sa.Column( + "ra_metadata", + postgresql.JSONB(astext_type=sa.Text()), + autoincrement=False, + nullable=True, + ) + ) diff --git a/backend/endpoints/rom.py b/backend/endpoints/rom.py index cd2f53d14..e5a849d3a 100644 --- a/backend/endpoints/rom.py +++ b/backend/endpoints/rom.py @@ -36,7 +36,12 @@ from handler.database import db_platform_handler, db_rom_handler from handler.database.base_handler import sync_session from handler.filesystem import fs_resource_handler, fs_rom_handler from handler.filesystem.base_handler import CoverSize -from handler.metadata import meta_igdb_handler, meta_moby_handler, meta_ss_handler +from handler.metadata import ( + meta_igdb_handler, + meta_launchbox_handler, + meta_moby_handler, + meta_ss_handler, +) from logger.formatter import BLUE from logger.formatter import highlight as hl from logger.logger import log @@ -586,6 +591,20 @@ async def update_rom( ) cleaned_data.update({"path_screenshots": path_screenshots}) + if ( + cleaned_data.get("launchbox_id", "") + and int(cleaned_data.get("launchbox_id", "")) != rom.launchbox_id + ): + igdb_rom = await meta_launchbox_handler.get_rom_by_id( + cleaned_data["launchbox_id"] + ) + cleaned_data.update(igdb_rom) + path_screenshots = await fs_resource_handler.get_rom_screenshots( + rom=rom, + url_screenshots=cleaned_data.get("url_screenshots", []), + ) + cleaned_data.update({"path_screenshots": path_screenshots}) + cleaned_data.update( { "name": data.get("name", rom.name), @@ -714,7 +733,7 @@ async def add_rom_manuals(request: Request, id: int): manuals_path = f"{RESOURCES_BASE_PATH}/{rom.fs_resources_path}/manual" file_location = Path(f"{manuals_path}/{rom.id}.pdf") - log.info(f"Uploading {hl(file_location)}") + log.info(f"Uploading {hl(str(file_location))}") if not os.path.exists(manuals_path): await Path(manuals_path).mkdir(parents=True, exist_ok=True) @@ -778,7 +797,7 @@ async def delete_roms( raise RomNotFoundInDatabaseException(id) log.info( - f"Deleting {hl(rom.name, color=BLUE)} [{hl(rom.fs_name)}] from database" + f"Deleting {hl(rom.name or 'ROM', color=BLUE)} [{hl(rom.fs_name)}] from database" ) db_rom_handler.delete_rom(id) @@ -786,7 +805,7 @@ async def delete_roms( rmtree(f"{RESOURCES_BASE_PATH}/{rom.fs_resources_path}") except FileNotFoundError: log.error( - f"Couldn't find resources to delete for {hl(rom.name, color=BLUE)}" + f"Couldn't find resources to delete for {hl(rom.name or 'ROM', color=BLUE)}" ) if id in delete_from_fs: diff --git a/backend/handler/metadata/launchbox_handler.py b/backend/handler/metadata/launchbox_handler.py index 0aabe0bf1..700d86855 100644 --- a/backend/handler/metadata/launchbox_handler.py +++ b/backend/handler/metadata/launchbox_handler.py @@ -34,6 +34,7 @@ class LaunchboxMetadata(TypedDict): class LaunchboxRom(TypedDict): + launchbox_id: int | None name: NotRequired[str] summary: NotRequired[str] url_cover: NotRequired[str] @@ -91,16 +92,17 @@ class LaunchboxHandler(MetadataHandler): from handler.filesystem import fs_rom_handler if not LAUNCHBOX_API_ENABLED: - return LaunchboxRom() + return LaunchboxRom(launchbox_id=None) search_term = fs_rom_handler.get_file_name_with_no_extension(fs_name) - fallback_rom = LaunchboxRom() + fallback_rom = LaunchboxRom(launchbox_id=None) index_entry = await self._search_rom(search_term) if not index_entry: return fallback_rom rom = { + "launchbox_id": index_entry["DatabaseID"], "name": index_entry["Name"], "summary": index_entry.get("Overview", ""), "launchbox_metadata": extract_metadata_from_launchbox_rom(index_entry), @@ -110,16 +112,17 @@ class LaunchboxHandler(MetadataHandler): async def get_rom_by_id(self, database_id: int) -> LaunchboxRom: if not LAUNCHBOX_API_ENABLED: - return LaunchboxRom() + return LaunchboxRom(launchbox_id=None) metadata_database_index_entry = await async_cache.hget( LAUNCHBOX_METADATA_DATABASE_ID_KEY, str(database_id) ) if not metadata_database_index_entry: - return LaunchboxRom() + return LaunchboxRom(launchbox_id=None) rom = { + "launchbox_id": database_id, "name": metadata_database_index_entry["Name"], "summary": metadata_database_index_entry.get("Overview", ""), "launchbox_metadata": extract_metadata_from_launchbox_rom( @@ -196,139 +199,139 @@ SLUG_TO_LAUNCHBOX_PLATFORM_NAME = { "fm-towns": "Fujitsu FM Towns Marty", "fm-7": "Fujitsu FM-7", "super-acan": "Funtech Super Acan", - "": "GCE Vectrex", - "": "Game Wave Family Entertainment System", - "": "GamePark GP32", - "": "GameWave", - "": "Hartung Game Master", - "": "Hector HRX", + "vectrex": "GCE Vectrex", + # "game-wave": "Game Wave Family Entertainment System", + "gp32": "GamePark GP32", + "game-wave": "GameWave", + "hartung": "Hartung Game Master", + "hrx": "Hector HRX", "vc-4000": "Interton VC 4000", - "": "Jupiter Ace", - "": "Linux", - "": "MS-DOS", - "": "MUGEN", - "": "Magnavox Odyssey", - "": "Magnavox Odyssey 2", - "": "Matra and Hachette Alice", - "": "Mattel Aquarius", - "": "Mattel HyperScan", - "": "Mattel Intellivision", - "": "Mega Duck", - "": "Memotech MTX512", - "": "Microsoft MSX", - "": "Microsoft MSX2", - "": "Microsoft MSX2+", - "": "Microsoft Xbox", - "": "Microsoft Xbox 360", - "": "Microsoft Xbox One", - "": "Microsoft Xbox Series X/S", - "": "NEC PC-8801", - "": "NEC PC-9801", - "": "NEC PC-FX", - "": "NEC TurboGrafx-16", - "": "NEC TurboGrafx-CD", - "": "Namco System 22", - "": "Nintendo 3DS", - "": "Nintendo 64", - "": "Nintendo 64DD", - "": "Nintendo DS", - "": "Nintendo Entertainment System", - "": "Nintendo Famicom Disk System", - "": "Nintendo Game & Watch", - "": "Nintendo Game Boy", - "": "Nintendo Game Boy Advance", - "": "Nintendo Game Boy Color", - "": "Nintendo GameCube", - "": "Nintendo Pokemon Mini", - "": "Nintendo Satellaview", - "": "Nintendo Switch", - "": "Nintendo Switch 2", - "": "Nintendo Virtual Boy", - "": "Nintendo Wii", - "": "Nintendo Wii U", - "": "Nokia N-Gage", - "": "Nuon", - "": "OpenBOR", - "": "Oric Atmos", - "": "Othello Multivision", - "": "Ouya", - "": "PC Engine SuperGrafx", - "": "PICO-8", - "": "Philips CD-i", - "": "Philips VG 5000", - "": "Philips Videopac+", - "": "Pinball", - "": "RCA Studio II", - "": "SAM Coupé", - "": "SNK Neo Geo AES", - "": "SNK Neo Geo CD", - "": "SNK Neo Geo MVS", - "": "SNK Neo Geo Pocket", - "": "SNK Neo Geo Pocket Color", - "": "Sammy Atomiswave", - "": "ScummVM", - "": "Sega 32X", - "": "Sega CD", - "": "Sega CD 32X", - "": "Sega Dreamcast", - "": "Sega Dreamcast VMU", - "": "Sega Game Gear", - "": "Sega Genesis", - "": "Sega Hikaru", - "": "Sega Master System", - "": "Sega Model 1", - "": "Sega Model 2", - "": "Sega Model 3", - "": "Sega Naomi", - "": "Sega Naomi 2", - "": "Sega Pico", - "": "Sega SC-3000", - "": "Sega SG-1000", - "": "Sega ST-V", - "": "Sega Saturn", - "": "Sega System 16", - "": "Sega System 32", - "": "Sega Triforce", - "": "Sharp MZ-2500", - "": "Sharp X1", - "": "Sharp X68000", - "": "Sinclair ZX Spectrum", - "": "Sinclair ZX-81", - "": "Sony PSP", - "": "Sony PSP Minis", - "": "Sony Playstation", - "": "Sony Playstation 2", - "": "Sony Playstation 3", - "": "Sony Playstation 4", - "": "Sony Playstation 5", - "": "Sony Playstation Vita", - "": "Sony PocketStation", - "": "Sord M5", - "": "Spectravideo", - "": "Super Nintendo Entertainment System", - "": "TRS-80 Color Computer", - "": "Taito Type X", - "": "Tandy TRS-80", - "": "Tapwave Zodiac", - "": "Texas Instruments TI 99/4A", - "": "Tiger Game.com", - "": "Tomy Tutor", - "": "VTech CreatiVision", - "": "VTech Socrates", - "": "VTech V.Smile", - "": "Vector-06C", - "": "Watara Supervision", - "": "Web Browser", - "": "Windows", - "": "Windows 3.X", - "": "WoW Action Max", - "": "WonderSwan", - "": "WonderSwan Color", - "": "XaviXPORT", - "": "ZiNc", + "jupiter-ace": "Jupiter Ace", + "linux": "Linux", + "dos": "MS-DOS", + "mugen": "MUGEN", + "odyssey--1": "Magnavox Odyssey", + "odyssey-2-slash-videopac-g7000": "Magnavox Odyssey 2", + "alice-3290": "Matra and Hachette Alice", + "mattel-aquarius": "Mattel Aquarius", + "hyperscan": "Mattel HyperScan", + "intellivision": "Mattel Intellivision", + "mega-duck-slash-cougar-boy": "Mega Duck", + "mtx512": "Memotech MTX512", + "msx": "Microsoft MSX", + "msx2": "Microsoft MSX2", + "msx2plus": "Microsoft MSX2+", + "xbox": "Microsoft Xbox", + "xbox360": "Microsoft Xbox 360", + "xboxone": "Microsoft Xbox One", + "series-x": "Microsoft Xbox Series X/S", + "pc-8800-series": "NEC PC-8801", + "pc-9800-series": "NEC PC-9801", + "pc-fx": "NEC PC-FX", + "turbografx16--1": "NEC TurboGrafx-16", + "turbografx-16-slash-pc-engine-cd": "NEC TurboGrafx-CD", + "system-32": "Namco System 22", + "3ds": "Nintendo 3DS", + "n64": "Nintendo 64", + "64dd": "Nintendo 64DD", + "nds": "Nintendo DS", + "nes": "Nintendo Entertainment System", + "famicom": "Nintendo Famicom Disk System", + "g-and-w": "Nintendo Game & Watch", + "gb": "Nintendo Game Boy", + "gba": "Nintendo Game Boy Advance", + "gbc": "Nintendo Game Boy Color", + "ngc": "Nintendo GameCube", + "pokemon-mini": "Nintendo Pokemon Mini", + "satellaview": "Nintendo Satellaview", + "switch": "Nintendo Switch", + "switch2": "Nintendo Switch 2", + "virtualboy": "Nintendo Virtual Boy", + "wii": "Nintendo Wii", + "wiiu": "Nintendo Wii U", + "ngage": "Nokia N-Gage", + "nuon": "Nuon", + "openbor": "OpenBOR", + "atmos": "Oric Atmos", + "multivision": "Othello Multivision", + "ouya": "Ouya", + "supergrafx": "PC Engine SuperGrafx", + "pico": "PICO-8", + "philips-cd-i": "Philips CD-i", + "philips-vg-5000": "Philips VG 5000", + "videopac-g7400": "Philips Videopac+", + "pinball": "Pinball", + "rca-studio-ii": "RCA Studio II", + "sam-coupe": "SAM Coupé", + "neogeoaes": "SNK Neo Geo AES", + "neo-geo-cd": "SNK Neo Geo CD", + "neogeomvs": "SNK Neo Geo MVS", + "neo-geo-pocket": "SNK Neo Geo Pocket", + "neo-geo-pocket-color": "SNK Neo Geo Pocket Color", + # "": "Sammy Atomiswave", + "scummvm": "ScummVM", + "sega32": "Sega 32X", + "segacd": "Sega CD", + "segacd32": "Sega CD 32X", + "dc": "Sega Dreamcast", + "vmu": "Sega Dreamcast VMU", + "gamegear": "Sega Game Gear", + "genesis-slash-megadrive": "Sega Genesis", + "hikaru": "Sega Hikaru", + "sms": "Sega Master System", + "model1": "Sega Model 1", + "model2": "Sega Model 2", + "model3": "Sega Model 3", + # "": "Sega Naomi", + # "": "Sega Naomi 2", + "sega-pico": "Sega Pico", + "sc3000": "Sega SC-3000", + "sg1000": "Sega SG-1000", + "stv": "Sega ST-V", + "saturn": "Sega Saturn", + "system16": "Sega System 16", + "system32": "Sega System 32", + # "": "Sega Triforce", + "sharp-mz-80b20002500": "Sharp MZ-2500", + "x1": "Sharp X1", + "sharp-x68000": "Sharp X68000", + "zxs": "Sinclair ZX Spectrum", + "sinclair-zx81": "Sinclair ZX-81", + "psp": "Sony PSP", + "psp-minis": "Sony PSP Minis", + "ps": "Sony Playstation", + "ps2": "Sony Playstation 2", + "ps3": "Sony Playstation 3", + "ps4--1": "Sony Playstation 4", + "ps5": "Sony Playstation 5", + "psvita": "Sony Playstation Vita", + "pocketstation": "Sony PocketStation", + "sord-m5": "Sord M5", + "spectravideo": "Spectravideo", + "snes": "Super Nintendo Entertainment System", + "trs-80-color-computer": "TRS-80 Color Computer", + "type-x": "Taito Type X", + "trs-80": "Tandy TRS-80", + "zod": "Tapwave Zodiac", + "ti-994a": "Texas Instruments TI 99/4A", + "game-dot-com": "Tiger Game.com", + "tomy-tutor": "Tomy Tutor", + "creativision": "VTech CreatiVision", + "socrates": "VTech Socrates", + "vsmile": "VTech V.Smile", + "06c": "Vector-06C", + "watara-slash-quickshot-supervision": "Watara Supervision", + "browser": "Web Browser", + "win": "Windows", + "win3x": "Windows 3.X", + "action-max": "WoW Action Max", + "wonderswan": "WonderSwan", + "wonderswan-color": "WonderSwan Color", + "xavixport": "XaviXPORT", + "zinc": "ZiNc", } # Reverse lookup LAUNCHBOX_PLATFORM_NAME_TO_SLUG = { - v["id"]: k for k, v in SLUG_TO_LAUNCHBOX_PLATFORM_NAME.items() + v: k for k, v in SLUG_TO_LAUNCHBOX_PLATFORM_NAME.items() } diff --git a/backend/handler/scan_handler.py b/backend/handler/scan_handler.py index 6dac46170..b9b2a7de2 100644 --- a/backend/handler/scan_handler.py +++ b/backend/handler/scan_handler.py @@ -9,11 +9,13 @@ from handler.filesystem import fs_asset_handler, fs_firmware_handler, fs_rom_han from handler.filesystem.roms_handler import FSRom from handler.metadata import ( meta_igdb_handler, + meta_launchbox_handler, meta_moby_handler, meta_ra_handler, meta_ss_handler, ) from handler.metadata.igdb_handler import IGDBPlatform, IGDBRom +from handler.metadata.launchbox_handler import LaunchboxPlatform, LaunchboxRom from handler.metadata.moby_handler import MobyGamesPlatform, MobyGamesRom from handler.metadata.ra_handler import RAGameRom, RAGamesPlatform from handler.metadata.ss_handler import SSPlatform, SSRom @@ -43,6 +45,7 @@ class MetadataSource: MOBY = "moby" SS = "ss" RA = "ra" + LB = "lb" async def fetch_ra_info( @@ -144,16 +147,26 @@ async def scan_platform( if MetadataSource.SS in metadata_sources else SSPlatform(ss_id=None, slug=platform_attrs["slug"]) ) - ra_platform = ( meta_ra_handler.get_platform(platform_attrs["slug"]) if MetadataSource.RA in metadata_sources else RAGamesPlatform(ra_id=None, slug=platform_attrs["slug"]) ) + launchbox_platform = ( + meta_launchbox_handler.get_platform(platform_attrs["slug"]) + if MetadataSource.LB in metadata_sources + else LaunchboxPlatform(slug=platform_attrs["slug"]) + ) platform_attrs["name"] = platform_attrs["slug"].replace("-", " ").title() platform_attrs.update( - {**ra_platform, **moby_platform, **ss_platform, **igdb_platform} + { + **launchbox_platform, + **ra_platform, + **moby_platform, + **ss_platform, + **igdb_platform, + } ) # Reverse order if ( @@ -355,9 +368,22 @@ async def scan_rom( return SSRom(ss_id=None) + async def fetch_launchbox_rom(): + if MetadataSource.IGDB in metadata_sources and ( + not rom + or scan_type == ScanType.COMPLETE + or (scan_type == ScanType.PARTIAL and not rom.launchbox_id) + or (scan_type == ScanType.UNIDENTIFIED and not rom.launchbox_id) + ): + return await meta_launchbox_handler.get_rom(rom_attrs["fs_name"]) + + return LaunchboxRom(launchbox_id=None) + # Run both metadata fetches concurrently - igdb_handler_rom, moby_handler_rom, ss_handler_rom = await asyncio.gather( - fetch_igdb_rom(), fetch_moby_rom(), fetch_ss_rom() + igdb_handler_rom, moby_handler_rom, ss_handler_rom, launchbox_handler_rom = ( + await asyncio.gather( + fetch_igdb_rom(), fetch_moby_rom(), fetch_ss_rom(), fetch_launchbox_rom() + ) ) if rom: @@ -368,15 +394,25 @@ async def scan_rom( rom_attrs.update({**ss_handler_rom}) if igdb_handler_rom.get("igdb_id"): rom_attrs.update({**igdb_handler_rom}) + if launchbox_handler_rom.get("launchbox_id"): + rom_attrs.update({**launchbox_handler_rom}) else: # Reversed to prioritize IGDB - rom_attrs.update({**moby_handler_rom, **ss_handler_rom, **igdb_handler_rom}) + rom_attrs.update( + { + **launchbox_handler_rom, + **moby_handler_rom, + **ss_handler_rom, + **igdb_handler_rom, + } + ) - # If not found in IGDB, MobyGames and Screenscraper + # If not found in any metadata source, we return the rom with the default values if ( not igdb_handler_rom.get("igdb_id") and not moby_handler_rom.get("moby_id") and not ss_handler_rom.get("ss_id") + and not launchbox_handler_rom.get("launchbox_id") ): log.warning( emoji.emojize(f"{hl(rom_attrs['fs_name'])} not identified :cross_mark:"), diff --git a/backend/models/rom.py b/backend/models/rom.py index 4c1670724..349b54b73 100644 --- a/backend/models/rom.py +++ b/backend/models/rom.py @@ -117,6 +117,7 @@ class Rom(BaseModel): moby_id: Mapped[int | None] ss_id: Mapped[int | None] ra_id: Mapped[int | None] + launchbox_id: Mapped[int | None] __table_args__ = ( Index("idx_roms_igdb_id", "igdb_id"), @@ -124,6 +125,7 @@ class Rom(BaseModel): Index("idx_roms_ss_id", "ss_id"), Index("idx_roms_ra_id", "ra_id"), Index("idx_roms_sgdb_id", "sgdb_id"), + Index("idx_roms_launchbox_id", "launchbox_id"), ) fs_name: Mapped[str] = mapped_column(String(length=450)) @@ -147,6 +149,9 @@ class Rom(BaseModel): ra_metadata: Mapped[dict[str, Any] | None] = mapped_column( CustomJSON(), default=dict ) + launchbox_metadata: Mapped[dict[str, Any] | None] = mapped_column( + CustomJSON(), default=dict + ) path_cover_s: Mapped[str | None] = mapped_column(Text, default="") path_cover_l: Mapped[str | None] = mapped_column(Text, default="") diff --git a/frontend/src/__generated__/index.ts b/frontend/src/__generated__/index.ts index 4c0458a40..76dc9a4be 100644 --- a/frontend/src/__generated__/index.ts +++ b/frontend/src/__generated__/index.ts @@ -6,6 +6,7 @@ export type { AddFirmwareResponse } from './models/AddFirmwareResponse'; export type { Body_add_collection_api_collections_post } from './models/Body_add_collection_api_collections_post'; export type { Body_add_firmware_api_firmware_post } from './models/Body_add_firmware_api_firmware_post'; +export type { Body_create_user_from_invite_api_users_register_post } from './models/Body_create_user_from_invite_api_users_register_post'; export type { Body_request_password_reset_api_forgot_password_post } from './models/Body_request_password_reset_api_forgot_password_post'; export type { Body_reset_password_api_reset_password_post } from './models/Body_reset_password_api_reset_password_post'; export type { Body_token_api_token_post } from './models/Body_token_api_token_post'; diff --git a/frontend/src/__generated__/models/Body_create_user_from_invite_api_users_register_post.ts b/frontend/src/__generated__/models/Body_create_user_from_invite_api_users_register_post.ts new file mode 100644 index 000000000..801608c5c --- /dev/null +++ b/frontend/src/__generated__/models/Body_create_user_from_invite_api_users_register_post.ts @@ -0,0 +1,11 @@ +/* generated using openapi-typescript-codegen -- do not edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +export type Body_create_user_from_invite_api_users_register_post = { + username: string; + email: string; + password: string; + token: string; +}; +