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
|
from os import path
|
||||||
import json
|
import json
|
||||||
|
|
||||||
TTL = 2 * 60 * 60 # 2 hours
|
|
||||||
|
|
||||||
if path.exists("config.toml"):
|
if path.exists("config.toml"):
|
||||||
with open("config.toml", "rb") as f:
|
with open("config.toml", "rb") as f:
|
||||||
data = tomllib.load(f)
|
config = tomllib.load(f)
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("config.toml not found")
|
raise RuntimeError("config.toml not found")
|
||||||
|
|
||||||
|
TTL = config["anilist"].get("TTL", 2 * 60 * 60) # 2 hour default if not specificed
|
||||||
|
|
||||||
def getMediaListCollection(
|
def getMediaListCollection(
|
||||||
mediaType=data["anilist"]["mediaType"],
|
mediaType=config["anilist"]["mediaType"],
|
||||||
userId=data["anilist"]["userId"],
|
userId=config["anilist"]["userId"],
|
||||||
*,
|
*,
|
||||||
cachePath="data.json"
|
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:
|
if not mediaType or not userId:
|
||||||
raise RuntimeError("MediaType and UserId must be set")
|
raise RuntimeError("MediaType and UserId must be set")
|
||||||
|
|
||||||
@ -45,15 +62,14 @@ def getMediaListCollection(
|
|||||||
|
|
||||||
if not cacheExpired and not parametersChanged:
|
if not cacheExpired and not parametersChanged:
|
||||||
print("Cache is fresh, using cached data...")
|
print("Cache is fresh, using cached data...")
|
||||||
return entries["response"]
|
return (entries["response"], True)
|
||||||
|
|
||||||
if cacheExpired:
|
if cacheExpired:
|
||||||
print("Cache has gone stale, refreshing...")
|
print("Cache has gone stale, refreshing...")
|
||||||
elif parametersChanged:
|
elif parametersChanged:
|
||||||
print("Cache parameters changed, refreshing...")
|
print("Cache parameters changed, refreshing...")
|
||||||
|
else:
|
||||||
else:
|
print("Cache is invalid, refreshing...")
|
||||||
print("Cache not found, refreshing...")
|
|
||||||
|
|
||||||
# Fetch fresh data from AniList
|
# Fetch fresh data from AniList
|
||||||
query = """
|
query = """
|
||||||
@ -112,8 +128,7 @@ def getMediaListCollection(
|
|||||||
collection = data["data"]["MediaListCollection"]
|
collection = data["data"]["MediaListCollection"]
|
||||||
|
|
||||||
for lst in collection["lists"]:
|
for lst in collection["lists"]:
|
||||||
for entry in lst["entries"]:
|
entries.setdefault(lst["name"], []).extend(lst["entries"])
|
||||||
entries[entry["id"]] = entry
|
|
||||||
|
|
||||||
if not collection["hasNextChunk"]:
|
if not collection["hasNextChunk"]:
|
||||||
break
|
break
|
||||||
@ -127,13 +142,14 @@ def getMediaListCollection(
|
|||||||
"mediaType": mediaType,
|
"mediaType": mediaType,
|
||||||
"userId": userId,
|
"userId": userId,
|
||||||
},
|
},
|
||||||
"response": list(entries.values()),
|
"response": entries
|
||||||
}
|
}
|
||||||
|
|
||||||
return form
|
return (form, False)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
entries = getMediaListCollection()
|
entries, cached = getMediaListCollection()
|
||||||
|
|
||||||
with open("data.json", "w") as f:
|
if not cached:
|
||||||
f.write(f"{json.dumps(entries, indent=2)}")
|
with open("data.json", "w") as f:
|
||||||
|
f.write(f"{json.dumps(entries, indent=2)}")
|
||||||
Loading…
Reference in New Issue
Block a user