From 1d2e275968386c482e837fd45654ea5ca3dda4ea Mon Sep 17 00:00:00 2001 From: zurdi zurdo Date: Mon, 13 Mar 2023 13:31:42 +0100 Subject: [PATCH] now it check if platform exists in igdb before triying to get rom details --- backend/src/handler/igdb_handler.py | 58 +++++++++++++++++------------ backend/src/main.py | 1 + 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/backend/src/handler/igdb_handler.py b/backend/src/handler/igdb_handler.py index aff4e65c9..056166aea 100644 --- a/backend/src/handler/igdb_handler.py +++ b/backend/src/handler/igdb_handler.py @@ -58,42 +58,52 @@ class IGDBHandler(): name: str = "" summary: str = "" url_cover: str = "" - try: - res_details: dict = requests.post("https://api.igdb.com/v4/games/", - headers=self.headers, - data=f"search \"{term}\";fields id, slug, name, summary; where platforms=[{platform_id}] & category=0;").json()[0] - igdb_id = res_details['id'] - slug = res_details['slug'] - name = res_details['name'] - summary = res_details['summary'] - except IndexError: + if platform_id: try: res_details: dict = requests.post("https://api.igdb.com/v4/games/", - headers=self.headers, - data=f"search \"{term}\";fields name, id, slug, summary; where platforms=[{platform_id}] & category=10;").json()[0] + headers=self.headers, + data=f"search \"{term}\";fields id, slug, name, summary; where platforms=[{platform_id}] & category=0;").json()[0] igdb_id = res_details['id'] slug = res_details['slug'] name = res_details['name'] - summary = res_details['summary'] + try: + summary = res_details['summary'] + except KeyError: + pass except IndexError: try: res_details: dict = requests.post("https://api.igdb.com/v4/games/", - headers=self.headers, - data=f"search \"{term}\";fields name, id, slug, summary; where platforms=[{platform_id}];").json()[0] + headers=self.headers, + data=f"search \"{term}\";fields name, id, slug, summary; where platforms=[{platform_id}] & category=10;").json()[0] igdb_id = res_details['id'] slug = res_details['slug'] name = res_details['name'] - summary = res_details['summary'] + try: + summary = res_details['summary'] + except KeyError: + pass except IndexError: - log.warning(f"{rom_filename} rom not found in igdb") - if igdb_id: - try: - res_details: dict = requests.post("https://api.igdb.com/v4/covers/", - headers=self.headers, - data=f"fields url; where game={igdb_id};").json()[0] - url_cover: str = f"https:{res_details['url']}" - except IndexError: - log.warning(f"{name} cover not found in igdb") + try: + res_details: dict = requests.post("https://api.igdb.com/v4/games/", + headers=self.headers, + data=f"search \"{term}\";fields name, id, slug, summary; where platforms=[{platform_id}];").json()[0] + igdb_id = res_details['id'] + slug = res_details['slug'] + name = res_details['name'] + try: + summary = res_details['summary'] + except KeyError: + pass + except IndexError: + log.warning(f"{rom_filename} rom not found in igdb") + if igdb_id: + try: + res_details: dict = requests.post("https://api.igdb.com/v4/covers/", + headers=self.headers, + data=f"fields url; where game={igdb_id};").json()[0] + url_cover: str = f"https:{res_details['url']}" + except IndexError: + log.warning(f"{name} cover not found in igdb") return (igdb_id, slug, name, summary, url_cover) diff --git a/backend/src/main.py b/backend/src/main.py index 862a0589f..9280abc36 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -56,6 +56,7 @@ async def scan(overwrite: bool=False): platforms.append(Platform(*details)) for rom_filename in fs.get_roms(platform_slug): + log.info(f"Getting {rom_filename} details") rom_igdb_id, rom_slug, rom_name, summary, url_cover = igdbh.get_rom_details(rom_filename, platform_igdb_id) rom_sgdb_id: str = "" if not rom_name: rom_name = rom_filename