mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 18:11:16 +01:00
igdb api calls refactored
This commit is contained in:
@@ -39,7 +39,7 @@ async def updateRom(req: Request, p_slug: str, id: int) -> dict:
|
||||
log.error(error)
|
||||
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=error)
|
||||
updated_rom['file_name_no_tags'] = get_file_name_with_no_tags(updated_rom['file_name'])
|
||||
updated_rom.update(fs.get_cover_details(True, p_slug, updated_rom['file_name'], updated_rom['url_cover']))
|
||||
updated_rom.update(fs.get_cover(True, p_slug, updated_rom['file_name'], updated_rom['url_cover']))
|
||||
dbh.update_rom(id, updated_rom)
|
||||
return {'data': dbh.get_rom(id), 'msg': f"{updated_rom['file_name']} updated successfully!"}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ async def search_rom_igdb(req: Request, igdb_id: str = None) -> dict:
|
||||
log.info(emoji.emojize(":magnifying_glass_tilted_right: IGDB Searching"))
|
||||
if igdb_id:
|
||||
log.info(f"Searching by id: {igdb_id}")
|
||||
matched_roms = igdbh.get_matched_roms_by_id(igdb_id)
|
||||
matched_roms = igdbh.get_matched_rom_by_id(igdb_id)
|
||||
else:
|
||||
log.info(emoji.emojize(f":video_game: {rom['p_slug']}: {COLORS['orange']}{rom['file_name']}{COLORS['reset']}"))
|
||||
matched_roms = igdbh.get_matched_roms(rom['file_name'], rom['p_igdb_id'], rom['p_slug'])
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
import sys
|
||||
import functools
|
||||
import unidecode
|
||||
from unidecode import unidecode as uc
|
||||
from time import time
|
||||
|
||||
import requests
|
||||
from config import CLIENT_ID, CLIENT_SECRET, DEFAULT_URL_COVER_L
|
||||
from utils import get_file_name_with_no_tags
|
||||
from utils import get_file_name_with_no_tags as get_search_term
|
||||
from logger.logger import log
|
||||
|
||||
|
||||
class IGDBHandler():
|
||||
|
||||
def __init__(self) -> None:
|
||||
base_url: str = 'https://api.igdb.com/v4'
|
||||
self.platform_url: str = f'{base_url}/platforms/'
|
||||
self.games_url: str = f'{base_url}/games/'
|
||||
self.covers_url: str = f'{base_url}/covers/'
|
||||
self.twitch_auth: TwitchAuth = TwitchAuth()
|
||||
self.headers = {
|
||||
'Client-ID': self.twitch_auth.client_id,
|
||||
@@ -26,17 +30,32 @@ class IGDBHandler():
|
||||
args[0].headers['Authorization'] = f'Bearer {args[0].twitch_auth.get_oauth_token()}'
|
||||
return func(*args)
|
||||
return wrapper
|
||||
|
||||
|
||||
def _search_rom_by_category(self, search_term: str, p_igdb_id: str, category: int = None) -> dict:
|
||||
category_query: str = f"& category={category}" if category else ""
|
||||
try:
|
||||
return requests.post(self.games_url, headers=self.headers,
|
||||
data=f"search \"{search_term}\"; \
|
||||
fields id, slug, name, summary; \
|
||||
where platforms=[{p_igdb_id}] {category_query};").json()[0]
|
||||
except IndexError:
|
||||
return {}
|
||||
|
||||
|
||||
def _search_cover(self, rom_id: str) -> str:
|
||||
res: dict = requests.post(self.covers_url, headers=self.headers,
|
||||
data=f"fields url; where game={rom_id};").json()[0]
|
||||
return f"https:{res['url']}" if 'url' in res.keys() else ""
|
||||
|
||||
|
||||
@check_twitch_token
|
||||
def get_platform_details(self, slug: str) -> tuple:
|
||||
igdb_id: str = ""
|
||||
name: str = ""
|
||||
def get_platform(self, slug: str) -> tuple:
|
||||
try:
|
||||
res_details: dict = requests.post("https://api.igdb.com/v4/platforms/", headers=self.headers,
|
||||
data=f"fields id, name; where slug=\"{slug}\";").json()[0]
|
||||
igdb_id = res_details['id']
|
||||
name = res_details['name']
|
||||
res: dict = requests.post(self.platform_url, headers=self.headers,
|
||||
data=f"fields id, name; where slug=\"{slug}\";").json()[0]
|
||||
igdb_id = res['id'] if 'id' in res.keys() else ""
|
||||
name = res['name'] if 'name' in res.keys() else ""
|
||||
except IndexError:
|
||||
log.warning(f"{slug} not found in IGDB")
|
||||
if not name: name = slug
|
||||
@@ -44,118 +63,70 @@ class IGDBHandler():
|
||||
|
||||
|
||||
@check_twitch_token
|
||||
def get_rom_details(self, file_name: str, p_igdb_id: int, r_igdb_id_search: str) -> dict:
|
||||
search_term: str = unidecode.unidecode(get_file_name_with_no_tags(file_name))
|
||||
r_igdb_id: str = ""
|
||||
r_slug: str = ""
|
||||
r_name: str = ""
|
||||
summary: str = ""
|
||||
url_cover: str = ""
|
||||
def get_rom_by_id(self, r_igdb_id: str) -> dict:
|
||||
res: dict = requests.post(self.games_url, headers=self.headers,
|
||||
data=f"fields slug, name, summary; where id=[{r_igdb_id}];").json()[0]
|
||||
r_slug = res['slug'] if 'slug' in res.keys() else ""
|
||||
r_name = res['name'] if 'name' in res.keys() else ""
|
||||
summary = res['summary'] if 'summary' in res.keys() else ""
|
||||
return {'r_igdb_id': r_igdb_id, 'r_slug': r_slug, 'r_name': r_name, 'summary': summary, 'url_cover': self._search_cover(r_igdb_id)}
|
||||
|
||||
|
||||
@check_twitch_token
|
||||
def get_rom(self, file_name: str, p_igdb_id: int) -> dict:
|
||||
search_term: str = uc(get_search_term(file_name))
|
||||
res = (self._search_rom_by_category(search_term, p_igdb_id, 0) or
|
||||
self._search_rom_by_category(search_term, p_igdb_id, 10) or
|
||||
self._search_rom_by_category(search_term, p_igdb_id))
|
||||
|
||||
r_igdb_id = res['id'] if 'id' in res.keys() else ""
|
||||
r_slug = res['slug'] if 'slug' in res.keys() else ""
|
||||
r_name = res['name'] if 'name' in res.keys() else ""
|
||||
summary = res['summary'] if 'summary' in res.keys() else ""
|
||||
|
||||
if r_igdb_id_search:
|
||||
res_details: dict = requests.post("https://api.igdb.com/v4/games/", headers=self.headers,
|
||||
data=f"fields id, slug, name, summary; where id={r_igdb_id_search};").json()[0]
|
||||
r_igdb_id = res_details['id']
|
||||
r_slug = res_details['slug']
|
||||
r_name = res_details['name']
|
||||
try:
|
||||
summary = res_details['summary']
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
else: #TODO: improve API calls to make only one
|
||||
if p_igdb_id:
|
||||
try:
|
||||
res_details: dict = requests.post("https://api.igdb.com/v4/games/", headers=self.headers,
|
||||
data=f"search \"{search_term}\";fields id, slug, name, summary; where platforms=[{p_igdb_id}] & category=0;").json()[0]
|
||||
r_igdb_id = res_details['id']
|
||||
r_slug = res_details['slug']
|
||||
r_name = res_details['name']
|
||||
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 \"{search_term}\";fields name, id, slug, summary; where platforms=[{p_igdb_id}] & category=10;").json()[0]
|
||||
r_igdb_id = res_details['id']
|
||||
r_slug = res_details['slug']
|
||||
r_name = res_details['name']
|
||||
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 \"{search_term}\";fields name, id, slug, summary; where platforms=[{p_igdb_id}];").json()[0]
|
||||
r_igdb_id = res_details['id']
|
||||
r_slug = res_details['slug']
|
||||
r_name = res_details['name']
|
||||
try:
|
||||
summary = res_details['summary']
|
||||
except KeyError:
|
||||
pass
|
||||
except IndexError:
|
||||
log.warning(f"{file_name} not found in IGDB")
|
||||
if r_igdb_id:
|
||||
try:
|
||||
res_details: dict = requests.post("https://api.igdb.com/v4/covers/", headers=self.headers,
|
||||
data=f"fields url; where game={r_igdb_id};").json()[0]
|
||||
url_cover: str = f"https:{res_details['url']}"
|
||||
except IndexError:
|
||||
log.warning(f"{r_name} cover not found in IGDB")
|
||||
if not r_name: r_name = search_term
|
||||
return {'r_igdb_id': r_igdb_id, 'r_slug': r_slug, 'r_name': r_name, 'summary': summary, 'url_cover': url_cover}
|
||||
|
||||
if not r_igdb_id: log.warning(f"{r_name} not found in IGDB")
|
||||
return {'r_igdb_id': r_igdb_id, 'r_slug': r_slug, 'r_name': r_name, 'summary': summary, 'url_cover': self._search_cover(r_igdb_id)}
|
||||
|
||||
|
||||
@check_twitch_token
|
||||
def get_matched_rom_by_id(self, igdb_id: str) -> list:
|
||||
matched_rom: list[dict] = []
|
||||
res: list = requests.post(self.games_url, headers=self.headers,
|
||||
data=f"fields name, id, slug, summary; where id={igdb_id};")
|
||||
if res.status_code == 200:
|
||||
matched_rom = res.json()
|
||||
for rom in matched_rom:
|
||||
rom['url_cover'] = self._search_cover(rom['id']).replace('t_thumb', f't_cover_big')
|
||||
rom['r_igdb_id'] = rom.pop('id')
|
||||
rom['r_slug'] = rom.pop('slug')
|
||||
rom['r_name'] = rom.pop('name')
|
||||
return matched_rom
|
||||
|
||||
|
||||
@check_twitch_token
|
||||
def get_matched_roms(self, file_name: str, p_igdb_id: int, p_slug: str) -> list:
|
||||
matched_roms: list[dict] = []
|
||||
if p_igdb_id != '':
|
||||
search_term: str = unidecode.unidecode(get_file_name_with_no_tags(file_name))
|
||||
matched_roms: list = requests.post("https://api.igdb.com/v4/games/", headers=self.headers,
|
||||
data=f"search \"{search_term}\";fields name, id, slug, summary; where platforms=[{p_igdb_id}];").json()
|
||||
if p_igdb_id:
|
||||
matched_roms: list = requests.post(self.games_url, headers=self.headers,
|
||||
data=f"search \"{uc(get_search_term(file_name))}\"; \
|
||||
fields name, id, slug, summary; \
|
||||
where platforms=[{p_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['url_cover'] = self._search_cover(rom['id']).replace('t_thumb', f't_cover_big')
|
||||
rom['r_igdb_id'] = rom.pop('id')
|
||||
rom['r_slug'] = rom.pop('slug')
|
||||
rom['r_name'] = rom.pop('name')
|
||||
else:
|
||||
log.warning(f"{p_slug} is not supported!")
|
||||
return matched_roms
|
||||
|
||||
|
||||
def get_matched_roms_by_id(self, igdb_id: str) -> list:
|
||||
res: list = requests.post("https://api.igdb.com/v4/games/", headers=self.headers,
|
||||
data=f"fields name, id, slug, summary; where id={igdb_id};")
|
||||
if res.status_code == 200:
|
||||
matched_roms = res.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')
|
||||
rom['r_name'] = rom.pop('name')
|
||||
else:
|
||||
matched_roms: list = []
|
||||
return matched_roms
|
||||
|
||||
|
||||
|
||||
class TwitchAuth():
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.base_url: str = 'https://id.twitch.tv/oauth2/token'
|
||||
self.token: str = ""
|
||||
self.token_checkout: int = int(time())
|
||||
self.SECURE_SECONDS_OFFSET: int = 10 # seconds offset to avoid invalid token
|
||||
@@ -169,7 +140,7 @@ class TwitchAuth():
|
||||
|
||||
|
||||
def _update_twitch_token(self) -> str:
|
||||
res = requests.post(url=f"https://id.twitch.tv/oauth2/token",
|
||||
res = requests.post(url=self.base_url,
|
||||
params={
|
||||
'client_id': self.client_id,
|
||||
'client_secret': self.client_secret,
|
||||
|
||||
@@ -24,7 +24,7 @@ def scan_platform(fs_slug: str) -> Platform:
|
||||
platform_attrs['slug'] = fs_slug
|
||||
except (KeyError, TypeError, AttributeError):
|
||||
platform_attrs['slug'] = fs_slug
|
||||
platform_attrs.update(igdbh.get_platform_details(platform_attrs['slug']))
|
||||
platform_attrs.update(igdbh.get_platform(platform_attrs['slug']))
|
||||
platform_attrs['n_roms'] = len(fs.get_roms(platform_attrs['fs_slug']))
|
||||
platform = Platform(**platform_attrs)
|
||||
return platform
|
||||
@@ -33,8 +33,9 @@ def scan_platform(fs_slug: str) -> Platform:
|
||||
def scan_rom(platform: Platform, rom_attrs: dict, r_igbd_id_search: str = '', overwrite: bool = False) -> Rom:
|
||||
p_slug: str = platform.fs_slug if platform.fs_slug else platform.slug
|
||||
roms_path: str = fs.get_roms_structure(p_slug)
|
||||
rom_attrs.update(igdbh.get_rom_details(rom_attrs['file_name'], platform.igdb_id, r_igbd_id_search))
|
||||
rom_attrs.update(fs.get_cover_details(overwrite, platform.slug, rom_attrs['file_name'], rom_attrs['url_cover']))
|
||||
if r_igbd_id_search: rom_attrs.update(igdbh.get_rom_by_id(r_igbd_id_search))
|
||||
else: rom_attrs.update(igdbh.get_rom(rom_attrs['file_name'], platform.igdb_id))
|
||||
rom_attrs.update(fs.get_cover(overwrite, platform.slug, rom_attrs['file_name'], rom_attrs['url_cover']))
|
||||
file_size, file_size_units = fs.get_rom_size(rom_attrs['multi'], rom_attrs['file_name'], rom_attrs['files'], roms_path)
|
||||
reg, rev, other_tags = parse_tags(rom_attrs['file_name'])
|
||||
rom_attrs.update({'file_path': roms_path, 'file_name': rom_attrs['file_name'], 'file_name_no_tags': get_file_name_with_no_tags(rom_attrs['file_name']),
|
||||
|
||||
@@ -57,7 +57,7 @@ def _get_cover_path(p_slug: str, file_name: str, size: str) -> str:
|
||||
return f"{RESOURCES_BASE_PATH}/{p_slug}/{file_name}_{size}.png"
|
||||
|
||||
|
||||
def get_cover_details(overwrite: bool, p_slug: str, file_name: str, url_cover: str) -> tuple:
|
||||
def get_cover(overwrite: bool, p_slug: str, file_name: str, url_cover: str) -> tuple:
|
||||
path_cover_s: str = DEFAULT_PATH_COVER_S
|
||||
path_cover_l: str = DEFAULT_PATH_COVER_L
|
||||
has_cover: int = 0
|
||||
|
||||
Reference in New Issue
Block a user