cookies.py
1.46 KB
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
"""Cookie 文件持久化,对应 Go cookies/cookies.go。"""
from __future__ import annotations
import os
from pathlib import Path
def get_cookies_file_path(account: str = "") -> str:
"""获取 cookies 文件路径。
优先级:
1. /tmp/cookies.json(向后兼容)
2. COOKIES_PATH 环境变量
3. 多账号模式:~/.xhs/accounts/{account}/cookies.json
4. ./cookies.json(本地调试)
"""
if account:
account_dir = Path.home() / ".xhs" / "accounts" / account
account_dir.mkdir(parents=True, exist_ok=True)
return str(account_dir / "cookies.json")
# 旧路径
import tempfile
old_path = os.path.join(tempfile.gettempdir(), "cookies.json")
if os.path.exists(old_path):
return old_path
# 环境变量
env_path = os.getenv("COOKIES_PATH")
if env_path:
return env_path
return "cookies.json"
def load_cookies(path: str) -> bytes | None:
"""从文件加载 cookies。"""
try:
with open(path, "rb") as f:
return f.read()
except FileNotFoundError:
return None
def save_cookies(path: str, data: bytes) -> None:
"""保存 cookies 到文件。"""
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
with open(path, "wb") as f:
f.write(data)
def delete_cookies(path: str) -> None:
"""删除 cookies 文件。"""
import contextlib
with contextlib.suppress(FileNotFoundError):
os.remove(path)