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 .game import Game
from .save_cover import resize_cover, save_cover
from .steamgriddb import SGDBSave
from .steamgriddb import SGDBSave, needs_cover
class Importer:
@@ -58,7 +58,7 @@ class Importer:
if 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))
self.games.add(game)

View File

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