Load cookie from file with mtime-based caching.
()
| 46 | |
| 47 | |
| 48 | def load_cookie() -> tuple: |
| 49 | """Load cookie from file with mtime-based caching.""" |
| 50 | cookie_file = CONFIG.get("cookie_file") |
| 51 | if not cookie_file or not os.path.exists(cookie_file): |
| 52 | return "", None |
| 53 | try: |
| 54 | mtime = os.path.getmtime(cookie_file) |
| 55 | if mtime == _cookie_cache["mtime"] and _cookie_cache["str"]: |
| 56 | return _cookie_cache["str"], _cookie_cache["sapisid"] |
| 57 | with open(cookie_file, "r") as f: |
| 58 | content = f.read().strip() |
| 59 | if content.startswith("{"): |
| 60 | data = json.loads(content) |
| 61 | cookie_str = data.get("cookie", "") |
| 62 | sapisid = data.get("sapisid", "") |
| 63 | else: |
| 64 | cookie_str = content |
| 65 | pairs = dict(p.split("=", 1) for p in cookie_str.split("; ") if "=" in p) |
| 66 | sapisid = pairs.get("SAPISID", "") |
| 67 | _cookie_cache.update({"str": cookie_str, "sapisid": sapisid or None, "mtime": mtime}) |
| 68 | return cookie_str, sapisid if sapisid else None |
| 69 | except Exception as e: |
| 70 | log(f"Cookie load error: {e}") |
| 71 | return _cookie_cache["str"], _cookie_cache["sapisid"] |
| 72 | |
| 73 | |
| 74 | def make_sapisidhash(sapisid: str) -> str: |
no test coverage detected