From be5baca3775eed81e32b14575b065a9ba78e0723 Mon Sep 17 00:00:00 2001 From: Peter Taylor Date: Thu, 5 May 2022 14:43:54 +0100 Subject: [PATCH] Added individual game downloader --- itchiodl/downloader/__main__.py | 23 ++++++++++++++++++++++- itchiodl/library.py | 18 +++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/itchiodl/downloader/__main__.py b/itchiodl/downloader/__main__.py index aada99d..15cde04 100644 --- a/itchiodl/downloader/__main__.py +++ b/itchiodl/downloader/__main__.py @@ -1,5 +1,6 @@ import argparse from getpass import getpass +import re import itchiodl @@ -25,6 +26,18 @@ def main(): help="Number of concurrent downloads, defaults to 4", ) + parser.add_argument( + "--download-publisher", + type=str, + help="Download all games from a specific publisher", + ) + + parser.add_argument( + "--download-game", + type=str, + help="Download a specific game, should be in the format publisher.itch.io/game", + ) + args = parser.parse_args() l = "" @@ -37,7 +50,15 @@ def main(): l = args.api_key lib = itchiodl.Library(l, args.jobs) - lib.load_games() + + if args.download_publisher: + lib.load_game(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)) + else: + lib.load_owned_games() + lib.download_library(args.platform) diff --git a/itchiodl/library.py b/itchiodl/library.py index f563901..d2563c8 100644 --- a/itchiodl/library.py +++ b/itchiodl/library.py @@ -1,6 +1,7 @@ import json from concurrent.futures import ThreadPoolExecutor import requests +from bs4 import BeautifulSoup from itchiodl.game import Game @@ -27,7 +28,7 @@ class Library: return len(j["owned_keys"]) - def load_games(self): + def load_owned_games(self): """Load all games in the library via the API""" page = 1 while True: @@ -36,6 +37,21 @@ class Library: break page += 1 + def load_game(self, publisher, title): + """Load a game by publisher and title""" + requests.get( + f"https://{publisher}.itch.io/{title}/data.json", + headers={"Authorization": self.login}, + ) + self.games.append(Game(publisher, title)) + + 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])) + def download_library(self, platform=None): """Download all games in the library"""