itch-dl/itch_dl/keys.py

58 lines
1.5 KiB
Python
Raw Normal View History

import logging
from typing import Dict, List, Tuple
from .api import ItchApiClient
KEYS_CACHED: bool = False
DOWNLOAD_KEYS: Dict[int, str] = {}
GAME_URLS: List[str] = []
def load_keys_and_urls(client: ItchApiClient) -> None:
global KEYS_CACHED # noqa: PLW0603 (whatever, I'll move all this to a class one day)
logging.info("Fetching all download keys...")
page = 1
while True:
logging.info("Downloading page %d (found %d keys total)", page, len(DOWNLOAD_KEYS))
r = client.get("/profile/owned-keys", data={"page": page}, timeout=15)
if not r.ok:
break
data = r.json()
2024-03-17 01:17:19 +01:00
if "owned_keys" not in data:
break # Assuming we're out of keys already...
2024-03-17 01:17:19 +01:00
for key in data["owned_keys"]:
DOWNLOAD_KEYS[key["game_id"]] = key["id"]
GAME_URLS.append(key["game"]["url"])
2024-03-17 01:17:19 +01:00
if len(data["owned_keys"]) == data["per_page"]:
page += 1
else:
break
logging.info("Fetched %d download keys.", len(DOWNLOAD_KEYS))
KEYS_CACHED = True
def get_owned_keys(client: ItchApiClient) -> Tuple[Dict[int, str], List[str]]:
if not KEYS_CACHED:
load_keys_and_urls(client)
return DOWNLOAD_KEYS, GAME_URLS
def get_download_keys(client: ItchApiClient) -> Dict[int, str]:
if not KEYS_CACHED:
load_keys_and_urls(client)
return DOWNLOAD_KEYS
def get_owned_games(client: ItchApiClient) -> List[str]:
if not KEYS_CACHED:
load_keys_and_urls(client)
return GAME_URLS