diff --git a/itch_dl/handlers.py b/itch_dl/handlers.py index e45faa1..f7034e8 100644 --- a/itch_dl/handlers.py +++ b/itch_dl/handlers.py @@ -10,6 +10,7 @@ from .api import ItchApiClient from .utils import ItchDownloadError, get_int_after_marker_in_json from .consts import ITCH_BASE, ITCH_URL, ITCH_BROWSER_TYPES from .config import Settings +from .keys import get_owned_games def get_jobs_for_game_jam_json(game_jam_json: dict) -> List[str]: @@ -132,6 +133,9 @@ def get_jobs_for_itch_url(url: str, client: ItchApiClient) -> List[str]: raise ValueError("itch-dl expects a username in profile links.") + elif site == "my-purchases": # User Purchased Games + return get_owned_games(client) + # Something else? raise NotImplementedError(f"itch-dl does not understand \"{site}\" URLs. Please file a new issue.") diff --git a/itch_dl/keys.py b/itch_dl/keys.py index b2087d4..27d4340 100644 --- a/itch_dl/keys.py +++ b/itch_dl/keys.py @@ -1,12 +1,20 @@ import logging -from typing import Dict +from typing import Dict, List, Optional, Tuple from .api import ItchApiClient -def get_download_keys(client: ItchApiClient) -> Dict[int, str]: +cached_owned_keys: Optional[Tuple[Dict[int, str], List[str]]] = None + + +def get_owned_keys(client: ItchApiClient) -> Tuple[Dict[int, str], List[str]]: + global cached_owned_keys + if cached_owned_keys is not None: + return cached_owned_keys + logging.info("Fetching all download keys...") download_keys: Dict[int, str] = {} + game_urls: List[str] = [] page = 1 while True: @@ -21,6 +29,7 @@ def get_download_keys(client: ItchApiClient) -> Dict[int, str]: for key in data['owned_keys']: download_keys[key['game_id']] = key['id'] + game_urls.append(key['game']['url']) if len(data['owned_keys']) == data['per_page']: page += 1 @@ -28,4 +37,16 @@ def get_download_keys(client: ItchApiClient) -> Dict[int, str]: break logging.info(f"Fetched {len(download_keys)} download keys.") + + cached_owned_keys = (download_keys, game_urls) + return cached_owned_keys + + +def get_download_keys(client: ItchApiClient) -> Dict[int, str]: + (download_keys, _) = get_owned_keys(client) return download_keys + + +def get_owned_games(client: ItchApiClient) -> List[str]: + (_, game_urls) = get_owned_keys(client) + return game_urls \ No newline at end of file