From 0dbd109d8c7cfa6de2465e44e58c10c7d6823e78 Mon Sep 17 00:00:00 2001 From: Uthman Fatih Date: Thu, 8 Oct 2026 22:45:00 +0100 Subject: [PATCH] Refactor config loading and improve cache handling in getMediaListCollection --- main.py | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index b10a1aa..327829d 100644 --- a/main.py +++ b/main.py @@ -4,20 +4,37 @@ import tomllib from os import path import json -TTL = 2 * 60 * 60 # 2 hours - if path.exists("config.toml"): with open("config.toml", "rb") as f: - data = tomllib.load(f) + config = tomllib.load(f) else: raise RuntimeError("config.toml not found") +TTL = config["anilist"].get("TTL", 2 * 60 * 60) # 2 hour default if not specificed + def getMediaListCollection( - mediaType=data["anilist"]["mediaType"], - userId=data["anilist"]["userId"], + mediaType=config["anilist"]["mediaType"], + userId=config["anilist"]["userId"], *, cachePath="data.json" ): + """ + Returns a tuple of (entries, cached) + + Entries is a dictionary of the values to be written to the cache file, such as: + - timestamp + - ttl + - params supplied + - response + + cached is a boolean indicating whether the cache file was used or not + + Takes the following arguments: + - mediaType: the media type to use for the request (if not supplied, defaults to value supplied from config.toml) + - userId: the user id to use for the request (if not supplied, defaults to value supplied from config.toml) + - cachePath: the path to the cache file (if not supplied, defaults to "data.json") + """ + if not mediaType or not userId: raise RuntimeError("MediaType and UserId must be set") @@ -45,15 +62,14 @@ def getMediaListCollection( if not cacheExpired and not parametersChanged: print("Cache is fresh, using cached data...") - return entries["response"] + return (entries["response"], True) if cacheExpired: print("Cache has gone stale, refreshing...") elif parametersChanged: print("Cache parameters changed, refreshing...") - - else: - print("Cache not found, refreshing...") + else: + print("Cache is invalid, refreshing...") # Fetch fresh data from AniList query = """ @@ -112,8 +128,7 @@ def getMediaListCollection( collection = data["data"]["MediaListCollection"] for lst in collection["lists"]: - for entry in lst["entries"]: - entries[entry["id"]] = entry + entries.setdefault(lst["name"], []).extend(lst["entries"]) if not collection["hasNextChunk"]: break @@ -127,13 +142,14 @@ def getMediaListCollection( "mediaType": mediaType, "userId": userId, }, - "response": list(entries.values()), + "response": entries } - return form + return (form, False) if __name__ == "__main__": - entries = getMediaListCollection() + entries, cached = getMediaListCollection() - with open("data.json", "w") as f: - f.write(f"{json.dumps(entries, indent=2)}") \ No newline at end of file + if not cached: + with open("data.json", "w") as f: + f.write(f"{json.dumps(entries, indent=2)}") \ No newline at end of file