Refactor config loading and improve cache handling in getMediaListCollection
This commit is contained in:
parent
d77acb3dc0
commit
0dbd109d8c
48
main.py
48
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)}")
|
||||
if not cached:
|
||||
with open("data.json", "w") as f:
|
||||
f.write(f"{json.dumps(entries, indent=2)}")
|
||||
Loading…
Reference in New Issue
Block a user