From 406af545d2ce7a458ada5804773cb0f7cad456a9 Mon Sep 17 00:00:00 2001 From: Peter Taylor Date: Thu, 24 Feb 2022 14:51:55 +0000 Subject: [PATCH] Fixes By MatthewThomasRussell (edits by me) (#4) * added print to url * added some more prints * added error handling for non 200 response codes and will report these errors * update error handling * forgot to actually write string to file :( * update to readme for new error logging feature * Changed file writing to use with ... as notation Co-authored-by: Matthew Russell --- Readme.md | 17 +++++++++++++++++ src/itchio/game.py | 24 ++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index a643b87..98d2142 100644 --- a/Readme.md +++ b/Readme.md @@ -22,3 +22,20 @@ python -m itchio.downloader ``` This is a bit of a bodge, but it works. It essentially goes through and clicks the "Download" link on every item on the bundle's page, which adds it to your itchio library. It does not download any files. You will need the download page's URL (this will be in the bundle's email, and possibly your purchase history). It will not work with 2FA, and I'm unlikely to be able to fix it without making it far more complicated + + +## Errors +if a download fails it will be reported in ```errors.txt``` in the same directory as your downloads + +An example of which could look something like this: +```Cannot download game/asset: +Publisher Name: +Output File: // +Request URL: +Request Response Code: 404 +Error Reason: Not Found +This game/asset has been skipped please download manually +--------------------------------------------------------- +``` + +This is not a perfect solution but does prevent the whole process from crashing \ No newline at end of file diff --git a/src/itchio/game.py b/src/itchio/game.py index 617ec98..14a7c8b 100644 --- a/src/itchio/game.py +++ b/src/itchio/game.py @@ -2,6 +2,9 @@ import re import requests import json import os +import urllib + + import itchio.utils @@ -53,9 +56,26 @@ class Game: j = r.json() # Download - url = f"https://api.itch.io/uploads/{d['id']}/download?api_key={token}&download_key_id={self.id}&uuid={j['uuid']}" - itchio.utils.download_url(url, outfile, self.name +" - "+file) + # response_code = urllib.request.urlopen(url).getcode() + try: + itchio.utils.download_url(url, outfile, self.name +" - "+file) + except urllib.error.HTTPError as e: + print("This one has broken!!") + + with open('errors.txt', 'a') as f: + f.write(f""" Cannot download game/asset: {self.game_slug} + Publisher Name: {self.publisher_slug} + Output File: {outfile} + Request URL: {url} + Request Response Code: {e.code} + Error Reason: {e.reason} + This game/asset has been skipped please download manually + ---------------------------------------------------------\n """) + + continue + + with open(f"{self.publisher_slug}/{self.game_slug}.json", "w") as f: json.dump({