2022-05-15 02:02:45 +02:00
|
|
|
import logging
|
2023-08-19 16:07:38 +02:00
|
|
|
from typing import Dict, List, Optional, Tuple
|
2022-05-15 02:02:45 +02:00
|
|
|
|
|
|
|
from .api import ItchApiClient
|
|
|
|
|
|
|
|
|
2023-08-19 16:07:38 +02:00
|
|
|
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
|
|
|
|
|
2022-05-15 02:02:45 +02:00
|
|
|
logging.info("Fetching all download keys...")
|
2022-06-12 19:31:25 +02:00
|
|
|
download_keys: Dict[int, str] = {}
|
2023-08-19 16:07:38 +02:00
|
|
|
game_urls: List[str] = []
|
2022-05-15 02:02:45 +02:00
|
|
|
page = 1
|
|
|
|
|
|
|
|
while True:
|
|
|
|
logging.info(f"Downloading page {page} (found {len(download_keys)} keys total)")
|
|
|
|
r = client.get("/profile/owned-keys", data={"page": page}, timeout=15)
|
|
|
|
if not r.ok:
|
|
|
|
break
|
|
|
|
|
|
|
|
data = r.json()
|
|
|
|
if 'owned_keys' not in data:
|
|
|
|
break # Assuming we're out of keys already...
|
|
|
|
|
|
|
|
for key in data['owned_keys']:
|
|
|
|
download_keys[key['game_id']] = key['id']
|
2023-08-19 16:07:38 +02:00
|
|
|
game_urls.append(key['game']['url'])
|
2022-05-15 02:02:45 +02:00
|
|
|
|
|
|
|
if len(data['owned_keys']) == data['per_page']:
|
|
|
|
page += 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
logging.info(f"Fetched {len(download_keys)} download keys.")
|
2023-08-19 16:07:38 +02:00
|
|
|
|
|
|
|
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)
|
2022-05-15 02:02:45 +02:00
|
|
|
return download_keys
|
2023-08-19 16:07:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_owned_games(client: ItchApiClient) -> List[str]:
|
|
|
|
(_, game_urls) = get_owned_keys(client)
|
|
|
|
return game_urls
|