Use the requests library consistently

This commit is contained in:
kramo
2023-04-06 13:55:49 +02:00
parent ed3b8d7019
commit d2064ecaf4
5 changed files with 89 additions and 71 deletions

View File

@@ -15,14 +15,13 @@ jobs:
with:
msystem: UCRT64
update: true
install: mingw-w64-ucrt-x86_64-gtk4 mingw-w64-ucrt-x86_64-libadwaita mingw-w64-ucrt-x86_64-python-gobject mingw-w64-ucrt-x86_64-python-yaml mingw-w64-ucrt-x86_64-python-pip mingw-w64-ucrt-x86_64-desktop-file-utils mingw-w64-ucrt-x86_64-ca-certificates mingw-w64-ucrt-x86_64-meson git
install: mingw-w64-ucrt-x86_64-gtk4 mingw-w64-ucrt-x86_64-libadwaita mingw-w64-ucrt-x86_64-python-gobject mingw-w64-ucrt-x86_64-python-yaml mingw-w64-ucrt-x86_64-python-requests mingw-w64-ucrt-x86_64-desktop-file-utils mingw-w64-ucrt-x86_64-ca-certificates mingw-w64-ucrt-x86_64-meson git
- name: Compile
shell: msys2 {0}
run: |
pip install python-steamgriddb
meson setup _build
ninja -C _build install
pacman --noconfirm -Rs mingw-w64-ucrt-x86_64-python-pip mingw-w64-ucrt-x86_64-desktop-file-utils mingw-w64-ucrt-x86_64-meson git
pacman --noconfirm -Rs mingw-w64-ucrt-x86_64-desktop-file-utils mingw-w64-ucrt-x86_64-meson git
find /ucrt64/share/locale/ -type f ! -name "*cartridges.mo" -delete
- name: "Inno Setup"
run: iscc ".\.windows\Cartridges.iss"

View File

@@ -34,6 +34,11 @@
"*.la",
"*.a"
],
"modules" : [
{
"name": "python3-modules",
"buildsystem": "simple",
"build-commands": [],
"modules": [
{
"name": "python3-pyyaml",
@@ -50,10 +55,10 @@
]
},
{
"name": "python3-python-steamgriddb",
"name": "python3-requests",
"buildsystem": "simple",
"build-commands": [
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"python-steamgriddb\" --no-build-isolation"
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"requests\" --no-build-isolation"
],
"sources": [
{
@@ -71,11 +76,6 @@
"url": "https://files.pythonhosted.org/packages/fc/34/3030de6f1370931b9dbb4dad48f6ab1015ab1d32447850b9fc94e60097be/idna-3.4-py3-none-any.whl",
"sha256": "90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/d6/90/a84d927799ca177d4f7a111f99fee0a3f19da63e42c3b325c9c1bfe3bba0/python-steamgriddb-1.0.5.tar.gz",
"sha256": "036db7bb09865da73b40b68cf04fb9675cd18b4908275092d91f37bf16245069"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/d2/f4/274d1dbe96b41cf4e0efb70cbced278ffd61b5c7bb70338b62af94ccb25b/requests-2.28.2-py3-none-any.whl",
@@ -87,6 +87,8 @@
"sha256": "aa751d169e23c7479ce47a0cb0da579e3ede798f994f5816a74e4f4500dcea42"
}
]
}
]
},
{
"name" : "blueprint-compiler",

View File

@@ -18,12 +18,12 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import os
import urllib.request
from pathlib import Path
from shutil import copyfile
from sqlite3 import connect
from time import time
import requests
from gi.repository import GdkPixbuf, Gio
@@ -53,9 +53,10 @@ def get_game(task, current_time, parent_widget, row):
if row[3] or row[2]:
tmp_file = Gio.File.new_tmp(None)[0]
try:
with urllib.request.urlopen(row[3] or row[2], timeout=5) as open_file:
Path(tmp_file.get_path()).write_bytes(open_file.read())
except urllib.error.URLError:
with requests.get(row[3] or row[2], timeout=5) as cover:
cover.raise_for_status()
Path(tmp_file.get_path()).write_bytes(cover.content)
except requests.exceptions.RequestException:
task.return_value((values, None))
return

View File

@@ -17,18 +17,17 @@
#
# SPDX-License-Identifier: GPL-3.0-or-later
import json
import os
import re
import urllib.request
from pathlib import Path
from time import time
import requests
from gi.repository import Gio
def update_values_from_data(content, values):
basic_data = json.loads(content)[values["appid"]]
basic_data = content[values["appid"]]
if not basic_data["success"]:
values["blacklisted"] = True
else:
@@ -80,12 +79,13 @@ def get_game(task, datatypes, current_time, parent_widget, appmanifest, steam_di
)
try:
with urllib.request.urlopen(
with requests.get(
f'https://store.steampowered.com/api/appdetails?appids={values["appid"]}',
timeout=5,
) as open_file:
content = open_file.read().decode("utf-8")
except urllib.error.URLError:
open_file.raise_for_status()
content = open_file.json()
except requests.exceptions.RequestException:
task.return_value((values, image_path if image_path.exists() else None))
return

View File

@@ -2,7 +2,6 @@ from pathlib import Path
import requests
from gi.repository import Gio
from steamgrid import SteamGridDB, http
from .create_dialog import create_dialog
from .save_cover import save_cover
@@ -11,7 +10,6 @@ from .save_cover import save_cover
class SGDBSave:
def __init__(self, parent_widget, games, importer=None):
self.parent_widget = parent_widget
self.sgdb = SteamGridDB(self.parent_widget.schema.get_string("sgdb-key"))
self.importer = importer
self.exception = None
@@ -41,29 +39,47 @@ class SGDBSave:
if not self.importer:
self.parent_widget.loading = game[0]
url = "https://www.steamgriddb.com/api/v2/"
headers = {
"Authorization": f'Bearer {self.parent_widget.schema.get_string("sgdb-key")}'
}
try:
search_result = self.sgdb.search_game(game[1])
search_result = requests.get(
f"{url}search/autocomplete/{game[1]}",
headers=headers,
timeout=5,
)
search_result.raise_for_status()
except requests.exceptions.RequestException:
task.return_value(game[0])
return
except http.HTTPException as exception:
self.exception = str(exception)
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
)
task.return_value(game[0])
return
try:
grid = self.sgdb.get_grids_by_gameid(
[search_result[0].id], is_nsfw=False
)[0]
except (TypeError, IndexError):
headers["dimensions"] = "600x900"
grid = requests.get(
f'{url}grids/game/{search_result.json()["data"][0]["id"]}',
headers=headers,
timeout=5,
)
except (requests.exceptions.RequestException, IndexError):
task.return_value(game[0])
return
tmp_file = Gio.File.new_tmp(None)[0]
try:
response = requests.get(str(grid), timeout=5)
except requests.exceptions.RequestException:
response = requests.get(
grid.json()["data"][0]["url"],
timeout=5,
)
except (requests.exceptions.RequestException, IndexError):
task.return_value(game[0])
return