Finalise caching

This commit is contained in:
Uthman Fatih 2026-10-08 22:24:04 +01:00
parent 68603d09f2
commit d77acb3dc0
2 changed files with 84 additions and 23 deletions

View File

@ -1,6 +1,6 @@
[anilist] [anilist]
user_id = 8283179 userId = 8283179
media_type = "ANIME" mediaType = "ANIME"
[ical] [ical]
# reserved for future use # reserved for future use

103
main.py
View File

@ -12,63 +12,124 @@ if path.exists("config.toml"):
else: else:
raise RuntimeError("config.toml not found") raise RuntimeError("config.toml not found")
def getMediaListCollection(type=data["anilist"]["mediaType"], userId=data["anilist"]["userId"]): def getMediaListCollection(
if not type or not userId: mediaType=data["anilist"]["mediaType"],
userId=data["anilist"]["userId"],
*,
cachePath="data.json"
):
if not mediaType or not userId:
raise RuntimeError("MediaType and UserId must be set") raise RuntimeError("MediaType and UserId must be set")
if path.exists(cachePath):
with open(cachePath, "r") as f:
entries = json.load(f)
if (
"timestamp" in entries
and "ttl" in entries
and "response" in entries
and "params" in entries
):
timestamp = float(entries["timestamp"])
ttl = float(entries["ttl"])
cacheUserId = entries["params"].get("userId")
cacheMediaType = entries["params"].get("mediaType")
cacheExpired = time.time() - timestamp > ttl
parametersChanged = (
cacheUserId != userId
or cacheMediaType != mediaType
)
if not cacheExpired and not parametersChanged:
print("Cache is fresh, using cached data...")
return entries["response"]
if cacheExpired:
print("Cache has gone stale, refreshing...")
elif parametersChanged:
print("Cache parameters changed, refreshing...")
else:
print("Cache not found, refreshing...")
# Fetch fresh data from AniList
query = """ query = """
query ($type: MediaType!, $userId: Int!, $chunk: Int, $perChunk: Int) { query ($type: MediaType!, $userId: Int!, $chunk: Int, $perChunk: Int) {
MediaListCollection(type: $type, userId: $userId, chunk: $chunk, perChunk: $perChunk) { MediaListCollection(
hasNextChunk type: $type,
lists { userId: $userId,
name chunk: $chunk,
entries { perChunk: $perChunk
id ) {
media { hasNextChunk
id lists {
title { name
english entries {
romaji id
} media {
id
title {
english
romaji
}
}
}
} }
} }
}
}
} }
""" """
entries = {} # keyed by entry id, dedupes custom-list repeats entries = {}
chunk = 1 chunk = 1
while True: while True:
variables = { 'type': type, 'userId': int(userId), 'chunk': chunk, 'perChunk': 500, } variables = {
"type": mediaType,
"userId": int(userId),
"chunk": chunk,
"perChunk": 500,
}
resp = requests.post( resp = requests.post(
"https://graphql.anilist.co", "https://graphql.anilist.co",
json={"query": query, "variables": variables}, json={
"query": query,
"variables": variables,
},
timeout=30, timeout=30,
) )
resp.raise_for_status() resp.raise_for_status()
data = resp.json() data = resp.json()
if "errors" in data: if "errors" in data:
raise RuntimeError(data["errors"]) raise RuntimeError(data["errors"])
collection = data["data"]["MediaListCollection"] collection = data["data"]["MediaListCollection"]
for lst in collection["lists"]: for lst in collection["lists"]:
for entry in lst["entries"]: for entry in lst["entries"]:
entries[entry["id"]] = entry entries[entry["id"]] = entry
if not collection["hasNextChunk"]: if not collection["hasNextChunk"]:
break break
chunk += 1 chunk += 1
form = { form = {
"timestamp": str(time.time()), "timestamp": str(time.time()),
"ttl": str(TTL), "ttl": str(TTL),
"params": {
"mediaType": mediaType,
"userId": userId,
},
"response": list(entries.values()), "response": list(entries.values()),
} }
return form return form
if __name__ == "__main__": if __name__ == "__main__":