mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 10:08:40 +01:00
search sgdb after getting all game titles
This commit is contained in:
@@ -74,7 +74,7 @@ def _normalize_search_term(
|
||||
|
||||
# Remove punctuation and normalize spaces in one step
|
||||
if remove_punctuation:
|
||||
name = NON_WORD_SPACE_PATTERN.sub("", name)
|
||||
name = NON_WORD_SPACE_PATTERN.sub(" ", name)
|
||||
name = MULTIPLE_SPACE_PATTERN.sub(" ", name)
|
||||
|
||||
# Unicode normalization and accent removal
|
||||
|
||||
@@ -46,39 +46,45 @@ class SGDBBaseHandler(MetadataHandler):
|
||||
|
||||
return list(filter(None, results))
|
||||
|
||||
async def get_details_by_name(self, game_name: str) -> SGDBRom:
|
||||
search_term = self.normalize_search_term(game_name, remove_articles=False)
|
||||
games = await self.sgdb_service.search_games(term=search_term)
|
||||
if not games:
|
||||
log.debug(f"Could not find '{search_term}' on SteamGridDB")
|
||||
return SGDBRom(sgdb_id=None)
|
||||
async def get_details_by_names(self, game_names: list[str]) -> SGDBRom:
|
||||
for game_name in game_names:
|
||||
search_term = self.normalize_search_term(game_name, remove_articles=False)
|
||||
games = await self.sgdb_service.search_games(term=search_term)
|
||||
if not games:
|
||||
log.debug(f"Could not find '{search_term}' on SteamGridDB")
|
||||
continue
|
||||
|
||||
# SGDB search is fuzzy so no need to split the search term by special characters
|
||||
for game in games:
|
||||
game_name_lower = game["name"].lower()
|
||||
game_name_normalized = self.normalize_search_term(
|
||||
game["name"], remove_articles=False
|
||||
)
|
||||
|
||||
if (
|
||||
game_name_lower == search_term.lower()
|
||||
or game_name_normalized == search_term
|
||||
):
|
||||
game_details = await self._get_game_covers(
|
||||
game_id=game["id"],
|
||||
game_name=game["name"],
|
||||
is_nsfw=False,
|
||||
is_humor=False,
|
||||
is_epilepsy=False,
|
||||
# SGDB search is fuzzy so no need to split the search term by special characters
|
||||
for game in games:
|
||||
game_name_lower = game["name"].lower()
|
||||
game_name_normalized = self.normalize_search_term(
|
||||
game["name"], remove_articles=False
|
||||
)
|
||||
|
||||
first_resource = next(
|
||||
(res for res in game_details["resources"] if res["url"]), None
|
||||
)
|
||||
if first_resource:
|
||||
return SGDBRom(sgdb_id=game["id"], url_cover=first_resource["url"])
|
||||
print(f"Checking {game_name_lower} against {search_term.lower()}")
|
||||
print(f"Checking {game_name_normalized} against {search_term}")
|
||||
|
||||
log.debug(f"No exact match found for '{game_name}' on SteamGridDB")
|
||||
if (
|
||||
game_name_lower == search_term.lower()
|
||||
or game_name_normalized == search_term
|
||||
):
|
||||
game_details = await self._get_game_covers(
|
||||
game_id=game["id"],
|
||||
game_name=game["name"],
|
||||
is_nsfw=False,
|
||||
is_humor=False,
|
||||
is_epilepsy=False,
|
||||
)
|
||||
|
||||
first_resource = next(
|
||||
(res for res in game_details["resources"] if res["url"]), None
|
||||
)
|
||||
if first_resource:
|
||||
return SGDBRom(
|
||||
sgdb_id=game["id"], url_cover=first_resource["url"]
|
||||
)
|
||||
|
||||
log.debug(f"No exact match found for '{game_names}' on SteamGridDB")
|
||||
return SGDBRom(sgdb_id=None)
|
||||
|
||||
async def _get_game_covers(
|
||||
|
||||
@@ -505,21 +505,6 @@ async def scan_rom(
|
||||
|
||||
return HasheousRom(hasheous_id=None, igdb_id=None, tgdb_id=None, ra_id=None)
|
||||
|
||||
async def fetch_sgdb_details() -> SGDBRom:
|
||||
"""Fetch SteamGridDB details for the ROM."""
|
||||
if (
|
||||
MetadataSource.SGDB in metadata_sources
|
||||
and newly_added
|
||||
or scan_type == ScanType.COMPLETE
|
||||
or (scan_type == ScanType.PARTIAL and not rom.sgdb_id)
|
||||
or (scan_type == ScanType.UNIDENTIFIED and not rom.sgdb_id)
|
||||
):
|
||||
return await meta_sgdb_handler.get_details_by_name(
|
||||
rom_attrs["fs_name_no_tags"]
|
||||
)
|
||||
|
||||
return SGDBRom(sgdb_id=None)
|
||||
|
||||
# Run metadata fetches concurrently
|
||||
(
|
||||
igdb_handler_rom,
|
||||
@@ -528,7 +513,6 @@ async def scan_rom(
|
||||
ra_handler_rom,
|
||||
launchbox_handler_rom,
|
||||
hasheous_handler_rom,
|
||||
sgdb_handler_rom,
|
||||
) = await asyncio.gather(
|
||||
fetch_igdb_rom(playmatch_hash_match, hasheous_hash_match),
|
||||
fetch_moby_rom(),
|
||||
@@ -536,7 +520,6 @@ async def scan_rom(
|
||||
fetch_ra_rom(hasheous_hash_match),
|
||||
fetch_launchbox_rom(platform.slug),
|
||||
fetch_hasheous_rom(hasheous_hash_match),
|
||||
fetch_sgdb_details(),
|
||||
)
|
||||
|
||||
# Only update fields if match is found
|
||||
@@ -552,8 +535,6 @@ async def scan_rom(
|
||||
rom_attrs.update({**hasheous_handler_rom})
|
||||
if igdb_handler_rom.get("igdb_id"):
|
||||
rom_attrs.update({**igdb_handler_rom})
|
||||
if sgdb_handler_rom.get("sgdb_id"):
|
||||
rom_attrs.update({**sgdb_handler_rom})
|
||||
|
||||
# If not found in any metadata source, we return the rom with the default values
|
||||
if (
|
||||
@@ -563,7 +544,6 @@ async def scan_rom(
|
||||
and not ra_handler_rom.get("ra_id")
|
||||
and not launchbox_handler_rom.get("launchbox_id")
|
||||
and not hasheous_handler_rom.get("hasheous_id")
|
||||
and not sgdb_handler_rom.get("sgdb_id")
|
||||
):
|
||||
log.warning(
|
||||
emoji.emojize(f"{hl(rom_attrs['fs_name'])} not identified :cross_mark:"),
|
||||
@@ -571,6 +551,32 @@ async def scan_rom(
|
||||
)
|
||||
return Rom(**rom_attrs)
|
||||
|
||||
async def fetch_sgdb_details() -> SGDBRom:
|
||||
"""Fetch SteamGridDB details for the ROM."""
|
||||
if (
|
||||
MetadataSource.SGDB in metadata_sources
|
||||
and newly_added
|
||||
or scan_type == ScanType.COMPLETE
|
||||
or (scan_type == ScanType.PARTIAL and not rom.sgdb_id)
|
||||
or (scan_type == ScanType.UNIDENTIFIED and not rom.sgdb_id)
|
||||
):
|
||||
game_names = [
|
||||
igdb_handler_rom.get("name", None),
|
||||
hasheous_handler_rom.get("name", None),
|
||||
ss_handler_rom.get("name", None),
|
||||
moby_handler_rom.get("name", None),
|
||||
launchbox_handler_rom.get("name", None),
|
||||
rom_attrs["fs_name_no_tags"],
|
||||
]
|
||||
game_names = [name for name in game_names if name]
|
||||
return await meta_sgdb_handler.get_details_by_names(game_names)
|
||||
|
||||
return SGDBRom(sgdb_id=None)
|
||||
|
||||
sgdb_hander_rom = await fetch_sgdb_details()
|
||||
if sgdb_hander_rom.get("sgdb_id"):
|
||||
rom_attrs.update({**sgdb_hander_rom})
|
||||
|
||||
log.info(
|
||||
emoji.emojize(
|
||||
f"{hl(rom_attrs['fs_name'])} identified as {hl(rom_attrs['name'], color=BLUE)} :alien_monster:"
|
||||
|
||||
Reference in New Issue
Block a user