From 8a162686eec0f8c45bb0cbaed763cd528bf951a6 Mon Sep 17 00:00:00 2001 From: zurdi zurdo Date: Wed, 12 Apr 2023 15:32:59 +0200 Subject: [PATCH] search game by id added --- backend/src/handler/igdb_handler.py | 17 ++++++++++++++++- backend/src/main.py | 10 +++++++--- frontend/src/views/Details.vue | 19 ++++++++++++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/backend/src/handler/igdb_handler.py b/backend/src/handler/igdb_handler.py index 5447633a7..64e0125ae 100644 --- a/backend/src/handler/igdb_handler.py +++ b/backend/src/handler/igdb_handler.py @@ -123,7 +123,7 @@ class IGDBHandler(): for rom in matched_roms: try: res_details: dict = requests.post("https://api.igdb.com/v4/covers/", headers=self.headers, - data=f"fields url; where game={rom['id']};").json()[0] + data=f"fields url; where game={rom['id']};").json()[0] rom['url_cover'] = f"https:{res_details['url']}".replace('t_thumb', f't_cover_big') except IndexError: rom['url_cover'] = DEFAULT_URL_COVER_L @@ -132,6 +132,21 @@ class IGDBHandler(): else: log.warning(f"{p_slug} is not supported!") return matched_roms + + def get_matched_roms_by_id(self, igdb_id: str) -> list: + matched_roms: list[dict] = [] + matched_roms: list = requests.post("https://api.igdb.com/v4/games/", headers=self.headers, + data=f"fields name, id, slug, summary; where id={igdb_id};").json() + for rom in matched_roms: + try: + res_details: dict = requests.post("https://api.igdb.com/v4/covers/", headers=self.headers, + data=f"fields url; where game={rom['id']};").json()[0] + rom['url_cover'] = f"https:{res_details['url']}".replace('t_thumb', f't_cover_big') + except IndexError: + rom['url_cover'] = DEFAULT_URL_COVER_L + rom['r_igdb_id'] = rom.pop('id') + rom['r_slug'] = rom.pop('slug') + return matched_roms diff --git a/backend/src/main.py b/backend/src/main.py index c329e9f83..27ac9d05d 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -112,13 +112,17 @@ def remove_rom(p_slug: str, file_name: str, filesystem: bool=False) -> dict: @app.put("/search/roms/igdb") -async def search_rom_igdb(req: Request) -> dict: +async def search_rom_igdb(req: Request, igdb_id: str=None) -> dict: """Get all the roms matched from igdb.""" data: dict = await req.json() log.info(emoji.emojize(":magnifying_glass_tilted_right: IGDB Searching")) - log.info(emoji.emojize(f":video_game: {data['rom']['p_slug']}: {COLORS['orange']}{data['rom']['file_name']}{COLORS['reset']}")) - matched_roms = igdbh.get_matched_roms(data['rom']['file_name'], data['rom']['p_igdb_id'], data['rom']['p_slug']) + if igdb_id: + log.info(f"Searching by id: {igdb_id}") + matched_roms = igdbh.get_matched_roms_by_id(igdb_id) + else: + log.info(emoji.emojize(f":video_game: {data['rom']['p_slug']}: {COLORS['orange']}{data['rom']['file_name']}{COLORS['reset']}")) + matched_roms = igdbh.get_matched_roms(data['rom']['file_name'], data['rom']['p_igdb_id'], data['rom']['p_slug']) log.info("Results:") [log.info(f"\t - {COLORS['blue']}{rom['name']}{COLORS['reset']}") for rom in matched_roms] return {'data': matched_roms} diff --git a/frontend/src/views/Details.vue b/frontend/src/views/Details.vue index 03d4a1360..719cbae4c 100644 --- a/frontend/src/views/Details.vue +++ b/frontend/src/views/Details.vue @@ -8,6 +8,7 @@ import { downloadRom, downloadSave } from '@/utils/utils.js' const rom = ref(JSON.parse(localStorage.getItem('currentRom')) || '') const saveFiles = ref(false) const searching = ref(false) +const igdb_id = ref('') const matchedRoms = ref([]) const updating = ref(false) const editedRomName = ref(rom.value.file_name) @@ -27,7 +28,7 @@ emitter.on('currentRom', (currentRom) => { rom.value = currentRom }) async function searchRomIGDB() { searching.value = true dialogSearchRom.value = true - await axios.put('/api/search/roms/igdb', { + await axios.put('/api/search/roms/igdb?igdb_id='+igdb_id.value, { rom: rom.value }).then((response) => { matchedRoms.value = response.data.data @@ -155,14 +156,30 @@ async function deleteRom() { + Searching... mdi-close + Results found mdi-close + + +