From 57310c8c9931d79645fca67900336002f8d202d5 Mon Sep 17 00:00:00 2001 From: "Jeremie J. Jarosh" Date: Sat, 15 Apr 2023 10:49:19 -0500 Subject: [PATCH 1/3] Pass the "Human Folders" Option Through the `Library` Class To remove the need for the `Game` class to parse `sys.argv`, we'll store the option in the `Library` class and pass it to `Game` as needed. The `ArgumentParser.add_argument()` call has been updated to show up as an 'on/off' argument. Also fix the naming convention. According to Python's PEP 8 Style Guide, function and variable names should be lowercase with words separated by underscores (aka snake_case). The rest of this project seems to follow PEP 8, so `humanFolders` should be `human_folders`. --- itchiodl/downloader/__main__.py | 12 ++++++------ itchiodl/game.py | 11 +++-------- itchiodl/library.py | 9 +++++---- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/itchiodl/downloader/__main__.py b/itchiodl/downloader/__main__.py index 164f9bf..8814692 100644 --- a/itchiodl/downloader/__main__.py +++ b/itchiodl/downloader/__main__.py @@ -20,11 +20,11 @@ def main(): parser.add_argument( "--human-folders", - type=bool, - default=False, - const=True, - nargs="?", - help="Download Folders are named based on the full text version of the title instead of the trimmed URL title", + action="store_true", + help=( + "Download Folders are named based on the full text version of the title instead of " + "the trimmed URL title" + ), ) parser.add_argument( @@ -58,7 +58,7 @@ def main(): else: l = args.api_key - lib = itchiodl.Library(l, args.jobs) + lib = itchiodl.Library(l, jobs=args.jobs, human_folders=args.human_folders) if args.download_publisher: lib.load_games(args.download_publisher) diff --git a/itchiodl/game.py b/itchiodl/game.py index feaa1d6..1dbfb44 100644 --- a/itchiodl/game.py +++ b/itchiodl/game.py @@ -3,7 +3,6 @@ import urllib import datetime from pathlib import Path -from sys import argv import requests from itchiodl import utils @@ -12,12 +11,8 @@ class Game: """Representation of a game download""" - def __init__(self, data): - self.args = argv[1:] - if "--human-folders" in self.args: - self.humanFolders = True - else: - self.humanFolders = False + def __init__(self, data, human_folders: bool): + self.human_folders = human_folders self.data = data["game"] self.name = self.data["title"] @@ -32,7 +27,7 @@ def __init__(self, data): matches = re.match(r"https://(.+)\.itch\.io/(.+)", self.link) self.game_slug = matches.group(2) - if self.humanFolders: + if self.human_folders: self.game_slug = utils.clean_path(self.data["title"]) self.publisher_slug = self.data.get("user").get("display_name") # This Branch covers the case that the user has diff --git a/itchiodl/library.py b/itchiodl/library.py index 1132735..e0b4688 100644 --- a/itchiodl/library.py +++ b/itchiodl/library.py @@ -12,10 +12,11 @@ class Library: """Representation of a user's game library""" - def __init__(self, login, jobs=4): + def __init__(self, login, jobs=4, human_folders=False): self.login = login self.games = [] self.jobs = jobs + self.human_folders = human_folders def load_game_page(self, page): """Load a page of games via the API""" @@ -27,7 +28,7 @@ def load_game_page(self, page): j = json.loads(r.text) for s in j["owned_keys"]: - self.games.append(Game(s)) + self.games.append(Game(s, self.human_folders)) return len(j["owned_keys"]) @@ -54,7 +55,7 @@ def load_game(self, publisher, title): ) k = gsp.json() if k != {"uploads": {}}: - self.games.append(Game(k)) + self.games.append(Game(k, self.human_folders)) return print(f"{title} is a purchased game.") i = 1 @@ -85,7 +86,7 @@ def load_games(self, publisher): headers={"Authorization": self.login}, ) k = json.loads(gsp.text) - self.games.append(Game(k)) + self.games.append(Game(k, self.human_folders)) def download_library(self, platform=None): """Download all games in the library""" From 2b441abbc8e37675ba087241dd609dd65b9f5599 Mon Sep 17 00:00:00 2001 From: "Jeremie J. Jarosh" Date: Sat, 15 Apr 2023 11:36:55 -0500 Subject: [PATCH 2/3] Allow the download directory to be changed --- itchiodl/downloader/__main__.py | 14 +++++++++++++- itchiodl/game.py | 5 +++-- itchiodl/library.py | 9 +++++---- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/itchiodl/downloader/__main__.py b/itchiodl/downloader/__main__.py index 8814692..61de5e5 100644 --- a/itchiodl/downloader/__main__.py +++ b/itchiodl/downloader/__main__.py @@ -8,6 +8,16 @@ def main(): parser = argparse.ArgumentParser(prog="python -m hstp", description="Build an ") + parser.add_argument( + "-o", + "--output-dir", + type=str, + default=".", + help=( + "The location to store the downloaded files, defaults to the current working directory" + ), + ) + parser.add_argument( "-k", "--api-key", help="Use API key instead of username/password" ) @@ -58,7 +68,9 @@ def main(): else: l = args.api_key - lib = itchiodl.Library(l, jobs=args.jobs, human_folders=args.human_folders) + lib = itchiodl.Library( + l, jobs=args.jobs, human_folders=args.human_folders, output_dir=args.output_dir + ) if args.download_publisher: lib.load_games(args.download_publisher) diff --git a/itchiodl/game.py b/itchiodl/game.py index 1dbfb44..fb811e6 100644 --- a/itchiodl/game.py +++ b/itchiodl/game.py @@ -11,8 +11,9 @@ class Game: """Representation of a game download""" - def __init__(self, data, human_folders: bool): + def __init__(self, data, human_folders: bool, output_dir: str): self.human_folders = human_folders + self.output_path = Path(output_dir) self.data = data["game"] self.name = self.data["title"] @@ -40,7 +41,7 @@ def __init__(self, data, human_folders: bool): self.files = [] self.downloads = [] self.dir = ( - Path(".") + self.output_path / utils.clean_path(self.publisher_slug) / utils.clean_path(self.game_slug) ) diff --git a/itchiodl/library.py b/itchiodl/library.py index e0b4688..db83e00 100644 --- a/itchiodl/library.py +++ b/itchiodl/library.py @@ -12,11 +12,12 @@ class Library: """Representation of a user's game library""" - def __init__(self, login, jobs=4, human_folders=False): + def __init__(self, login, jobs=4, human_folders=False, output_dir="."): self.login = login self.games = [] self.jobs = jobs self.human_folders = human_folders + self.output_dir = output_dir def load_game_page(self, page): """Load a page of games via the API""" @@ -28,7 +29,7 @@ def load_game_page(self, page): j = json.loads(r.text) for s in j["owned_keys"]: - self.games.append(Game(s, self.human_folders)) + self.games.append(Game(s, self.human_folders, self.output_dir)) return len(j["owned_keys"]) @@ -55,7 +56,7 @@ def load_game(self, publisher, title): ) k = gsp.json() if k != {"uploads": {}}: - self.games.append(Game(k, self.human_folders)) + self.games.append(Game(k, self.human_folders, self.output_dir)) return print(f"{title} is a purchased game.") i = 1 @@ -86,7 +87,7 @@ def load_games(self, publisher): headers={"Authorization": self.login}, ) k = json.loads(gsp.text) - self.games.append(Game(k, self.human_folders)) + self.games.append(Game(k, self.human_folders, self.output_dir)) def download_library(self, platform=None): """Download all games in the library""" From 4e3c1db6a7d7521000dfb5a451bfe031bbee1f8a Mon Sep 17 00:00:00 2001 From: "Jeremie J. Jarosh" Date: Sat, 15 Apr 2023 11:45:30 -0500 Subject: [PATCH 3/3] Store `Game` options in a single variable --- itchiodl/library.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/itchiodl/library.py b/itchiodl/library.py index db83e00..80aa022 100644 --- a/itchiodl/library.py +++ b/itchiodl/library.py @@ -16,8 +16,10 @@ def __init__(self, login, jobs=4, human_folders=False, output_dir="."): self.login = login self.games = [] self.jobs = jobs - self.human_folders = human_folders - self.output_dir = output_dir + self.game_kwargs = { + "human_folders": human_folders, + "output_dir": output_dir, + } def load_game_page(self, page): """Load a page of games via the API""" @@ -29,7 +31,7 @@ def load_game_page(self, page): j = json.loads(r.text) for s in j["owned_keys"]: - self.games.append(Game(s, self.human_folders, self.output_dir)) + self.games.append(Game(s, **self.game_kwargs)) return len(j["owned_keys"]) @@ -56,7 +58,7 @@ def load_game(self, publisher, title): ) k = gsp.json() if k != {"uploads": {}}: - self.games.append(Game(k, self.human_folders, self.output_dir)) + self.games.append(Game(k, **self.game_kwargs)) return print(f"{title} is a purchased game.") i = 1 @@ -87,7 +89,7 @@ def load_games(self, publisher): headers={"Authorization": self.login}, ) k = json.loads(gsp.text) - self.games.append(Game(k, self.human_folders, self.output_dir)) + self.games.append(Game(k, **self.game_kwargs)) def download_library(self, platform=None): """Download all games in the library"""