forked from Mirrors/itch-dl
Enable lots of extra Ruff checks
Warns about various small code smells and odd issues we can catch early. Nothing here should change the program behavior directly.
This commit is contained in:
@@ -1,25 +1,20 @@
|
||||
import logging
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
from typing import Dict, List, Tuple
|
||||
|
||||
from .api import ItchApiClient
|
||||
|
||||
|
||||
cached_owned_keys: Optional[Tuple[Dict[int, str], List[str]]] = None
|
||||
KEYS_CACHED: bool = False
|
||||
DOWNLOAD_KEYS: Dict[int, str] = {}
|
||||
GAME_URLS: List[str] = []
|
||||
|
||||
|
||||
def get_owned_keys(client: ItchApiClient) -> Tuple[Dict[int, str], List[str]]:
|
||||
global cached_owned_keys
|
||||
if cached_owned_keys is not None:
|
||||
logging.debug(f"Fetched {len(cached_owned_keys[0])} download keys from cache.")
|
||||
return cached_owned_keys
|
||||
|
||||
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...")
|
||||
download_keys: Dict[int, str] = {}
|
||||
game_urls: List[str] = []
|
||||
page = 1
|
||||
|
||||
while True:
|
||||
logging.info(f"Downloading page {page} (found {len(download_keys)} keys total)")
|
||||
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
|
||||
@@ -29,25 +24,34 @@ def get_owned_keys(client: ItchApiClient) -> Tuple[Dict[int, str], List[str]]:
|
||||
break # Assuming we're out of keys already...
|
||||
|
||||
for key in data["owned_keys"]:
|
||||
download_keys[key["game_id"]] = key["id"]
|
||||
game_urls.append(key["game"]["url"])
|
||||
DOWNLOAD_KEYS[key["game_id"]] = key["id"]
|
||||
GAME_URLS.append(key["game"]["url"])
|
||||
|
||||
if len(data["owned_keys"]) == data["per_page"]:
|
||||
page += 1
|
||||
else:
|
||||
break
|
||||
|
||||
logging.info(f"Fetched {len(download_keys)} download keys.")
|
||||
logging.info("Fetched %d download keys.", len(DOWNLOAD_KEYS))
|
||||
KEYS_CACHED = True
|
||||
|
||||
cached_owned_keys = (download_keys, game_urls)
|
||||
return cached_owned_keys
|
||||
|
||||
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]:
|
||||
(download_keys, _) = get_owned_keys(client)
|
||||
return download_keys
|
||||
if not KEYS_CACHED:
|
||||
load_keys_and_urls(client)
|
||||
|
||||
return DOWNLOAD_KEYS
|
||||
|
||||
|
||||
def get_owned_games(client: ItchApiClient) -> List[str]:
|
||||
(_, game_urls) = get_owned_keys(client)
|
||||
return game_urls
|
||||
if not KEYS_CACHED:
|
||||
load_keys_and_urls(client)
|
||||
|
||||
return GAME_URLS
|
||||
|
||||
Reference in New Issue
Block a user