From c93b5ddb0c43d26b4748f06b73d1b2a0d3b2ae9a Mon Sep 17 00:00:00 2001 From: Peter Taylor Date: Wed, 18 May 2022 13:44:49 +0100 Subject: [PATCH] #55 merges (dos2Unix) Co-authored-by: Blaze Marshall --- itchiodl/downloader/__main__.py | 2 +- itchiodl/game.py | 28 ++++++++++++++++++++++------ itchiodl/library.py | 16 +++++++++++++--- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/itchiodl/downloader/__main__.py b/itchiodl/downloader/__main__.py index 15cde04..70e2223 100644 --- a/itchiodl/downloader/__main__.py +++ b/itchiodl/downloader/__main__.py @@ -52,7 +52,7 @@ def main(): lib = itchiodl.Library(l, args.jobs) if args.download_publisher: - lib.load_game(args.download_publisher) + lib.load_games(args.download_publisher) elif args.download_game: matches = re.match(r"https://(.+)\.itch\.io/(.+)", args.download_game) lib.load_game(matches.group(1), matches.group(2)) diff --git a/itchiodl/game.py b/itchiodl/game.py index 314932d..bbf3e9a 100644 --- a/itchiodl/game.py +++ b/itchiodl/game.py @@ -18,9 +18,13 @@ class Game: self.name = self.data["title"] self.publisher = self.data["user"]["username"] self.link = self.data["url"] - - self.id = data["id"] - self.game_id = data["game_id"] + if "game_id" in self.data: + self.id = self.data["id"] + self.game_id = self.data["game_id"] + else: + self.id = False + self.game_id = self.data["id"] + matches = re.match(r"https://(.+)\.itch\.io/(.+)", self.link) self.game_slug = matches.group(2) @@ -32,10 +36,16 @@ class Game: def load_downloads(self, token): """Load all downloads for this game""" self.downloads = [] - r = requests.get( + if self.id: + r = requests.get( f"https://api.itch.io/games/{self.game_id}/uploads?download_key_id={self.id}", headers={"Authorization": token}, ) + else: + r = requests.get( + f"https://api.itch.io/games/{self.game_id}/uploads", + headers={"Authorization": token}, + ) j = r.json() for d in j["uploads"]: self.downloads.append(d) @@ -129,10 +139,16 @@ class Game: j = r.json() # Download - url = ( + if self.id: + url = ( f"https://api.itch.io/uploads/{d['id']}/" + f"download?api_key={token}&download_key_id={self.id}&uuid={j['uuid']}" - ) + ) + else: + url = ( + f"https://api.itch.io/uploads/{d['id']}/" + + f"download?api_key={token}&uuid={j['uuid']}" + ) # response_code = urllib.request.urlopen(url).getcode() try: itchiodl.utils.download(url, path, self.name, file) diff --git a/itchiodl/library.py b/itchiodl/library.py index e91b290..d9ac40c 100644 --- a/itchiodl/library.py +++ b/itchiodl/library.py @@ -44,14 +44,24 @@ class Library: headers={"Authorization": self.login}, ) j = json.loads(rsp.text) - self.games.append(Game(j["id"])) + game_id = j["id"] + gsp = requests.get( + f"https://api.itch.io/games/{game_id}", + headers={"Authorization": self.login}, + ) + self.games.append(Game(json.loads(gsp.text))) def load_games(self, publisher): """Load all games by publisher""" rsp = requests.get(f"https://{publisher}.itch.io") soup = BeautifulSoup(rsp.text, "html.parser") - for link in soup.select("a.game-link"): - self.games.append(Game(link.get("data-label").split(":")[1])) + for link in soup.select("a.game_link"): + game_id = link.get("data-label").split(":")[1] + gsp = requests.get( + f"https://api.itch.io/games/{game_id}", + headers={"Authorization": self.login}, + ) + self.games.append(Game(json.loads(gsp.text))) def download_library(self, platform=None): """Download all games in the library"""