-
Notifications
You must be signed in to change notification settings - Fork 8
/
caching.py
97 lines (72 loc) · 2.23 KB
/
caching.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Sticker caching functionality used by the downloader."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, Callable
from loguru import logger
from requests_cache.session import CachedSession
# requests_cache
cachedSession = CachedSession(
".cache/tstickers.requests.sqlite",
backend="sqlite",
expire_after=60 * 60 * 12,
allowable_codes=(200,),
allowable_methods=("GET", "POST"),
)
CACHE_DIR = Path(".cache")
if not CACHE_DIR.exists():
CACHE_DIR.mkdir()
def verify_converted(pack_name: str) -> bool:
"""Verify the cache for a packName eg. "DonutTheDog". Uses the cache "version"
to call the verify function for that version.
Args:
----
pack_name (str): name of the sticker pack eg. "DonutTheDog"
Returns:
-------
bool: if the converted cache has been verified
"""
cache = CACHE_DIR / pack_name
if cache.exists():
data = json.loads(cache.read_text(encoding="utf-8"))
verify_func = _get_verify_function(data.get("version", 1))
if verify_func(data):
logger.info(f"-> Cache hit for {pack_name}!")
return True
logger.info(f"-> Cache miss for {pack_name}!")
return False
def _verify_converted_v1(data: dict[str, Any]) -> bool:
"""Verify the cache for a packName using cache data.
Args:
----
data (dict[Path, Any]): packName cache data to verify
Returns:
-------
bool: if the converted cache has been verified
"""
return (
len(list(Path(f"{data['info']['swd']}").glob("**/*"))) > 0
and data["converted"]["static"] + data["converted"]["animated"]
>= data["converted"]["total"]
)
def create_converted(pack_name: str, data: dict) -> None:
"""Write cache data to a file identified by packName.
Args:
----
pack_name (str): name of the sticker pack eg. "DonutTheDog"
data (dict): packName cache data to write to cache
"""
cache = CACHE_DIR / pack_name
cache.write_text(json.dumps(data), encoding="utf-8")
def _get_verify_function(version: int) -> Callable[[dict[str, Any]], bool]:
"""Get the appropriate cache verification function based on version.
Args:
----
version (int): Cache version
Returns:
-------
Callable[[dict[str, Any]], bool]: Cache verification function
"""
return {
1: _verify_converted_v1,
}.get(version, _verify_converted_v1)