forked from Mirrors/itch-dl
Add a Settings system
Allows permanently configuring itch-dl with an API key and other things in the future. Adds a new dependency, Pydantic, to validate the config.
This commit is contained in:
@@ -4,6 +4,7 @@ import argparse
|
||||
|
||||
from .handlers import get_jobs_for_url_or_path
|
||||
from .downloader import drive_downloads
|
||||
from .config import Settings, load_config
|
||||
from .keys import get_download_keys
|
||||
from .api import ItchApiClient
|
||||
|
||||
@@ -11,12 +12,14 @@ logging.basicConfig()
|
||||
logging.getLogger().setLevel(logging.INFO)
|
||||
|
||||
|
||||
def parse_args():
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(description="Bulk download stuff from Itch.io.")
|
||||
parser.add_argument("url_or_path",
|
||||
help="itch.io URL or path to a game jam entries.json file")
|
||||
parser.add_argument("--api-key", metavar="key", required=True,
|
||||
parser.add_argument("--api-key", metavar="key", default=None,
|
||||
help="itch.io API key - https://itch.io/user/settings/api-keys")
|
||||
parser.add_argument("--profile", metavar="profile", default=None,
|
||||
help="configuration profile to load")
|
||||
parser.add_argument("--urls-only", action="store_true",
|
||||
help="print scraped game URLs without downloading them")
|
||||
parser.add_argument("--download-to", metavar="path",
|
||||
@@ -30,18 +33,36 @@ def parse_args():
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def apply_args_on_settings(args: argparse.Namespace, settings: Settings):
|
||||
if args.api_key:
|
||||
settings.api_key = args.api_key
|
||||
|
||||
|
||||
def run() -> int:
|
||||
args = parse_args()
|
||||
if args.verbose:
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
|
||||
jobs = get_jobs_for_url_or_path(args.url_or_path, args.api_key)
|
||||
settings = load_config(profile=args.profile)
|
||||
apply_args_on_settings(args, settings)
|
||||
|
||||
if not settings.api_key:
|
||||
exit("You did not provide an API key which itch-dl requires.\n"
|
||||
"See https://github.com/DragoonAethis/itch-dl/wiki/API-Keys for more info.")
|
||||
|
||||
# Check API key validity:
|
||||
client = ItchApiClient(settings.api_key, settings.user_agent)
|
||||
profile_req = client.get("/profile")
|
||||
if not profile_req.ok:
|
||||
exit(f"Provided API key appears to be invalid: {profile_req.text}\n"
|
||||
"See https://github.com/DragoonAethis/itch-dl/wiki/API-Keys for more info.")
|
||||
|
||||
jobs = get_jobs_for_url_or_path(args.url_or_path, settings)
|
||||
jobs = list(set(jobs)) # Deduplicate, just in case...
|
||||
logging.info(f"Found {len(jobs)} URL(s).")
|
||||
|
||||
if len(jobs) == 0:
|
||||
print("No URLs to download.")
|
||||
return 1
|
||||
exit("No URLs to download.")
|
||||
|
||||
if args.urls_only:
|
||||
for job in jobs:
|
||||
@@ -54,15 +75,7 @@ def run() -> int:
|
||||
download_to = os.path.normpath(args.download_to)
|
||||
os.makedirs(download_to, exist_ok=True)
|
||||
|
||||
client = ItchApiClient(args.api_key)
|
||||
|
||||
# Check API key validity:
|
||||
profile_req = client.get("/profile")
|
||||
if not profile_req.ok:
|
||||
print(f"Provided API key appears to be invalid: {profile_req.text}")
|
||||
exit(1)
|
||||
|
||||
# Grab all the download keys (there's no way to fetch them per title...):
|
||||
keys = get_download_keys(client)
|
||||
|
||||
return drive_downloads(jobs, download_to, args.mirror_web, args.api_key, keys, parallel=args.parallel)
|
||||
return drive_downloads(jobs, download_to, args.mirror_web, settings, keys, parallel=args.parallel)
|
||||
|
||||
Reference in New Issue
Block a user