error handling

This commit is contained in:
Fusselkater 2022-11-27 20:26:14 +01:00
parent 0d99fcdcbc
commit 75df076d23
3 changed files with 37 additions and 31 deletions

View file

@ -69,23 +69,26 @@ class GameHandler:
uploads = self.__itch_client.game_uploads(game_id)
for upload in uploads:
if upload[f'p_{os_type}'] == True:
url = self.__itch_client.game_url(upload['id'])
dst = os.path.join(dst_dir, upload['filename'])
# Download game
md5 = hashlib.md5()
try:
url = self.__itch_client.game_url(upload['id'])
dst = os.path.join(dst_dir, upload['filename'])
# Download game
md5 = hashlib.md5()
with requests.get(url, stream=True) as r:
r.raise_for_status()
total_size = int(r.headers.get('content-length', 0))
with tqdm.wrapattr(open(dst, 'wb'), 'write', miniters=1, desc='Downloading game', total=total_size) as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
md5.update(chunk)
if 'md5_hash' in upload and md5.hexdigest() != upload['md5_hash']:
raise HandlerException('Invalid checksum')
return (dst, upload, meta)
with requests.get(url, stream=True) as r:
r.raise_for_status()
total_size = int(r.headers.get('content-length', 0))
with tqdm.wrapattr(open(dst, 'wb'), 'write', miniters=1, desc='Downloading game', total=total_size) as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
md5.update(chunk)
if 'md5_hash' in upload and md5.hexdigest() != upload['md5_hash']:
raise HandlerException('Invalid checksum')
return (dst, upload, meta)
except itch.API_ERROR as err:
raise HandlerException(str(err))
raise HandlerException('No valid upload found')
def list_local(self, games):

View file

@ -42,16 +42,16 @@ class Client:
try:
return response.json()['url']
except requests.exceptions.JSONDecodeError as err:
raise API_ERROR(str(err))
raise API_ERROR('Cound not decode answer: ' + str(err))
except KeyError as err:
raise API_ERROR(str(err))
raise API_ERROR('Could not find ' + str(err))
def game_meta(self, game_id: int):
url = urljoin(self.ITCH_API_BASE, f'{self.__api_key}/game/{game_id}')
response = self.__session.get(url)
try:
return response.json()['game']
except requests.exceptions.JSONDecodeError as err:
raise API_ERROR(str(err))
raise API_ERROR('Cound not decode answer: ' + str(err))
except KeyError as err:
raise API_ERROR(str(err))
raise API_ERROR('Could not find ' + str(err))

View file

@ -73,18 +73,21 @@ def parse_install(args):
game_path = os.path.join(config['global']['game_dir'], args.game_id)
os.makedirs(game_path, exist_ok=True)
dst, upload, meta = install_game(args.game_id, game_path, args.output)
try:
dst, upload, meta = install_game(args.game_id, game_path, args.output)
# Write game to config
config['games'][args.game_id] = {
'path': game_path,
'last_update': upload['updated_at'],
'url': meta['url'],
'title': meta['title'],
'description': meta['short_text']
}
# Write game to config
config['games'][args.game_id] = {
'path': game_path,
'last_update': upload['updated_at'],
'url': meta['url'],
'title': meta['title'],
'description': meta['short_text']
}
print('FYI: As itch.io does not have any information on how to execute the game, you have to set the executable manually on first run with the --executable option')
print('FYI: As itch.io does not have any information on how to execute the game, you have to set the executable manually on first run with the --executable option')
except handlers.HandlerException as err:
raise SystemExit(str(err))
def parser_run(args):
global config