Potentially fix bad fd error

This commit is contained in:
kramo
2023-04-19 22:46:57 +02:00
parent 2b2b7e0c75
commit 7a5450d7d8
2 changed files with 58 additions and 55 deletions

View File

@@ -24,7 +24,7 @@ from gi.repository import Adw, Gtk
from .create_dialog import create_dialog from .create_dialog import create_dialog
from .game import Game from .game import Game
from .save_cover import resize_cover, save_cover from .save_cover import resize_cover, save_cover
from .steamgriddb import SGDBSave from .steamgriddb import SGDBSave, needs_cover
class Importer: class Importer:
@@ -58,7 +58,7 @@ class Importer:
if values: if values:
game = Game(self.win, values) game = Game(self.win, values)
if cover_path: if not needs_cover(self.win.schema, cover_path):
save_cover(self.win, game.game_id, resize_cover(self.win, cover_path)) save_cover(self.win, game.game_id, resize_cover(self.win, cover_path))
self.games.add(game) self.games.add(game)

View File

@@ -7,6 +7,12 @@ from .create_dialog import create_dialog
from .save_cover import save_cover, resize_cover from .save_cover import save_cover, resize_cover
def needs_cover(schema, previous):
return schema.get_boolean("sgdb") and (
(schema.get_boolean("sgdb-prefer")) or not previous
)
class SGDBSave: class SGDBSave:
def __init__(self, win, games, importer=None): def __init__(self, win, games, importer=None):
self.win = win self.win = win
@@ -27,72 +33,69 @@ class SGDBSave:
Gio.Task.new(None, None, self.task_done).run_in_thread(create_func(game)) Gio.Task.new(None, None, self.task_done).run_in_thread(create_func(game))
def update_cover(self, task, game): def update_cover(self, task, game):
if self.win.schema.get_boolean("sgdb") and ( if not needs_cover(
self.win.schema.get_boolean("sgdb-prefer") self.win.schema,
or ( (self.win.covers_dir / f"{game.game_id}.gif").is_file()
not (self.win.covers_dir / f"{game.game_id}.gif").is_file() or (self.win.covers_dir / f"{game.game_id}.tiff").is_file(),
and not (self.win.covers_dir / f"{game.game_id}.tiff").is_file()
)
): ):
if not self.importer: task.return_value(game)
game.set_loading(1) return
url = "https://www.steamgriddb.com/api/v2/" if not self.importer:
headers = { game.set_loading(1)
"Authorization": f'Bearer {self.win.schema.get_string("sgdb-key")}'
}
try: url = "https://www.steamgriddb.com/api/v2/"
search_result = requests.get( headers = {"Authorization": f'Bearer {self.win.schema.get_string("sgdb-key")}'}
f"{url}search/autocomplete/{game.name}",
headers=headers, try:
timeout=5, search_result = requests.get(
f"{url}search/autocomplete/{game.name}",
headers=headers,
timeout=5,
)
if search_result.status_code != 200:
self.exception = str(
search_result.json()["errors"][0]
if "errors" in tuple(search_result.json())
else search_result.status_code
) )
if search_result.status_code != 200: search_result.raise_for_status()
self.exception = str( except requests.exceptions.RequestException:
search_result.json()["errors"][0] task.return_value(game)
if "errors" in tuple(search_result.json()) return
else search_result.status_code
)
search_result.raise_for_status()
except requests.exceptions.RequestException:
task.return_value(game)
return
response = None response = None
try: try:
if self.win.schema.get_boolean("sgdb-animated"): if self.win.schema.get_boolean("sgdb-animated"):
try: try:
grid = requests.get(
f'{url}grids/game/{search_result.json()["data"][0]["id"]}?dimensions=600x900&types=animated',
headers=headers,
timeout=5,
)
response = requests.get(
grid.json()["data"][0]["url"], timeout=5
)
except IndexError:
pass
if not response:
grid = requests.get( grid = requests.get(
f'{url}grids/game/{search_result.json()["data"][0]["id"]}?dimensions=600x900', f'{url}grids/game/{search_result.json()["data"][0]["id"]}?dimensions=600x900&types=animated',
headers=headers, headers=headers,
timeout=5, timeout=5,
) )
response = requests.get(grid.json()["data"][0]["url"], timeout=5) response = requests.get(grid.json()["data"][0]["url"], timeout=5)
except (requests.exceptions.RequestException, IndexError): except IndexError:
task.return_value(game) pass
return if not response:
grid = requests.get(
f'{url}grids/game/{search_result.json()["data"][0]["id"]}?dimensions=600x900',
headers=headers,
timeout=5,
)
response = requests.get(grid.json()["data"][0]["url"], timeout=5)
except (requests.exceptions.RequestException, IndexError):
task.return_value(game)
return
tmp_file = Gio.File.new_tmp()[0] tmp_file = Gio.File.new_tmp()[0]
Path(tmp_file.get_path()).write_bytes(response.content) Path(tmp_file.get_path()).write_bytes(response.content)
save_cover( save_cover(
self.win, self.win,
game.game_id, game.game_id,
resize_cover(self.win, tmp_file.get_path()), resize_cover(self.win, tmp_file.get_path()),
) )
task.return_value(game) task.return_value(game)