Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the download directory to be different from the current working directory #83

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions itchiodl/downloader/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -20,11 +30,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(
Expand Down Expand Up @@ -58,7 +68,9 @@ 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, output_dir=args.output_dir
)

if args.download_publisher:
lib.load_games(args.download_publisher)
Expand Down
14 changes: 5 additions & 9 deletions itchiodl/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import urllib
import datetime
from pathlib import Path
from sys import argv
import requests

from itchiodl import utils
Expand All @@ -12,12 +11,9 @@
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, output_dir: str):
self.human_folders = human_folders
self.output_path = Path(output_dir)

self.data = data["game"]
self.name = self.data["title"]
Expand All @@ -32,7 +28,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
Expand All @@ -45,7 +41,7 @@ def __init__(self, data):
self.files = []
self.downloads = []
self.dir = (
Path(".")
self.output_path
/ utils.clean_path(self.publisher_slug)
/ utils.clean_path(self.game_slug)
)
Expand Down
12 changes: 8 additions & 4 deletions itchiodl/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
class Library:
"""Representation of a user's game library"""

def __init__(self, login, jobs=4):
def __init__(self, login, jobs=4, human_folders=False, output_dir="."):
self.login = login
self.games = []
self.jobs = jobs
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"""
Expand All @@ -27,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.games.append(Game(s, **self.game_kwargs))

return len(j["owned_keys"])

Expand All @@ -54,7 +58,7 @@ def load_game(self, publisher, title):
)
k = gsp.json()
if k != {"uploads": {}}:
self.games.append(Game(k))
self.games.append(Game(k, **self.game_kwargs))
return
print(f"{title} is a purchased game.")
i = 1
Expand Down Expand Up @@ -85,7 +89,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.game_kwargs))

def download_library(self, platform=None):
"""Download all games in the library"""
Expand Down