2022-05-15 02:02:45 +02:00
|
|
|
import logging
|
2025-01-31 23:40:40 +01:00
|
|
|
from typing import Dict, List, Tuple
|
2022-05-15 02:02:45 +02:00
|
|
|
|
|
|
|
from .api import ItchApiClient
|
|
|
|
|
2025-01-31 23:40:40 +01:00
|
|
|
KEYS_CACHED: bool = False
|
|
|
|
DOWNLOAD_KEYS: Dict[int, str] = {}
|
|
|
|
GAME_URLS: List[str] = []
|
2022-05-15 02:02:45 +02:00
|
|
|
|
2023-08-19 09:07:38 -05:00
|
|
|
|
2025-01-31 23:40:40 +01:00
|
|
|
def load_keys_and_urls(client: ItchApiClient) -> None:
|
|
|
|
global KEYS_CACHED # noqa: PLW0603 (whatever, I'll move all this to a class one day)
|
2022-05-15 02:02:45 +02:00
|
|
|
logging.info("Fetching all download keys...")
|
|
|
|
page = 1
|
|
|
|
|
|
|
|
while True:
|
2025-01-31 23:40:40 +01:00
|
|
|
logging.info("Downloading page %d (found %d keys total)", page, len(DOWNLOAD_KEYS))
|
2022-05-15 02:02:45 +02:00
|
|
|
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:
|
2022-05-15 02:02:45 +02:00
|
|
|
break # Assuming we're out of keys already...
|
|
|
|
|
2024-03-17 01:17:19 +01:00
|
|
|
for key in data["owned_keys"]:
|
2025-01-31 23:40:40 +01:00
|
|
|
DOWNLOAD_KEYS[key["game_id"]] = key["id"]
|
|
|
|
GAME_URLS.append(key["game"]["url"])
|
2022-05-15 02:02:45 +02:00
|
|
|
|
2024-03-17 01:17:19 +01:00
|
|
|
if len(data["owned_keys"]) == data["per_page"]:
|
2022-05-15 02:02:45 +02:00
|
|
|
page += 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2025-01-31 23:40:40 +01:00
|
|
|
logging.info("Fetched %d download keys.", len(DOWNLOAD_KEYS))
|
|
|
|
KEYS_CACHED = True
|
|
|
|
|
2023-08-19 09:07:38 -05:00
|
|
|
|
2025-01-31 23:40:40 +01:00
|
|
|
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
|
2023-08-19 09:07:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_download_keys(client: ItchApiClient) -> Dict[int, str]:
|
2025-01-31 23:40:40 +01:00
|
|
|
if not KEYS_CACHED:
|
|
|
|
load_keys_and_urls(client)
|
|
|
|
|
|
|
|
return DOWNLOAD_KEYS
|
2023-08-19 09:07:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_owned_games(client: ItchApiClient) -> List[str]:
|
2025-01-31 23:40:40 +01:00
|
|
|
if not KEYS_CACHED:
|
|
|
|
load_keys_and_urls(client)
|
|
|
|
|
|
|
|
return GAME_URLS
|