first commit

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
kunkunxiaomaoandClaude committed 2026-11-16 20:45:22 +08:00
commit 9c5fef1a4d
412 files changed
+94582

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Sakura downloader application package."""
+1
View File
@@ -0,0 +1 @@
"""Core business logic for Sakura downloader."""
+167
View File
@@ -0,0 +1,167 @@
from __future__ import annotations
import html
import json
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from urllib.parse import urlencode
import requests
from pixiv_app.core.downloader import USER_AGENT, build_headers
LOGIN_PAGE_URL = "https://accounts.pixiv.net/login"
LOGIN_AJAX_URL = "https://accounts.pixiv.net/ajax/login?lang=zh"
VALIDATE_URL = "https://www.pixiv.net/setting_user.php"
@dataclass
class AuthResult:
success: bool
message: str
cookie: str = ""
requires_verification: bool = False
user_name: str = ""
user_id: str = ""
class SessionStore:
def __init__(self, path: str | Path = "pixiv_app_session.json") -> None:
self.path = Path(path)
def load(self) -> dict[str, Any]:
if not self.path.exists():
return {}
try:
return json.loads(self.path.read_text(encoding="utf-8"))
except Exception:
return {}
def save(self, payload: dict[str, Any]) -> None:
self.path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
class PixivAuthClient:
def __init__(self) -> None:
self.timeout = 20
def validate_cookie(self, cookie: str) -> AuthResult:
cookie = cookie.strip()
if not cookie:
return AuthResult(False, "Cookie 为空,请先填写或登录。")
session = requests.Session()
session.headers.update(build_headers(cookie=cookie, referer="https://www.pixiv.net/"))
try:
response = session.get(VALIDATE_URL, allow_redirects=True, timeout=self.timeout)
final_url = response.url
if "return_to=" in final_url or "accounts.pixiv.net" in final_url:
return AuthResult(False, "当前 Cookie 已失效或未登录,请更新会话。")
user_id, user_name = self._extract_user_info(response.text)
return AuthResult(True, "Cookie 有效,可直接开始抓取。", cookie=cookie, user_name=user_name, user_id=user_id)
except Exception as exc:
return AuthResult(False, f"Cookie 检测失败: {exc}")
def login_with_password(self, login_id: str, password: str) -> AuthResult:
login_id = login_id.strip()
password = password.strip()
if not login_id or not password:
return AuthResult(False, "账号和密码都不能为空。")
session = requests.Session()
session.headers.update(
{
"User-Agent": USER_AGENT,
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Origin": "https://accounts.pixiv.net",
"Referer": LOGIN_PAGE_URL,
"X-Requested-With": "XMLHttpRequest",
}
)
try:
login_page = session.get(LOGIN_PAGE_URL, timeout=self.timeout)
login_page.raise_for_status()
tt = self._extract_tt(login_page.text)
payload = {
"login_id": login_id,
"password": password,
"source": "accounts",
"app_ios": "0",
"ref": "",
"return_to": "https://www.pixiv.net/",
"g_recaptcha_response": "",
"recaptcha_enterprise_score_token": "",
}
headers = dict(session.headers)
if tt:
headers["X-CSRF-Token"] = tt
response = session.post(
LOGIN_AJAX_URL,
data=urlencode(payload),
headers={**headers, "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"},
timeout=self.timeout,
)
response.raise_for_status()
body = response.json()
if isinstance(body, dict) and body.get("body", {}).get("success"):
cookie = self._cookies_to_header(session.cookies)
validation = self.validate_cookie(cookie)
if validation.success:
validation.message = "账号密码登录成功,会话已可用。"
return validation
return AuthResult(True, "登录接口返回成功,但会话校验未完全通过,请手动检测。", cookie=cookie)
body_payload = body.get("body", {}) if isinstance(body, dict) else {}
errors = body_payload.get("errors", {}) if isinstance(body_payload, dict) else {}
if errors.get("recaptcha") or body_payload.get("requireExtraVerification") or body_payload.get("requireTwoFactorAuthentication"):
return AuthResult(
False,
"Pixiv 要求额外验证或二步验证,程序无法静默完成。请改用 Cookie 登录,或先在浏览器登录后导出 Cookie。",
requires_verification=True,
)
message = self._flatten_error_message(errors) or "账号密码登录失败,请检查账号、密码或改用 Cookie。"
return AuthResult(False, message)
except Exception as exc:
return AuthResult(False, f"账号密码登录失败: {exc}")
def _extract_tt(self, html_text: str) -> str:
match = re.search(r'"pixivAccount\.tt":"([^"]+)"', html_text)
return match.group(1) if match else ""
def _extract_user_info(self, html_text: str) -> tuple[str, str]:
user_id = ""
user_name = ""
patterns = [
r'"userData":\{"id":"(\d+)","pixivId":"[^"]*","name":"([^"]+)"',
r'"user_id":"(\d+)".*?"name":"([^"]+)"',
]
for pattern in patterns:
match = re.search(pattern, html_text)
if match:
user_id = match.group(1)
user_name = html.unescape(match.group(2))
break
return user_id, user_name
def _flatten_error_message(self, errors: dict[str, Any]) -> str:
messages: list[str] = []
for key, value in errors.items():
if isinstance(value, str) and value.strip():
messages.append(f"{key}: {value}")
elif isinstance(value, list):
text_items = [str(item).strip() for item in value if str(item).strip()]
if text_items:
messages.append(f"{key}: {'; '.join(text_items)}")
return " | ".join(messages)
def _cookies_to_header(self, jar: requests.cookies.RequestsCookieJar) -> str:
parts = [f"{cookie.name}={cookie.value}" for cookie in jar]
return "; ".join(parts)
+353
View File
@@ -0,0 +1,353 @@
"""
Playwright 浏览器代理池 - 支持动态换 IP
功能:
- 多代理轮换(轮询/随机/会话粘性)
- 健康检查(自动剔除失效代理)
- 延迟统计(加权选择最优代理)
- 线程安全
"""
import random
import threading
import time
from dataclasses import dataclass, field
from typing import Optional, List, Dict, Any
from enum import Enum
class RotationStrategy(Enum):
"""轮换策略"""
ROUND_ROBIN = "round_robin" # 轮询
RANDOM = "random" # 随机
WEIGHTED = "weighted" # 加权(按成功率+延迟)
SESSION_STICKY = "session_sticky" # 会话粘性(同会话同IP)
@dataclass
class ProxyInfo:
"""代理信息"""
server: str # http://ip:port 或 socks5://ip:port
username: str = ""
password: str = ""
# 统计数据
success_count: int = 0
fail_count: int = 0
total_latency_ms: float = 0.0
avg_latency_ms: float = 0.0
last_check_time: float = 0.0
last_fail_time: float = 0.0
# 状态
healthy: bool = True
cooldown_until: float = 0.0 # 冷却结束时间
@property
def proxy_url(self) -> str:
"""获取完整的代理URL(含认证)"""
if self.username and self.password:
# http://user:pass@ip:port
protocol = self.server.split("://")[0]
rest = self.server.split("://")[1]
return f"{protocol}://{self.username}:{self.password}@{rest}"
return self.server
def to_requests_proxies(self) -> dict[str, str]:
return {"http": self.proxy_url, "https": self.proxy_url}
def to_playwright_proxy(self) -> dict[str, str]:
data = {"server": self.server}
if self.username:
data["username"] = self.username
data["password"] = self.password
return data
@property
def success_rate(self) -> float:
"""成功率"""
total = self.success_count + self.fail_count
if total == 0:
return 1.0
return self.success_count / total
@property
def weight(self) -> float:
"""
计算代理权重(用于加权随机选择)
考虑因素:成功率、延迟、冷却状态
"""
if not self.healthy:
return 0.0
# 冷却中
if self.cooldown_until > time.time():
return 0.0
# 成功率权重
success_weight = self.success_rate
# 延迟权重(延迟越低权重越高)
if self.avg_latency_ms > 0:
latency_weight = max(0.1, 1000.0 / max(self.avg_latency_ms, 1))
else:
latency_weight = 1.0
# 失败惩罚
fail_penalty = 1.0 / (1.0 + self.fail_count * 0.5)
return success_weight * latency_weight * fail_penalty
class ProxyPool:
"""代理池 - 管理多个代理的获取和轮换"""
def __init__(
self,
strategy: RotationStrategy = RotationStrategy.WEIGHTED,
health_check_url: str = "https://httpbin.org/ip",
health_check_timeout: int = 8,
max_failures: int = 3,
cooldown_seconds: int = 60,
auto_health_check: bool = True,
):
"""
Args:
strategy: 轮换策略
health_check_url: 健康检查URL
health_check_timeout: 健康检查超时(秒)
max_failures: 最大失败次数,超过后标记为不健康
cooldown_seconds: 不健康代理的冷却时间(秒)
auto_health_check: 是否自动健康检查
"""
self.strategy = strategy
self.health_check_url = health_check_url
self.health_check_timeout = health_check_timeout
self.max_failures = max_failures
self.cooldown_seconds = cooldown_seconds
self.auto_health_check = auto_health_check
self._proxies: List[ProxyInfo] = []
self._session_proxy_map: Dict[str, ProxyInfo] = {} # 会话粘性用
self._round_robin_index = 0
self._lock = threading.Lock()
def add_proxy(self, server: str, username: str = "", password: str = "") -> None:
"""添加单个代理"""
with self._lock:
# 去重
existing = {p.server for p in self._proxies}
if server not in existing:
self._proxies.append(ProxyInfo(
server=server,
username=username,
password=password,
))
def add_proxy_url(self, value: str) -> None:
parsed = parse_proxy_url(value)
if parsed is not None:
self.add_proxy(parsed.server, parsed.username, parsed.password)
def add_proxies(self, proxies: List[dict]) -> None:
"""批量添加代理
Args:
proxies: [{"server": "http://1.2.3.4:8080", "username": "", "password": ""}, ...]
"""
for p in proxies:
self.add_proxy(
server=p["server"],
username=p.get("username", ""),
password=p.get("password", ""),
)
def add_proxy_text(self, text: str) -> int:
count = 0
for line in text.splitlines():
parsed = parse_proxy_url(line)
if parsed is None:
continue
self.add_proxy(parsed.server, parsed.username, parsed.password)
count += 1
return count
def remove_proxy(self, server: str) -> None:
"""移除代理"""
with self._lock:
self._proxies = [p for p in self._proxies if p.server != server]
def get_proxy(self, session_id: str = None) -> Optional[ProxyInfo]:
"""
获取一个代理
Args:
session_id: 会话ID,用于SESSION_STICKY策略
Returns:
代理信息,如果没有可用代理返回None
"""
with self._lock:
# 过滤出健康的代理
now = time.time()
healthy = [p for p in self._proxies if p.healthy and p.cooldown_until <= now]
if not healthy:
# 尝试从冷却中恢复
now = time.time()
recovered = []
for p in self._proxies:
if not p.healthy and p.cooldown_until <= now:
p.healthy = True
p.fail_count = 0
recovered.append(p)
if recovered:
healthy = recovered
if not healthy:
return None
# 根据策略选择代理
if self.strategy == RotationStrategy.RANDOM:
return random.choice(healthy)
elif self.strategy == RotationStrategy.WEIGHTED:
# 加权随机选择
weights = [p.weight for p in healthy]
total_weight = sum(weights)
if total_weight <= 0:
return random.choice(healthy)
r = random.uniform(0, total_weight)
cumsum = 0
for i, w in enumerate(weights):
cumsum += w
if r <= cumsum:
return healthy[i]
return healthy[0]
elif self.strategy == RotationStrategy.SESSION_STICKY:
if session_id and session_id in self._session_proxy_map:
proxy = self._session_proxy_map[session_id]
if proxy in healthy:
return proxy
# 新会话,分配一个代理
proxy = self._get_round_robin(healthy)
if session_id:
self._session_proxy_map[session_id] = proxy
return proxy
else: # ROUND_ROBIN
return self._get_round_robin(healthy)
def _get_round_robin(self, proxies: List[ProxyInfo]) -> ProxyInfo:
"""轮询获取代理"""
proxy = proxies[self._round_robin_index % len(proxies)]
self._round_robin_index += 1
return proxy
def mark_success(self, proxy: ProxyInfo, latency_ms: float = 0) -> None:
"""标记代理请求成功"""
if proxy is None:
return
with self._lock:
proxy.success_count += 1
proxy.fail_count = 0
proxy.healthy = True
if latency_ms > 0:
# 更新平均延迟(指数移动平均)
if proxy.avg_latency_ms == 0:
proxy.avg_latency_ms = latency_ms
else:
proxy.avg_latency_ms = proxy.avg_latency_ms * 0.7 + latency_ms * 0.3
proxy.last_check_time = time.time()
def mark_failed(self, proxy: ProxyInfo) -> None:
"""标记代理请求失败"""
if proxy is None:
return
with self._lock:
proxy.fail_count += 1
if proxy.fail_count >= self.max_failures:
proxy.healthy = False
proxy.cooldown_until = time.time() + self.cooldown_seconds
def health_check(self, proxy: ProxyInfo = None) -> bool:
"""
健康检查单个或所有代理
Returns:
如果检查通过返回True
"""
import requests
proxies_to_check = [proxy] if proxy else self._proxies
for p in proxies_to_check:
if p is None:
continue
try:
start = time.time()
resp = requests.get(
self.health_check_url,
proxies={"http": p.proxy_url, "https": p.proxy_url},
timeout=self.health_check_timeout,
)
if resp.status_code == 200:
latency_ms = (time.time() - start) * 1000
self.mark_success(p, latency_ms)
else:
self.mark_failed(p)
except Exception:
self.mark_failed(p)
return proxy.healthy if proxy else len(self.get_healthy_proxies()) > 0
def get_healthy_proxies(self) -> List[ProxyInfo]:
"""获取所有健康代理"""
with self._lock:
return [p for p in self._proxies if p.healthy]
def get_stats(self) -> dict:
"""获取代理池统计信息"""
with self._lock:
healthy = [p for p in self._proxies if p.healthy]
return {
"total": len(self._proxies),
"healthy": len(healthy),
"unhealthy": len(self._proxies) - len(healthy),
"proxies": [
{
"server": p.server,
"healthy": p.healthy,
"success_rate": p.success_rate,
"avg_latency_ms": round(p.avg_latency_ms, 2),
"fail_count": p.fail_count,
}
for p in self._proxies
]
}
def release_session(self, session_id: str) -> None:
"""释放会话绑定的代理"""
with self._lock:
self._session_proxy_map.pop(session_id, None)
def parse_proxy_url(value: str) -> Optional[ProxyInfo]:
text = value.strip()
if not text or text.startswith("#"):
return None
if "://" not in text:
text = "http://" + text
import re
match = re.match(r"^(https?|socks5)://(?:(?P<user>[^:@/]+):(?P<pwd>[^@/]+)@)?(?P<host>[\w.\-]+):(?P<port>\d{2,5})$", text, re.I)
if not match:
return None
scheme = text.split("://", 1)[0].lower()
host = match.group("host")
port = match.group("port")
user = match.group("user") or ""
pwd = match.group("pwd") or ""
return ProxyInfo(server=f"{scheme}://{host}:{port}", username=user, password=pwd)
+602
View File
@@ -0,0 +1,602 @@
from __future__ import annotations
import base64
import hashlib
import json
import logging
import threading
import time
from collections.abc import Callable
from dataclasses import asdict
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
import requests
from pixiv_app.core.paths import runtime_path
logger = logging.getLogger(__name__)
def file_sha256(path: str | Path) -> str:
digest = hashlib.sha256()
with Path(path).open("rb") as fp:
for chunk in iter(lambda: fp.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def persist_cloud_session_tokens(access_token: str, refresh_token: str = "") -> None:
"""将刷新后的 Token 写回桌面会话文件,供 GUI 与 Gallery API 共用。"""
from pixiv_app.core.auth import SessionStore
from pixiv_app.core.paths import app_session_file
store = SessionStore(app_session_file())
payload = store.load()
cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {}
cloud["access_token"] = access_token
if refresh_token:
cloud["refresh_token"] = refresh_token
payload["cloud_sync"] = cloud
store.save(payload)
def _decode_jwt_exp(token: str) -> float:
"""解码 JWT 的 exp 字段,返回 Unix 时间戳;解码失败返回 0。"""
if not token or "." not in token:
return 0.0
try:
payload = token.split(".")[1]
payload += "=" * (4 - len(payload) % 4)
decoded = json.loads(base64.urlsafe_b64decode(payload))
return float(decoded.get("exp", 0))
except Exception:
return 0.0
class CloudSession:
"""全局单例,统一管理云同步 Token 的读写、刷新与持久化。
所有需要访问 Sakura Cloud API 的模块(GUI、Gallery、FollowSync、XMedia)
都通过此单例获取配置和创建客户端,确保 token 刷新互不冲突、变更全局可见。
"""
_instance: CloudSession | None = None
_instance_lock = threading.Lock()
def __new__(cls) -> CloudSession:
if cls._instance is None:
with cls._instance_lock:
if cls._instance is None:
instance = super().__new__(cls)
instance._base_url = ""
instance._access_token = ""
instance._refresh_token = ""
instance._enabled = False
instance._refresh_lock = threading.Lock()
instance._listeners: list[Callable[[str, str], None]] = []
instance._last_refresh_time = 0.0
cls._instance = instance
return cls._instance
# ------------------------------------------------------------------
# 配置入口
# ------------------------------------------------------------------
def configure(
self,
*,
base_url: str = "",
access_token: str = "",
refresh_token: str = "",
enabled: bool = False,
) -> None:
"""GUI 启动 / 登录 / 保存会话时调用,同步最新 token 到全局单例。"""
self._base_url = (base_url or "").strip()
self._access_token = (access_token or "").strip()
self._refresh_token = (refresh_token or "").strip()
self._enabled = bool(enabled)
def load_from_disk(self) -> dict[str, Any]:
"""从磁盘会话文件读取云端配置并应用到单例,返回原始 payload。"""
from pixiv_app.core.auth import SessionStore
from pixiv_app.core.paths import app_session_file
store = SessionStore(app_session_file())
payload = store.load()
cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {}
self.configure(
base_url=str(cloud.get("base_url") or ""),
access_token=str(cloud.get("access_token") or ""),
refresh_token=str(cloud.get("refresh_token") or ""),
enabled=bool(cloud.get("enabled", False)),
)
return payload
@property
def enabled(self) -> bool:
return self._enabled and bool(self._base_url) and bool(self._access_token)
# ------------------------------------------------------------------
# Config 快照
# ------------------------------------------------------------------
def get_config(self) -> CloudSyncConfig:
"""返回当前 token 的 CloudSyncConfig 快照。
调用时机:每次创建 SakuraCloudClient 前。
若 access_token 即将过期(< 5 分钟),主动触发一次刷新。
"""
if self._access_token and self._is_expired_or_near(threshold_seconds=300):
self.try_refresh()
return CloudSyncConfig(
base_url=self._base_url,
access_token=self._access_token,
refresh_token=self._refresh_token,
)
# ------------------------------------------------------------------
# 监听者管理
# ------------------------------------------------------------------
def add_listener(self, callback: Callable[[str, str], None]) -> None:
"""注册 token 刷新回调(如 GUI 的 _on_cloud_token_refreshed)。"""
if callback not in self._listeners:
self._listeners.append(callback)
def remove_listener(self, callback: Callable[[str, str], None]) -> None:
try:
self._listeners.remove(callback)
except ValueError:
pass
def notify_listeners(self, access_token: str, refresh_token: str) -> None:
"""通知所有监听者 token 已更新(成功时为新的 token 对,失败时为空字符串)。"""
for callback in self._listeners:
try:
callback(access_token, refresh_token)
except Exception:
logger.exception("CloudSession listener %s failed", getattr(callback, "__name__", callback))
# ------------------------------------------------------------------
# Token 刷新(核心)
# ------------------------------------------------------------------
def try_refresh(self) -> bool:
"""统一刷新入口。使用全局锁防止并发重复刷新。
Returns:
True: 刷新成功,access_token 已更新
False: 刷新失败,token 已被清空,监听者已收到空 token 通知
"""
refresh = self._refresh_token
if not refresh:
return False
with self._refresh_lock:
# 二次检查:可能其他线程已刷新
refresh = self._refresh_token
if not refresh:
return False
# 距上次刷新不足 5 秒,跳过(防抖)
elapsed = time.time() - self._last_refresh_time
if elapsed < 5.0 and self._access_token:
return True
try:
resp = requests.post(
f"{self._base_url}/api/v1/auth/refresh",
json={"refresh_token": refresh},
timeout=30.0,
)
if resp.status_code != 200:
logger.warning(
"CloudSession refresh failed HTTP %s: %s",
resp.status_code,
resp.text[:200],
)
return False
body = resp.json()
data = body.get("data", body) if isinstance(body, dict) else {}
if not isinstance(data, dict):
self._on_refresh_failed()
return False
new_access = (data.get("access_token") or "").strip()
new_refresh = (data.get("refresh_token") or "").strip()
if not new_access:
self._on_refresh_failed()
return False
# 更新内存
self._access_token = new_access
if new_refresh:
self._refresh_token = new_refresh
self._last_refresh_time = time.time()
# 写盘
self._persist()
# 通知监听者
self.notify_listeners(self._access_token, self._refresh_token)
logger.info("CloudSession token refreshed successfully")
return True
except Exception:
logger.exception("CloudSession refresh error")
self._on_refresh_failed()
return False
def _on_refresh_failed(self) -> None:
"""刷新失败:清空 token + 写盘 + 通知监听者。"""
self._access_token = ""
self._refresh_token = ""
self._last_refresh_time = time.time()
self._persist()
self.notify_listeners("", "")
def _persist(self) -> None:
"""将当前 token 写入磁盘会话文件。异常记录日志但不抛出。"""
try:
persist_cloud_session_tokens(self._access_token, self._refresh_token)
except Exception:
logger.exception("CloudSession persist to disk failed")
# ------------------------------------------------------------------
# JWT 辅助
# ------------------------------------------------------------------
def _is_expired_or_near(self, threshold_seconds: int = 300) -> bool:
exp = _decode_jwt_exp(self._access_token)
if exp <= 0:
return False
return (exp - threshold_seconds) <= time.time()
@dataclass
class CloudSyncConfig:
base_url: str
access_token: str
refresh_token: str = ""
chunk_size: int = 5 * 1024 * 1024
timeout: float = 60.0
@dataclass
class SyncMetadata:
resource_type: str = "artwork"
source_platform: str = "pixiv"
source_id: str | None = None
content_type: str | None = None
title: str = ""
author: str | None = None
author_id: str | None = None
tags: list[str] = field(default_factory=list)
metadata: dict[str, Any] = field(default_factory=dict)
page_count: int = 1
thumbnail_url: str | None = None
description: str | None = None
word_count: int = 0
series_id: str | None = None
series_title: str | None = None
chapter_title: str | None = None
chapter_order: int = 0
class SyncFailureQueue:
def __init__(self, path: str | Path | None = None) -> None:
self.path = Path(path) if path else runtime_path("cloud_sync_failures.json")
def load(self) -> list[dict[str, Any]]:
if not self.path.is_file():
return []
try:
data = json.loads(self.path.read_text(encoding="utf-8"))
return data if isinstance(data, list) else []
except Exception:
return []
def save(self, items: list[dict[str, Any]]) -> None:
self.path.parent.mkdir(parents=True, exist_ok=True)
self.path.write_text(json.dumps(items, ensure_ascii=False, indent=2), encoding="utf-8")
def add(self, file_path: str | Path, metadata: SyncMetadata, mime_type: str | None, error: str) -> None:
now = time.time()
file_path = str(Path(file_path))
items = self.load()
key = (file_path, metadata.source_platform, metadata.source_id)
for item in items:
item_key = (
str(item.get("file_path") or ""),
str(item.get("metadata", {}).get("source_platform") or ""),
item.get("metadata", {}).get("source_id"),
)
if item_key == key:
attempts = int(item.get("attempts") or 0)
item.update(
{
"metadata": asdict(metadata),
"mime_type": mime_type,
"attempts": attempts,
"last_error": error,
"updated_at": now,
"next_retry_at": now + min(3600, 60 * (2 ** min(attempts, 5))),
}
)
self.save(items)
return
items.append(
{
"file_path": file_path,
"metadata": asdict(metadata),
"mime_type": mime_type,
"attempts": 0,
"last_error": error,
"created_at": now,
"updated_at": now,
"next_retry_at": now + 60,
}
)
self.save(items)
class SakuraCloudClient:
def __init__(
self,
config: CloudSyncConfig,
*,
on_token_refreshed: Callable[[str, str], None] | None = None,
cloud_session: CloudSession | None = None,
) -> None:
self.config = config
self.base_url = config.base_url.rstrip("/")
self.session = requests.Session()
self.session.headers.update({"Authorization": f"Bearer {config.access_token}"})
self._cloud_session = cloud_session # 全局单例引用
# 仅在没有 cloud_session 时使用独立锁和回调(兼容旧路径)
if cloud_session is None:
self._on_token_refreshed = on_token_refreshed or persist_cloud_session_tokens
self._refresh_lock = threading.Lock()
else:
self._on_token_refreshed = None
self._refresh_lock = None
def check_file(self, sha256: str, file_size: int) -> dict[str, Any]:
return self._post("/api/v1/sync/check", {"sha256": sha256, "file_size": file_size})
def init_upload(
self,
*,
sha256: str,
file_name: str,
file_size: int,
mime_type: str | None,
resource_type: str,
) -> dict[str, Any]:
return self._post(
"/api/v1/upload/init",
{
"sha256": sha256,
"file_name": file_name,
"file_size": file_size,
"mime_type": mime_type,
"resource_type": resource_type,
},
)
def upload_chunk(self, upload_id: str, chunk: bytes) -> dict[str, Any]:
response = self._request(
"POST",
"/api/v1/upload/chunk",
data={"upload_id": upload_id},
files={"chunk": ("chunk.bin", chunk)},
)
return self._unwrap(response)
def complete_upload(self, upload_id: str) -> dict[str, Any]:
return self._post("/api/v1/upload/complete", {"upload_id": upload_id})
def sync_metadata(self, file_id: str, metadata: SyncMetadata) -> dict[str, Any]:
payload = {
"file_id": file_id,
"resource_type": metadata.resource_type,
"source_platform": metadata.source_platform,
"source_id": metadata.source_id,
"content_type": metadata.content_type,
"title": metadata.title,
"author": metadata.author,
"author_id": metadata.author_id,
"tags": metadata.tags,
"metadata": metadata.metadata,
"page_count": metadata.page_count,
"thumbnail_url": metadata.thumbnail_url,
"description": metadata.description,
"word_count": metadata.word_count,
"series_id": metadata.series_id,
"series_title": metadata.series_title,
"chapter_title": metadata.chapter_title,
"chapter_order": metadata.chapter_order,
}
return self._post("/api/v1/sync/metadata", payload)
def upsert_follow(self, payload: dict[str, Any]) -> dict[str, Any]:
return self._post("/api/v1/follows", payload)
def bulk_upsert_follows(self, items: list[dict[str, Any]]) -> dict[str, Any]:
return self._post("/api/v1/follows/bulk-upsert", {"items": items})
def create_alert(self, payload: dict[str, Any]) -> dict[str, Any]:
return self._post("/api/v1/follows/alerts", payload)
def _request(
self,
method: str,
path: str,
*,
json: dict[str, Any] | None = None,
data: dict[str, Any] | None = None,
files: dict[str, Any] | None = None,
_retry_on_401: bool = True,
) -> requests.Response:
url = f"{self.base_url}{path}"
response = self.session.request(
method,
url,
json=json,
data=data,
files=files,
timeout=self.config.timeout,
)
if response.status_code == 401 and _retry_on_401 and self._try_refresh_token():
response = self.session.request(
method,
url,
json=json,
data=data,
files=files,
timeout=self.config.timeout,
)
return response
def _post(self, path: str, payload: dict[str, Any], *, _retry_on_401: bool = True) -> dict[str, Any]:
response = self._request("POST", path, json=payload, _retry_on_401=_retry_on_401)
return self._unwrap(response)
def _try_refresh_token(self) -> bool:
# --- 新路径:委托给全局 CloudSession ---
if self._cloud_session is not None:
ok = self._cloud_session.try_refresh()
if ok:
# 刷新成功后同步更新本地 config 和 session headers
new_config = self._cloud_session.get_config()
self.config.access_token = new_config.access_token
self.config.refresh_token = new_config.refresh_token
self.session.headers.update({"Authorization": f"Bearer {new_config.access_token}"})
return ok
# --- 旧路径(向后兼容,无 cloud_session 时使用) ---
refresh = self.config.refresh_token.strip()
if not refresh:
return False
with self._refresh_lock:
refresh = self.config.refresh_token.strip()
if not refresh:
return False
try:
resp = requests.post(
f"{self.base_url}/api/v1/auth/refresh",
json={"refresh_token": refresh},
timeout=self.config.timeout,
)
if resp.status_code != 200:
return False
body = resp.json()
data = body.get("data", body) if isinstance(body, dict) else {}
if not isinstance(data, dict):
return False
new_access = (data.get("access_token") or "").strip()
new_refresh = (data.get("refresh_token") or "").strip()
if not new_access:
return False
self.config.access_token = new_access
self.session.headers.update({"Authorization": f"Bearer {new_access}"})
if new_refresh:
self.config.refresh_token = new_refresh
try:
self._on_token_refreshed(self.config.access_token, self.config.refresh_token)
except Exception:
logger.exception("SakuraCloudClient._on_token_refreshed failed")
return True
except Exception:
logger.exception("SakuraCloudClient._try_refresh_token error")
return False
@staticmethod
def _unwrap(response: requests.Response) -> dict[str, Any]:
response.raise_for_status()
body = response.json()
if isinstance(body, dict) and "data" in body:
return body["data"]
return body
class SyncManager:
def __init__(self, client: SakuraCloudClient, failure_queue: SyncFailureQueue | None = None) -> None:
self.client = client
self.failure_queue = failure_queue or SyncFailureQueue()
def sync_file(self, file_path: str | Path, metadata: SyncMetadata, *, mime_type: str | None = None) -> dict[str, Any]:
try:
return self._sync_file_once(file_path, metadata, mime_type=mime_type)
except Exception as exc:
self.failure_queue.add(file_path, metadata, mime_type, str(exc))
raise
def _sync_file_once(self, file_path: str | Path, metadata: SyncMetadata, *, mime_type: str | None = None) -> dict[str, Any]:
path = Path(file_path)
sha256 = file_sha256(path)
file_size = path.stat().st_size
checked = self.client.check_file(sha256, file_size)
file_id = checked.get("file_id") if checked.get("owned_by_user") else None
if not file_id:
init = self.client.init_upload(
sha256=sha256,
file_name=path.name,
file_size=file_size,
mime_type=mime_type,
resource_type=self._upload_resource_type(metadata, mime_type),
)
if init.get("exists") and init.get("file_id"):
file_id = str(init["file_id"])
else:
upload_id = str(init["upload_id"])
with path.open("rb") as fp:
for chunk in iter(lambda: fp.read(self.client.config.chunk_size), b""):
self.client.upload_chunk(upload_id, chunk)
completed = self.client.complete_upload(upload_id)
file_id = str(completed["file_id"])
return self.client.sync_metadata(str(file_id), metadata)
@staticmethod
def _upload_resource_type(metadata: SyncMetadata, mime_type: str | None) -> str:
if metadata.resource_type == "novel":
return "novel"
if (mime_type or "").lower().startswith("video/") or (metadata.content_type or "").lower() == "video":
return "video"
return "image"
def retry_failed(self, *, limit: int = 5) -> dict[str, int]:
now = time.time()
items = self.failure_queue.load()
kept: list[dict[str, Any]] = []
retried = 0
succeeded = 0
failed = 0
for item in items:
if retried >= limit or float(item.get("next_retry_at") or 0) > now:
kept.append(item)
continue
path = Path(str(item.get("file_path") or ""))
if not path.is_file():
failed += 1
continue
try:
metadata = SyncMetadata(**dict(item.get("metadata") or {}))
self._sync_file_once(path, metadata, mime_type=item.get("mime_type"))
succeeded += 1
except Exception as exc:
attempts = int(item.get("attempts") or 0) + 1
item["attempts"] = attempts
item["last_error"] = str(exc)
item["updated_at"] = now
item["next_retry_at"] = now + min(3600, 60 * (2 ** min(attempts, 5)))
kept.append(item)
failed += 1
retried += 1
self.failure_queue.save(kept)
return {"retried": retried, "succeeded": succeeded, "failed": failed, "remaining": len(kept)}
+175
View File
@@ -0,0 +1,175 @@
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
Cookie = dict[str, Any]
def parse_cookie_text(text: str) -> list[Cookie]:
"""Parse JSON cookie exports or Netscape cookies.txt content."""
text = text.strip()
if not text:
raise ValueError("Cookie 文件为空。")
try:
payload = json.loads(text)
except json.JSONDecodeError:
payload = None
if payload is not None:
raw_items = payload.get("cookies", payload) if isinstance(payload, dict) else payload
if not isinstance(raw_items, list):
raise ValueError("Cookie JSON 必须是数组,或包含 cookies 数组。")
cookies = [_normalize_json_cookie(item) for item in raw_items if isinstance(item, dict)]
else:
cookies = _parse_netscape_cookie_text(text)
cookies = [item for item in cookies if item.get("name") and item.get("value") is not None]
if not cookies:
raise ValueError("没有解析到有效 Cookie。")
return cookies
def filter_cookies_for_domains(cookies: list[Cookie], domains: tuple[str, ...]) -> list[Cookie]:
result: list[Cookie] = []
for cookie in cookies:
domain = str(cookie.get("domain", "")).lower().lstrip(".")
if any(domain == item or domain.endswith("." + item) for item in domains):
result.append(cookie)
return result
def cookie_domains(cookies: list[Cookie]) -> list[str]:
domains = sorted({str(item.get("domain", "")).lower().lstrip(".") for item in cookies if item.get("domain")})
return [item for item in domains if item]
def has_cookie_domain(cookies: list[Cookie], domains: tuple[str, ...]) -> bool:
return bool(filter_cookies_for_domains(cookies, domains))
def cookies_to_header(cookies: list[Cookie], domains: tuple[str, ...] | None = None) -> str:
selected = filter_cookies_for_domains(cookies, domains) if domains else cookies
pairs: list[str] = []
seen: set[str] = set()
for cookie in selected:
name = str(cookie.get("name", "")).strip()
value = str(cookie.get("value", ""))
if not name or name in seen:
continue
seen.add(name)
pairs.append(f"{name}={value}")
if not pairs:
raise ValueError("没有找到匹配站点的 Cookie。")
return "; ".join(pairs)
def cookies_to_playwright(cookies: list[Cookie], domains: tuple[str, ...] | None = None) -> list[Cookie]:
selected = filter_cookies_for_domains(cookies, domains) if domains else cookies
output_by_key: dict[tuple[str, str, str], Cookie] = {}
for cookie in selected:
name = str(cookie.get("name") or "").strip()
domain = str(cookie.get("domain") or "").strip()
if not name or not domain:
continue
item: Cookie = {
"name": name,
"value": str(cookie.get("value", "")),
"domain": domain,
"path": str(cookie.get("path") or "/"),
"secure": bool(cookie.get("secure", False)),
"httpOnly": bool(cookie.get("httpOnly", False)),
}
expires = cookie.get("expires")
if expires is not None:
try:
expires_value = int(float(expires))
if expires_value > 0:
item["expires"] = expires_value
except (TypeError, ValueError):
pass
same_site = str(cookie.get("sameSite", "") or "").strip()
if same_site in {"Strict", "Lax", "None"}:
item["sameSite"] = same_site
key = (str(item["domain"]).lower(), str(item["path"]), name)
output_by_key[key] = item
output = list(output_by_key.values())
if not output:
raise ValueError("没有找到匹配站点的 Cookie。")
return output
def save_playwright_cookies(cookies: list[Cookie], path: str | Path) -> Path:
target = Path(path)
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(json.dumps(cookies, ensure_ascii=False, indent=2), encoding="utf-8")
return target
def cookie_summary(cookies: list[Cookie]) -> str:
domains = sorted({str(item.get("domain", "")).lstrip(".") or "-" for item in cookies})
shown = ", ".join(domains[:5])
suffix = "..." if len(domains) > 5 else ""
return f"{len(cookies)} 个 Cookie;域名:{shown}{suffix}"
def _parse_netscape_cookie_text(text: str) -> list[Cookie]:
cookies: list[Cookie] = []
for raw_line in text.splitlines():
line = raw_line.strip()
if not line or line.startswith("#HttpOnly_"):
http_only = line.startswith("#HttpOnly_")
line = line.removeprefix("#HttpOnly_")
elif line.startswith("#"):
continue
else:
http_only = False
parts = line.split("\t")
if len(parts) < 7:
parts = line.split()
if len(parts) < 7:
continue
domain, _include_subdomains, path, secure, expires, name = parts[:6]
value = "\t".join(parts[6:])
cookies.append(
{
"domain": domain,
"path": path or "/",
"secure": secure.upper() == "TRUE",
"expires": int(expires) if str(expires).isdigit() else -1,
"name": name,
"value": value,
"httpOnly": http_only,
"sameSite": "Lax",
}
)
return cookies
def _normalize_json_cookie(item: Cookie) -> Cookie:
expires = item.get("expires", item.get("expirationDate", item.get("expiry")))
same_site = _normalize_same_site(item.get("sameSite"))
return {
"domain": str(item.get("domain") or item.get("host") or ""),
"path": str(item.get("path") or "/"),
"secure": bool(item.get("secure", False)),
"expires": expires,
"name": str(item.get("name") or ""),
"value": str(item.get("value") or ""),
"httpOnly": bool(item.get("httpOnly", item.get("httponly", False))),
"sameSite": same_site,
}
def _normalize_same_site(value: Any) -> str:
text = str(value or "").strip().lower()
if text in {"strict", "samesitestrict"}:
return "Strict"
if text in {"none", "no_restriction", "samesitenone"}:
return "None"
return "Lax"
File diff suppressed because it is too large. Load diff
+159
View File
@@ -0,0 +1,159 @@
from __future__ import annotations
import copy
import os
import random
import threading
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import Any
class DeviceType(Enum):
WINDOWS_CHROME = "windows_chrome"
WINDOWS_EDGE = "windows_edge"
MAC_CHROME = "mac_chrome"
@dataclass
class BrowserProfile:
"""Stable browser context settings for one logical session.
This module intentionally avoids Canvas/WebGL spoofing or large fingerprint rotation.
The goal is consistency inside a session, not impersonating many different users.
"""
user_agent: str
platform: str = "Win32"
viewport: dict[str, int] = field(default_factory=lambda: {"width": 1920, "height": 1080})
locale: str = "zh-CN"
timezone_id: str = "Asia/Shanghai"
color_scheme: str = "light"
reduced_motion: str = "no-preference"
device_scale_factor: float = 1.0
is_mobile: bool = False
has_touch: bool = False
def to_playwright_context_options(self) -> dict[str, Any]:
return {
"user_agent": self.user_agent,
"viewport": dict(self.viewport),
"locale": self.locale,
"timezone_id": self.timezone_id,
"color_scheme": self.color_scheme,
"reduced_motion": self.reduced_motion,
"device_scale_factor": self.device_scale_factor,
"is_mobile": self.is_mobile,
"has_touch": self.has_touch,
}
def headers(self) -> dict[str, str]:
return {
"User-Agent": self.user_agent,
"Accept-Language": f"{self.locale},zh;q=0.9,en;q=0.8",
}
class BrowserProfileDatabase:
@staticmethod
def windows_chrome() -> BrowserProfile:
return BrowserProfile(
user_agent=real_browser_user_agent()
or "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
platform="Win32",
viewport={"width": 1920, "height": 1080},
)
@staticmethod
def windows_edge() -> BrowserProfile:
return BrowserProfile(
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0",
platform="Win32",
viewport={"width": 1536, "height": 864},
device_scale_factor=1.25,
)
@staticmethod
def mac_chrome() -> BrowserProfile:
return BrowserProfile(
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
platform="MacIntel",
viewport={"width": 1512, "height": 982},
device_scale_factor=2.0,
)
@staticmethod
def all_desktop() -> list[BrowserProfile]:
return [
BrowserProfileDatabase.windows_chrome(),
BrowserProfileDatabase.windows_edge(),
BrowserProfileDatabase.mac_chrome(),
]
@staticmethod
def by_device_type(device_type: DeviceType) -> BrowserProfile:
mapping = {
DeviceType.WINDOWS_CHROME: BrowserProfileDatabase.windows_chrome,
DeviceType.WINDOWS_EDGE: BrowserProfileDatabase.windows_edge,
DeviceType.MAC_CHROME: BrowserProfileDatabase.mac_chrome,
}
return mapping.get(device_type, BrowserProfileDatabase.windows_chrome)()
class SessionFingerprintManager:
"""Session-sticky browser profile manager."""
def __init__(self, profiles: list[BrowserProfile] | None = None, *, strategy: str = "sticky") -> None:
self.profiles = profiles or BrowserProfileDatabase.all_desktop()
self.strategy = strategy
self._session_map: dict[str, BrowserProfile] = {}
self._index = 0
self._lock = threading.Lock()
def get_fingerprint(self, session_id: str = "default") -> BrowserProfile:
with self._lock:
if self.strategy == "sticky" and session_id in self._session_map:
return copy.deepcopy(self._session_map[session_id])
if self.strategy == "round_robin":
profile = self.profiles[self._index % len(self.profiles)]
self._index += 1
elif self.strategy == "random":
profile = random.choice(self.profiles)
else:
stable_index = abs(hash(session_id)) % len(self.profiles)
profile = self.profiles[stable_index]
self._session_map[session_id] = copy.deepcopy(profile)
return copy.deepcopy(profile)
def get_stats(self) -> dict[str, Any]:
return {
"strategy": self.strategy,
"total_profiles": len(self.profiles),
"sticky_sessions": len(self._session_map),
}
Fingerprint = BrowserProfile
FingerprintManager = SessionFingerprintManager
FingerprintDatabase = BrowserProfileDatabase
def real_browser_user_agent() -> str:
"""Load a user-provided real browser UA, if configured."""
env_value = os.environ.get("PIXIV_REAL_USER_AGENT", "").strip()
if env_value:
return env_value
path_value = os.environ.get("PIXIV_REAL_USER_AGENT_FILE", "").strip()
if path_value:
path = Path(path_value)
if path.is_file():
return path.read_text(encoding="utf-8").strip()
return ""
def default_profile(session_id: str = "default") -> BrowserProfile:
return SessionFingerprintManager().get_fingerprint(session_id)
+597
View File
@@ -0,0 +1,597 @@
from __future__ import annotations
import re
from pathlib import Path
from typing import Any
from pixiv_app.core.auth import SessionStore
from pixiv_app.core.downloader import download_user_works, fetch_user_profile, fetch_user_work_ids
from pixiv_app.core.library_importer import LegacyDownloadImporter
from pixiv_app.core.library import DEFAULT_LIBRARY_DB, PixivLibrary
from pixiv_app.core.paths import app_session_file
from pixiv_app.core.thumbnails import DEFAULT_THUMBNAIL_DIR, ThumbnailCache
class LocalGalleryService:
def __init__(
self,
*,
db_path: str | Path = DEFAULT_LIBRARY_DB,
thumbnail_dir: str | Path = DEFAULT_THUMBNAIL_DIR,
) -> None:
self.db_path = Path(db_path)
library = PixivLibrary(self.db_path)
library.close()
self.thumbnail_cache = ThumbnailCache(db_path=self.db_path, cache_dir=thumbnail_dir)
def list_artworks(
self,
*,
query: str = "",
search_scope: str = "all",
tag: str = "",
artist_id: int | None = None,
artwork_type: str = "",
include_restricted: bool = True,
limit: int = 60,
offset: int = 0,
ensure_thumbnails: bool = True,
) -> dict[str, Any]:
limit = max(1, min(int(limit), 200))
offset = max(0, int(offset))
library = PixivLibrary(self.db_path)
try:
where: list[str] = []
params: list[Any] = []
joins = """
LEFT JOIN artists ar ON ar.artist_id = a.artist_id
LEFT JOIN followed_artists fa ON fa.artist_id = a.artist_id AND fa.enabled = 1
LEFT JOIN thumbnail_cache tc ON tc.artwork_id = a.artwork_id AND tc.page_index = 0
LEFT JOIN artwork_files af ON af.artwork_id = a.artwork_id AND af.page_index = 0
LEFT JOIN artwork_ocr_texts ocr ON ocr.artwork_id = a.artwork_id
"""
if query:
like = f"%{query}%"
if search_scope == "ocr":
where.append("(ocr.status = 'done' AND ocr.ocr_text LIKE ?)")
params.append(like)
elif search_scope == "title":
where.append("(a.title LIKE ? OR a.caption LIKE ? OR ar.name LIKE ?)")
params.extend([like, like, like])
elif search_scope == "tags":
joins += """
LEFT JOIN artwork_tags at_search ON at_search.artwork_id = a.artwork_id
LEFT JOIN tags t_search ON t_search.tag_id = at_search.tag_id
"""
where.append("t_search.name LIKE ?")
params.append(like)
else:
joins += """
LEFT JOIN artwork_tags at_search ON at_search.artwork_id = a.artwork_id
LEFT JOIN tags t_search ON t_search.tag_id = at_search.tag_id
"""
where.append(
"(a.title LIKE ? OR a.caption LIKE ? OR ar.name LIKE ? OR t_search.name LIKE ? "
"OR (ocr.status = 'done' AND ocr.ocr_text LIKE ?))"
)
params.extend([like, like, like, like, like])
if tag:
joins += """
JOIN artwork_tags at_filter ON at_filter.artwork_id = a.artwork_id
JOIN tags t_filter ON t_filter.tag_id = at_filter.tag_id
"""
where.append("t_filter.name = ?")
params.append(tag)
if artist_id is not None:
where.append("a.artist_id = ?")
params.append(int(artist_id))
if artwork_type:
where.append("a.type = ?")
params.append(artwork_type)
else:
where.append("a.type IN ('illust', 'manga')")
if not include_restricted:
where.append("a.x_restrict = 0")
where_sql = "WHERE " + " AND ".join(where) if where else ""
total = library.connection.execute(
f"SELECT COUNT(DISTINCT a.artwork_id) AS total FROM artworks a {joins} {where_sql}",
params,
).fetchone()["total"]
rows = library.connection.execute(
f"""
SELECT
a.artwork_id,
a.type,
a.title,
a.artist_id,
ar.name AS artist_name,
ar.account AS artist_account,
a.page_count,
a.bookmark_count,
a.like_count,
a.view_count,
a.x_restrict,
a.ai_type,
a.thumbnail_url,
CASE WHEN fav.artwork_id IS NULL THEN 0 ELSE 1 END AS is_favorite,
CASE WHEN fa.artist_id IS NULL THEN 0 ELSE 1 END AS is_followed,
COALESCE(tc.thumbnail_path, '') AS thumbnail_path,
COALESCE(af.file_path, '') AS primary_file_path,
CASE
WHEN ocr.status = 'done' AND ocr.ocr_text LIKE ? THEN 'ocr'
ELSE ''
END AS matched_by,
a.updated_at
FROM artworks a
{joins}
LEFT JOIN favorites fav ON fav.artwork_id = a.artwork_id
{where_sql}
GROUP BY a.artwork_id
ORDER BY a.updated_at DESC, a.artwork_id DESC
LIMIT ? OFFSET ?
""",
[f"%{query}%" if query else "", *params, limit, offset],
).fetchall()
items = [dict(row) for row in rows]
if ensure_thumbnails:
self._ensure_list_thumbnails(items)
items = self._refresh_thumbnail_paths(library, items)
return {"total": int(total), "limit": limit, "offset": offset, "items": items}
finally:
library.close()
def get_artwork(self, artwork_id: int, *, ensure_thumbnails: bool = True) -> dict[str, Any] | None:
if ensure_thumbnails:
self.thumbnail_cache.ensure_for_artwork(artwork_id)
library = PixivLibrary(self.db_path)
try:
row = library.connection.execute(
"""
SELECT a.*, ar.name AS artist_name, ar.account AS artist_account
, CASE WHEN fav.artwork_id IS NULL THEN 0 ELSE 1 END AS is_favorite
, CASE WHEN fa.artist_id IS NULL THEN 0 ELSE 1 END AS is_followed
FROM artworks a
LEFT JOIN artists ar ON ar.artist_id = a.artist_id
LEFT JOIN favorites fav ON fav.artwork_id = a.artwork_id
LEFT JOIN followed_artists fa ON fa.artist_id = a.artist_id AND fa.enabled = 1
WHERE a.artwork_id = ?
""",
(artwork_id,),
).fetchone()
if row is None:
return None
files = library.connection.execute(
"""
SELECT f.*, tc.thumbnail_path, tc.width AS thumbnail_width, tc.height AS thumbnail_height
FROM artwork_files f
LEFT JOIN thumbnail_cache tc
ON tc.artwork_id = f.artwork_id AND tc.page_index = f.page_index
WHERE f.artwork_id = ?
ORDER BY f.page_index ASC
""",
(artwork_id,),
).fetchall()
tags = library.connection.execute(
"""
SELECT t.name, t.translated_name
FROM tags t
JOIN artwork_tags at ON at.tag_id = t.tag_id
WHERE at.artwork_id = ?
ORDER BY t.name ASC
""",
(artwork_id,),
).fetchall()
history = library.connection.execute(
"""
SELECT status, downloaded_pages, skipped_pages, failed_pages, message, finished_at
FROM download_history
WHERE artwork_id = ?
ORDER BY finished_at DESC
LIMIT 20
""",
(artwork_id,),
).fetchall()
return {
"artwork": dict(row),
"files": [dict(item) for item in files],
"tags": [dict(item) for item in tags],
"history": [dict(item) for item in history],
}
finally:
library.close()
def list_tags(self, *, query: str = "", limit: int = 100) -> list[dict[str, Any]]:
library = PixivLibrary(self.db_path)
try:
params: list[Any] = []
where = ""
if query:
where = "WHERE t.name LIKE ? OR t.translated_name LIKE ?"
params.extend([f"%{query}%", f"%{query}%"])
rows = library.connection.execute(
f"""
SELECT t.name, t.translated_name, COUNT(at.artwork_id) AS artwork_count
FROM tags t
LEFT JOIN artwork_tags at ON at.tag_id = t.tag_id
{where}
GROUP BY t.tag_id
ORDER BY artwork_count DESC, t.name ASC
LIMIT ?
""",
[*params, max(1, min(int(limit), 500))],
).fetchall()
return [dict(row) for row in rows]
finally:
library.close()
def list_artists(self, *, query: str = "", limit: int = 100) -> list[dict[str, Any]]:
library = PixivLibrary(self.db_path)
try:
params: list[Any] = []
where = ""
if query:
where = "WHERE ar.name LIKE ? OR ar.account LIKE ?"
params.extend([f"%{query}%", f"%{query}%"])
rows = library.connection.execute(
f"""
SELECT ar.artist_id, ar.name, ar.account, COUNT(a.artwork_id) AS artwork_count
FROM artists ar
LEFT JOIN artworks a ON a.artist_id = ar.artist_id
{where}
GROUP BY ar.artist_id
ORDER BY
CASE WHEN COALESCE(NULLIF(ar.name, ''), NULLIF(ar.account, '')) = '' THEN 1 ELSE 0 END,
COALESCE(NULLIF(ar.name, ''), NULLIF(ar.account, ''), CAST(ar.artist_id AS TEXT)) ASC,
artwork_count DESC
LIMIT ?
""",
[*params, max(1, min(int(limit), 500))],
).fetchall()
return [dict(row) for row in rows]
finally:
library.close()
def get_artist(self, artist_id: int, *, limit: int = 60, offset: int = 0) -> dict[str, Any] | None:
library = PixivLibrary(self.db_path)
try:
row = library.connection.execute(
"""
SELECT ar.*, COUNT(a.artwork_id) AS artwork_count,
CASE WHEN fa.enabled = 1 THEN 1 ELSE 0 END AS is_followed,
fa.last_synced_at, fa.last_error
FROM artists ar
LEFT JOIN artworks a ON a.artist_id = ar.artist_id
LEFT JOIN followed_artists fa ON fa.artist_id = ar.artist_id
WHERE ar.artist_id = ?
GROUP BY ar.artist_id
""",
(artist_id,),
).fetchone()
if row is None:
return None
return {
"artist": dict(row),
"artworks": self.list_artworks(artist_id=artist_id, limit=limit, offset=offset),
}
finally:
library.close()
def list_favorites(self, *, limit: int = 60, offset: int = 0) -> dict[str, Any]:
library = PixivLibrary(self.db_path)
try:
total = library.connection.execute(
"""
SELECT COUNT(*) AS total
FROM favorites fav
JOIN artworks a ON a.artwork_id = fav.artwork_id
WHERE a.type IN ('illust', 'manga')
"""
).fetchone()["total"]
rows = library.connection.execute(
"""
SELECT
a.artwork_id, a.type, a.title, a.artist_id, ar.name AS artist_name,
ar.account AS artist_account,
a.page_count, a.bookmark_count, a.like_count, a.view_count,
a.x_restrict, a.ai_type, a.thumbnail_url,
1 AS is_favorite,
CASE WHEN fa.artist_id IS NULL THEN 0 ELSE 1 END AS is_followed,
COALESCE(tc.thumbnail_path, '') AS thumbnail_path,
COALESCE(af.file_path, '') AS primary_file_path,
fav.created_at AS favorite_at
FROM favorites fav
JOIN artworks a ON a.artwork_id = fav.artwork_id
LEFT JOIN artists ar ON ar.artist_id = a.artist_id
LEFT JOIN followed_artists fa ON fa.artist_id = a.artist_id AND fa.enabled = 1
LEFT JOIN thumbnail_cache tc ON tc.artwork_id = a.artwork_id AND tc.page_index = 0
LEFT JOIN artwork_files af ON af.artwork_id = a.artwork_id AND af.page_index = 0
WHERE a.type IN ('illust', 'manga')
ORDER BY fav.created_at DESC
LIMIT ? OFFSET ?
""",
(max(1, min(int(limit), 200)), max(0, int(offset))),
).fetchall()
items = [dict(row) for row in rows]
self._ensure_list_thumbnails(items)
items = self._refresh_thumbnail_paths(library, items)
return {"total": int(total), "limit": limit, "offset": offset, "items": items}
finally:
library.close()
def set_favorite(self, artwork_id: int, favorite: bool, note: str = "") -> dict[str, Any]:
library = PixivLibrary(self.db_path)
try:
library.set_favorite(artwork_id, favorite, note)
return {"artwork_id": artwork_id, "favorite": favorite}
finally:
library.close()
def list_followed_artists(self) -> list[dict[str, Any]]:
library = PixivLibrary(self.db_path)
try:
rows = library.connection.execute(
"""
SELECT fa.artist_id, COALESCE(NULLIF(fa.name, ''), NULLIF(ar.name, ''), '') AS name,
COALESCE(NULLIF(ar.account, ''), '') AS account,
fa.enabled, fa.last_synced_at, fa.last_error,
COUNT(a.artwork_id) AS artwork_count
FROM followed_artists fa
LEFT JOIN artists ar ON ar.artist_id = fa.artist_id
LEFT JOIN artworks a ON a.artist_id = fa.artist_id
WHERE fa.enabled = 1
GROUP BY fa.artist_id
ORDER BY
CASE WHEN COALESCE(NULLIF(fa.name, ''), NULLIF(ar.name, ''), NULLIF(ar.account, '')) = '' THEN 1 ELSE 0 END,
COALESCE(NULLIF(fa.name, ''), NULLIF(ar.name, ''), NULLIF(ar.account, ''), CAST(fa.artist_id AS TEXT)) ASC,
fa.artist_id ASC
"""
).fetchall()
return [dict(row) for row in rows]
finally:
library.close()
def set_followed_artist(self, artist_ref: int | str, followed: bool, name: str = "") -> dict[str, Any]:
artist_id = self._resolve_artist_id(artist_ref)
clean_name = self._clean_name(name)
account = ""
if followed and not clean_name:
try:
profile = fetch_user_profile(
artist_id,
cookie=str(SessionStore(app_session_file()).load().get("cookie", "")).strip(),
)
profile_info = profile.get("profile") if isinstance(profile.get("profile"), dict) else {}
clean_name = self._clean_name(
profile.get("name")
or profile.get("userName")
or profile.get("displayName")
or profile_info.get("name")
)
account = self._clean_name(profile.get("account") or profile.get("userAccount"))
except Exception:
clean_name = ""
library = PixivLibrary(self.db_path)
try:
if clean_name or account:
library.upsert_artist_profile(artist_id, name=clean_name, account=account)
library.set_followed_artist(artist_id, followed, clean_name)
return {
"artist_id": artist_id,
"followed": followed,
"name": clean_name,
"account": account,
"cloud_synced": False,
}
finally:
library.close()
def _resolve_artist_id(self, artist_ref: int | str) -> int:
text = str(artist_ref).strip()
if not text:
raise ValueError("Please enter a Pixiv artist ID, profile URL, or synced artist name.")
if text.lower().startswith("artist_") and text[7:].isdigit():
return int(text[7:])
match = re.search(r"/users/(\d+)", text)
if match:
return int(match.group(1))
if text.isdigit():
return int(text)
lookup = text.lstrip("@")
library = PixivLibrary(self.db_path)
try:
rows = [
dict(row)
for row in library.connection.execute(
"""
SELECT artist_id, name, account
FROM artists
WHERE lower(name) = lower(?) OR lower(account) = lower(?)
ORDER BY
CASE
WHEN lower(account) = lower(?) THEN 0
WHEN lower(name) = lower(?) THEN 1
ELSE 2
END,
artist_id ASC
""",
(lookup, lookup, lookup, lookup),
).fetchall()
]
finally:
library.close()
if len(rows) == 1:
return int(rows[0]["artist_id"])
if not rows:
raise ValueError("Could not resolve Pixiv artist ID from that name. Enter an ID/profile URL or sync the artist first.")
choices = ", ".join(str(int(row["artist_id"])) for row in rows[:5])
raise ValueError(f"Artist name matched multiple Pixiv users; use an ID instead: {choices}")
@staticmethod
def _clean_name(value: Any) -> str:
text = str(value or "").strip()
return "" if text.lower() in {"none", "null", "undefined"} else text
def scan_legacy_downloads(self, *, root: str | Path = "Sakura_Downloads") -> dict[str, Any]:
summary = LegacyDownloadImporter(db_path=self.db_path).scan(root)
self.thumbnail_cache.build_missing(limit=500)
return summary.__dict__
def sync_followed_artists(
self,
*,
cookie: str = "",
max_new_per_artist: int = 20,
download: bool = True,
save_root: str | Path = "Sakura_Downloads",
) -> dict[str, Any]:
if not cookie:
cookie = str(SessionStore(app_session_file()).load().get("cookie", "")).strip()
if not cookie:
raise ValueError("No Pixiv cookie available. Save a session in the GUI first or pass cookie explicitly.")
library = PixivLibrary(self.db_path)
try:
followed = library.connection.execute(
"""
SELECT fa.artist_id,
COALESCE(NULLIF(fa.name, ''), NULLIF(ar.name, ''), NULLIF(ar.account, ''), '') AS name,
COALESCE(NULLIF(ar.account, ''), '') AS account
FROM followed_artists fa
LEFT JOIN artists ar ON ar.artist_id = fa.artist_id
WHERE fa.enabled = 1
ORDER BY fa.artist_id
"""
).fetchall()
finally:
library.close()
results: list[dict[str, Any]] = []
for row in followed:
artist_id = int(row["artist_id"])
artist_name = self._clean_name(row["name"])
artist_account = self._clean_name(row["account"])
library = PixivLibrary(self.db_path)
try:
existing = {
int(item["artwork_id"])
for item in library.connection.execute(
"SELECT artwork_id FROM artworks WHERE artist_id = ? AND type IN ('illust', 'manga')",
(artist_id,),
).fetchall()
}
finally:
library.close()
try:
_, work_ids = fetch_user_work_ids(user_id=artist_id, cookie=cookie)
new_ids = [work_id for work_id in work_ids if work_id not in existing][: max(0, int(max_new_per_artist))]
downloaded = 0
if download and new_ids:
_, download_results = download_user_works(
user_id=artist_id,
cookie=cookie,
max_workers=3,
request_delay=0.2,
save_root=save_root,
work_ids=new_ids,
)
downloaded = len([item for item in download_results if item.ok])
library = PixivLibrary(self.db_path)
try:
library.mark_artist_synced(artist_id)
finally:
library.close()
cloud_synced = False
cloud_alerts = 0
results.append(
{
"artist_id": artist_id,
"remote_count": len(work_ids),
"new_count": len(new_ids),
"downloaded": downloaded,
"cloud_synced": cloud_synced,
"alerts": cloud_alerts,
"error": "",
}
)
except Exception as exc:
library = PixivLibrary(self.db_path)
try:
library.mark_artist_synced(artist_id, error=str(exc))
finally:
library.close()
results.append({"artist_id": artist_id, "remote_count": 0, "new_count": 0, "downloaded": 0, "alerts": 0, "error": str(exc)})
return {"artists": results}
def build_missing_thumbnails(self, *, limit: int = 200) -> list[dict[str, Any]]:
return [item.__dict__ for item in self.thumbnail_cache.build_missing(limit=limit)]
def get_thumbnail_path(self, artwork_id: int, *, page_index: int = 0) -> Path | None:
self.thumbnail_cache.ensure_for_artwork(artwork_id)
library = PixivLibrary(self.db_path)
try:
row = library.connection.execute(
"""
SELECT thumbnail_path
FROM thumbnail_cache
WHERE artwork_id = ? AND page_index = ?
""",
(artwork_id, page_index),
).fetchone()
if row is None:
return None
path = Path(str(row["thumbnail_path"]))
return path if path.exists() else None
finally:
library.close()
def get_source_file_path(self, artwork_id: int, *, page_index: int = 0) -> Path | None:
library = PixivLibrary(self.db_path)
try:
row = library.connection.execute(
"""
SELECT file_path
FROM artwork_files
WHERE artwork_id = ? AND page_index = ?
""",
(artwork_id, page_index),
).fetchone()
if row is None:
return None
path = Path(str(row["file_path"]))
return path if path.exists() else None
finally:
library.close()
def _ensure_list_thumbnails(self, items: list[dict[str, Any]]) -> None:
for item in items:
if item.get("thumbnail_path"):
continue
if not item.get("primary_file_path"):
continue
self.thumbnail_cache.ensure_for_artwork(int(item["artwork_id"]))
def _refresh_thumbnail_paths(self, library: PixivLibrary, items: list[dict[str, Any]]) -> list[dict[str, Any]]:
artwork_ids = [int(item["artwork_id"]) for item in items]
if not artwork_ids:
return items
placeholders = ",".join("?" for _ in artwork_ids)
rows = library.connection.execute(
f"""
SELECT artwork_id, thumbnail_path
FROM thumbnail_cache
WHERE page_index = 0 AND artwork_id IN ({placeholders})
""",
artwork_ids,
).fetchall()
thumbs = {int(row["artwork_id"]): str(row["thumbnail_path"]) for row in rows}
for item in items:
item["thumbnail_path"] = thumbs.get(int(item["artwork_id"]), item.get("thumbnail_path", ""))
return items
+703
View File
@@ -0,0 +1,703 @@
from __future__ import annotations
import json
import re
import sqlite3
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from pixiv_app.core.paths import app_library_file
DEFAULT_LIBRARY_DB = app_library_file()
@dataclass
class DownloadFileRecord:
page_index: int
file_path: str
source_url: str
status: str
size_bytes: int = 0
content_hash: str = ""
class PixivLibrary:
def __init__(self, db_path: str | Path = DEFAULT_LIBRARY_DB) -> None:
self.db_path = Path(db_path)
self.db_path.parent.mkdir(parents=True, exist_ok=True) if self.db_path.parent != Path(".") else None
# Allow sharing one connection across threads when guarded by a lock (e.g. TaskQueue).
self.connection = sqlite3.connect(self.db_path, timeout=30, check_same_thread=False)
self.connection.row_factory = sqlite3.Row
self.connection.execute("PRAGMA journal_mode=WAL")
self.connection.execute("PRAGMA foreign_keys=ON")
self.setup()
def close(self) -> None:
self.connection.close()
def setup(self) -> None:
with self.connection:
self.connection.executescript(
"""
CREATE TABLE IF NOT EXISTS artists (
artist_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
account TEXT,
avatar_url TEXT,
metadata_json TEXT NOT NULL DEFAULT '{}',
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS artworks (
artwork_id INTEGER PRIMARY KEY,
artist_id INTEGER,
type TEXT NOT NULL DEFAULT 'illust',
title TEXT NOT NULL DEFAULT '',
caption TEXT NOT NULL DEFAULT '',
page_count INTEGER NOT NULL DEFAULT 0,
bookmark_count INTEGER NOT NULL DEFAULT 0,
like_count INTEGER NOT NULL DEFAULT 0,
view_count INTEGER NOT NULL DEFAULT 0,
x_restrict INTEGER NOT NULL DEFAULT 0,
ai_type INTEGER NOT NULL DEFAULT 0,
thumbnail_url TEXT,
metadata_json TEXT NOT NULL DEFAULT '{}',
created_at TEXT,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (artist_id) REFERENCES artists(artist_id)
);
CREATE TABLE IF NOT EXISTS tags (
tag_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
translated_name TEXT
);
CREATE TABLE IF NOT EXISTS artwork_tags (
artwork_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
PRIMARY KEY (artwork_id, tag_id),
FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE,
FOREIGN KEY (tag_id) REFERENCES tags(tag_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS artwork_files (
artwork_id INTEGER NOT NULL,
page_index INTEGER NOT NULL,
file_path TEXT NOT NULL,
source_url TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL DEFAULT 'unknown',
size_bytes INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (artwork_id, page_index),
FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS download_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
artwork_id INTEGER NOT NULL,
status TEXT NOT NULL,
downloaded_pages INTEGER NOT NULL DEFAULT 0,
skipped_pages INTEGER NOT NULL DEFAULT 0,
failed_pages INTEGER NOT NULL DEFAULT 0,
message TEXT NOT NULL DEFAULT '',
started_at TEXT,
finished_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS sync_state (
key TEXT PRIMARY KEY,
value_json TEXT NOT NULL DEFAULT '{}',
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS thumbnail_cache (
artwork_id INTEGER NOT NULL,
page_index INTEGER NOT NULL,
source_path TEXT NOT NULL,
thumbnail_path TEXT NOT NULL,
width INTEGER NOT NULL DEFAULT 0,
height INTEGER NOT NULL DEFAULT 0,
source_mtime REAL NOT NULL DEFAULT 0,
source_size INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (artwork_id, page_index),
FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS artwork_ocr_texts (
artwork_id INTEGER PRIMARY KEY,
language TEXT NOT NULL DEFAULT 'mixed',
ocr_text TEXT NOT NULL DEFAULT '',
confidence REAL,
engine TEXT NOT NULL DEFAULT 'paddleocr',
status TEXT NOT NULL DEFAULT 'pending',
attempts INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS favorites (
artwork_id INTEGER PRIMARY KEY,
note TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (artwork_id) REFERENCES artworks(artwork_id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS followed_artists (
artist_id INTEGER PRIMARY KEY,
name TEXT NOT NULL DEFAULT '',
enabled INTEGER NOT NULL DEFAULT 1,
last_synced_at TEXT,
last_error TEXT,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_artworks_artist_id ON artworks(artist_id);
CREATE INDEX IF NOT EXISTS idx_artworks_type ON artworks(type);
CREATE INDEX IF NOT EXISTS idx_download_history_artwork_id ON download_history(artwork_id);
CREATE INDEX IF NOT EXISTS idx_artwork_files_path ON artwork_files(file_path);
CREATE INDEX IF NOT EXISTS idx_thumbnail_cache_path ON thumbnail_cache(thumbnail_path);
CREATE INDEX IF NOT EXISTS idx_artwork_ocr_texts_status ON artwork_ocr_texts(status);
CREATE INDEX IF NOT EXISTS idx_artwork_ocr_texts_text ON artwork_ocr_texts(ocr_text);
CREATE INDEX IF NOT EXISTS idx_favorites_created_at ON favorites(created_at);
CREATE INDEX IF NOT EXISTS idx_followed_artists_enabled ON followed_artists(enabled);
CREATE TABLE IF NOT EXISTS download_tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
fingerprint TEXT NOT NULL UNIQUE,
target_type TEXT NOT NULL,
target_id INTEGER,
page_index INTEGER,
task_kind TEXT NOT NULL,
priority INTEGER NOT NULL DEFAULT 2,
status TEXT NOT NULL DEFAULT 'pending',
payload_json TEXT NOT NULL DEFAULT '{}',
attempts INTEGER NOT NULL DEFAULT 0,
leased_until REAL,
worker_id TEXT,
last_error TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_download_tasks_status_pri_id
ON download_tasks(status, priority DESC, id ASC);
CREATE TABLE IF NOT EXISTS artist_sync_watermark (
artist_id INTEGER PRIMARY KEY,
last_synced_illust_id INTEGER NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
"""
)
self._apply_schema_migrations()
def _apply_schema_migrations(self) -> None:
"""Incremental ALTER for existing databases created before new columns/tables."""
with self.connection:
cols = {row[1] for row in self.connection.execute("PRAGMA table_info(artwork_files)")}
if "content_hash" not in cols:
self.connection.execute(
"ALTER TABLE artwork_files ADD COLUMN content_hash TEXT NOT NULL DEFAULT ''"
)
# Drop legacy placeholder names like artist_12345 so UI can fall back cleanly.
self.connection.execute(
"UPDATE artists SET name = '' WHERE name = 'artist_' || artist_id"
)
self.connection.execute(
"UPDATE followed_artists SET name = '' WHERE name = 'artist_' || artist_id"
)
self.connection.execute(
"UPDATE artists SET name = '' WHERE lower(name) IN ('none', 'null', 'undefined')"
)
self.connection.execute(
"UPDATE followed_artists SET name = '' WHERE lower(name) IN ('none', 'null', 'undefined')"
)
rows = self.connection.execute(
"SELECT artwork_id, metadata_json FROM artworks WHERE artist_id IS NULL OR artist_id = 0"
).fetchall()
for row in rows:
try:
metadata = json.loads(str(row["metadata_json"] or "{}"))
except Exception:
continue
artist_id = _as_int(metadata.get("userId") or metadata.get("author_id") or metadata.get("artistId"))
if artist_id is None:
continue
self.ensure_artist_stub(artist_id, name=str(metadata.get("userName") or metadata.get("artistName") or ""))
self.connection.execute(
"UPDATE artworks SET artist_id = ?, updated_at = CURRENT_TIMESTAMP WHERE artwork_id = ?",
(artist_id, int(row["artwork_id"])),
)
def upsert_artwork(self, detail: dict[str, Any], *, artwork_type: str = "illust") -> None:
source_id = _as_int(detail.get("illustId") or detail.get("id") or detail.get("novelId"))
if source_id is None:
return
source_platform = str(detail.get("source_platform") or detail.get("source") or "")
artwork_id = library_artwork_id(source_id, artwork_type, source_platform=source_platform)
artist_id = _as_int(detail.get("userId"))
if artist_id is not None:
self._upsert_artist(detail, artist_id)
tags = _extract_tags(detail)
urls = detail.get("urls") if isinstance(detail.get("urls"), dict) else {}
thumbnail_url = (
detail.get("url")
or detail.get("coverUrl")
or urls.get("small")
or urls.get("thumb")
or urls.get("regular")
or urls.get("original")
)
with self.connection:
self.connection.execute(
"""
INSERT INTO artworks (
artwork_id, artist_id, type, title, caption, page_count,
bookmark_count, like_count, view_count, x_restrict, ai_type,
thumbnail_url, metadata_json, created_at, updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(artwork_id) DO UPDATE SET
artist_id = excluded.artist_id,
type = excluded.type,
title = excluded.title,
caption = excluded.caption,
page_count = excluded.page_count,
bookmark_count = excluded.bookmark_count,
like_count = excluded.like_count,
view_count = excluded.view_count,
x_restrict = excluded.x_restrict,
ai_type = excluded.ai_type,
thumbnail_url = excluded.thumbnail_url,
metadata_json = excluded.metadata_json,
created_at = COALESCE(excluded.created_at, artworks.created_at),
updated_at = CURRENT_TIMESTAMP
""",
(
artwork_id,
artist_id,
artwork_type,
str(detail.get("title") or ""),
str(detail.get("description") or detail.get("caption") or ""),
_as_int(detail.get("pageCount")) or 0,
_as_int(detail.get("bookmarkCount")) or 0,
_as_int(detail.get("likeCount")) or 0,
_as_int(detail.get("viewCount")) or 0,
_as_int(detail.get("xRestrict")) or 0,
_as_int(detail.get("aiType")) or 0,
str(thumbnail_url) if thumbnail_url else None,
json.dumps({**detail, "sourceId": source_id}, ensure_ascii=False),
str(detail.get("createDate") or detail.get("uploadDate") or "") or None,
),
)
self.connection.execute("DELETE FROM artwork_tags WHERE artwork_id = ?", (artwork_id,))
for name, translated_name in tags:
tag_id = self._upsert_tag(name, translated_name)
self.connection.execute(
"INSERT OR IGNORE INTO artwork_tags (artwork_id, tag_id) VALUES (?, ?)",
(artwork_id, tag_id),
)
def ensure_artwork_stub(self, artwork_id: int, *, artwork_type: str = "illust", title: str = "", source_platform: str = "") -> int:
db_artwork_id = library_artwork_id(artwork_id, artwork_type, source_platform=source_platform)
with self.connection:
self.connection.execute(
"""
INSERT OR IGNORE INTO artworks (artwork_id, type, title, updated_at)
VALUES (?, ?, ?, CURRENT_TIMESTAMP)
""",
(db_artwork_id, artwork_type, title),
)
return db_artwork_id
def ensure_artist_stub(self, artist_id: int, *, name: str = "") -> None:
with self.connection:
self.connection.execute(
"""
INSERT OR IGNORE INTO artists (artist_id, name, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
""",
(artist_id, name or ""),
)
def upsert_artist_profile(self, artist_id: int, *, name: str = "", account: str = "", avatar_url: str = "") -> None:
clean_name = _clean_optional_text(name)
clean_account = _clean_optional_text(account)
clean_avatar = _clean_optional_text(avatar_url)
with self.connection:
self.connection.execute(
"""
INSERT INTO artists (artist_id, name, account, avatar_url, metadata_json, updated_at)
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(artist_id) DO UPDATE SET
name = COALESCE(NULLIF(excluded.name, ''), artists.name),
account = COALESCE(NULLIF(excluded.account, ''), artists.account),
avatar_url = COALESCE(NULLIF(excluded.avatar_url, ''), artists.avatar_url),
metadata_json = CASE
WHEN excluded.metadata_json != '{}' THEN excluded.metadata_json
ELSE artists.metadata_json
END,
updated_at = CURRENT_TIMESTAMP
""",
(
int(artist_id),
clean_name,
clean_account,
clean_avatar,
json.dumps(
{
"userId": int(artist_id),
"userName": clean_name,
"userAccount": clean_account,
},
ensure_ascii=False,
),
),
)
def set_artwork_artist(self, artwork_id: int, artist_id: int) -> None:
with self.connection:
self.ensure_artist_stub(artist_id)
self.connection.execute(
"""
UPDATE artworks
SET artist_id = ?, updated_at = CURRENT_TIMESTAMP
WHERE artwork_id = ?
""",
(artist_id, artwork_id),
)
def record_files(self, artwork_id: int, files: list[DownloadFileRecord]) -> None:
with self.connection:
for item in files:
self.connection.execute(
"""
INSERT INTO artwork_files (
artwork_id, page_index, file_path, source_url, status, size_bytes,
content_hash, updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(artwork_id, page_index) DO UPDATE SET
file_path = excluded.file_path,
source_url = excluded.source_url,
status = excluded.status,
size_bytes = excluded.size_bytes,
content_hash = CASE
WHEN excluded.content_hash != '' THEN excluded.content_hash
ELSE artwork_files.content_hash
END,
updated_at = CURRENT_TIMESTAMP
""",
(
artwork_id,
item.page_index,
item.file_path,
item.source_url,
item.status,
item.size_bytes,
item.content_hash or "",
),
)
def record_download_history(
self,
*,
artwork_id: int,
status: str,
downloaded_pages: int,
skipped_pages: int,
failed_pages: int,
message: str,
started_at: str | None = None,
) -> None:
with self.connection:
self.connection.execute(
"""
INSERT INTO download_history (
artwork_id, status, downloaded_pages, skipped_pages, failed_pages, message, started_at
)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(artwork_id, status, downloaded_pages, skipped_pages, failed_pages, message, started_at),
)
def update_sync_state(self, key: str, value: dict[str, Any]) -> None:
with self.connection:
self.connection.execute(
"""
INSERT INTO sync_state (key, value_json, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(key) DO UPDATE SET
value_json = excluded.value_json,
updated_at = CURRENT_TIMESTAMP
""",
(key, json.dumps(value, ensure_ascii=False)),
)
def upsert_thumbnail(
self,
*,
artwork_id: int,
page_index: int,
source_path: str,
thumbnail_path: str,
width: int,
height: int,
source_mtime: float,
source_size: int,
) -> None:
with self.connection:
self.connection.execute(
"""
INSERT INTO thumbnail_cache (
artwork_id, page_index, source_path, thumbnail_path, width, height,
source_mtime, source_size, updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(artwork_id, page_index) DO UPDATE SET
source_path = excluded.source_path,
thumbnail_path = excluded.thumbnail_path,
width = excluded.width,
height = excluded.height,
source_mtime = excluded.source_mtime,
source_size = excluded.source_size,
updated_at = CURRENT_TIMESTAMP
""",
(artwork_id, page_index, source_path, thumbnail_path, width, height, source_mtime, source_size),
)
def upsert_ocr_text(
self,
artwork_id: int,
*,
text: str,
language: str = "mixed",
confidence: float | None = None,
engine: str = "paddleocr",
status: str = "done",
attempts: int = 1,
) -> None:
with self.connection:
self.connection.execute(
"""
INSERT INTO artwork_ocr_texts (
artwork_id, language, ocr_text, confidence, engine, status, attempts, updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(artwork_id) DO UPDATE SET
language = excluded.language,
ocr_text = excluded.ocr_text,
confidence = excluded.confidence,
engine = excluded.engine,
status = excluded.status,
attempts = excluded.attempts,
updated_at = CURRENT_TIMESTAMP
""",
(int(artwork_id), language, text, confidence, engine, status, attempts),
)
def get_ocr_text(self, artwork_id: int) -> sqlite3.Row | None:
return self.connection.execute(
"SELECT * FROM artwork_ocr_texts WHERE artwork_id = ?",
(int(artwork_id),),
).fetchone()
def set_favorite(self, artwork_id: int, favorite: bool, note: str = "") -> None:
with self.connection:
if favorite:
self.connection.execute(
"""
INSERT INTO favorites (artwork_id, note, created_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(artwork_id) DO UPDATE SET note = excluded.note
""",
(artwork_id, note),
)
else:
self.connection.execute("DELETE FROM favorites WHERE artwork_id = ?", (artwork_id,))
def set_followed_artist(self, artist_id: int, followed: bool, name: str = "") -> None:
with self.connection:
self.ensure_artist_stub(artist_id, name=name)
if followed:
self.connection.execute(
"""
INSERT INTO followed_artists (artist_id, name, enabled, created_at)
VALUES (?, ?, 1, CURRENT_TIMESTAMP)
ON CONFLICT(artist_id) DO UPDATE SET
name = COALESCE(NULLIF(excluded.name, ''), followed_artists.name),
enabled = 1
""",
(artist_id, name),
)
else:
self.connection.execute(
"UPDATE followed_artists SET enabled = 0 WHERE artist_id = ?",
(artist_id,),
)
def upsert_origin_followed_artist(
self,
artist_id: int,
*,
name: str = "",
account: str = "",
avatar_url: str = "",
) -> str:
clean_name = _clean_optional_text(name)
before = self.connection.execute(
"""
SELECT fa.enabled, fa.name AS follow_name, ar.name AS artist_name,
ar.account, ar.avatar_url
FROM followed_artists fa
LEFT JOIN artists ar ON ar.artist_id = fa.artist_id
WHERE fa.artist_id = ?
""",
(int(artist_id),),
).fetchone()
status = "created"
if before is not None:
changed = (
not bool(before["enabled"])
or (clean_name and clean_name != str(before["follow_name"] or before["artist_name"] or ""))
or (account and account != str(before["account"] or ""))
or (avatar_url and avatar_url != str(before["avatar_url"] or ""))
)
status = "updated" if changed else "skipped"
with self.connection:
self.upsert_artist_profile(
int(artist_id),
name=clean_name,
account=account,
avatar_url=avatar_url,
)
self.connection.execute(
"""
INSERT INTO followed_artists (artist_id, name, enabled, last_synced_at, last_error, created_at)
VALUES (?, ?, 1, CURRENT_TIMESTAMP, '', CURRENT_TIMESTAMP)
ON CONFLICT(artist_id) DO UPDATE SET
name = COALESCE(NULLIF(excluded.name, ''), followed_artists.name),
enabled = 1,
last_synced_at = CURRENT_TIMESTAMP,
last_error = ''
""",
(int(artist_id), clean_name),
)
return status
def mark_artist_synced(self, artist_id: int, *, error: str = "") -> None:
with self.connection:
self.connection.execute(
"""
INSERT INTO followed_artists (artist_id, enabled, last_synced_at, last_error)
VALUES (?, 1, CURRENT_TIMESTAMP, ?)
ON CONFLICT(artist_id) DO UPDATE SET
last_synced_at = CURRENT_TIMESTAMP,
last_error = excluded.last_error
""",
(artist_id, error),
)
def _upsert_artist(self, detail: dict[str, Any], artist_id: int) -> None:
self.connection.execute(
"""
INSERT INTO artists (artist_id, name, account, avatar_url, metadata_json, updated_at)
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(artist_id) DO UPDATE SET
name = excluded.name,
account = COALESCE(excluded.account, artists.account),
avatar_url = COALESCE(excluded.avatar_url, artists.avatar_url),
metadata_json = excluded.metadata_json,
updated_at = CURRENT_TIMESTAMP
""",
(
artist_id,
str(detail.get("userName") or detail.get("artistName") or ""),
str(detail.get("userAccount") or "") or None,
str(detail.get("profileImageUrl") or detail.get("userImage") or "") or None,
json.dumps(
{
"userId": artist_id,
"userName": detail.get("userName"),
"userAccount": detail.get("userAccount"),
},
ensure_ascii=False,
),
),
)
def _upsert_tag(self, name: str, translated_name: str | None) -> int:
self.connection.execute(
"""
INSERT INTO tags (name, translated_name)
VALUES (?, ?)
ON CONFLICT(name) DO UPDATE SET
translated_name = COALESCE(excluded.translated_name, tags.translated_name)
""",
(name, translated_name),
)
row = self.connection.execute("SELECT tag_id FROM tags WHERE name = ?", (name,)).fetchone()
return int(row["tag_id"])
def _as_int(value: Any) -> int | None:
if value is None:
return None
try:
return int(value)
except (TypeError, ValueError):
return None
def _clean_optional_text(value: Any) -> str:
text = str(value or "").strip()
return "" if text.lower() in {"none", "null", "undefined"} else text
def library_artwork_id(source_id: int, artwork_type: str = "illust", source_platform: str = "") -> int:
"""Map a source-native ID into the unified library artwork_id namespace.
Pixiv novels use negative source_id; AO3 works use a negative offset
range to avoid collisions with Pixiv novel IDs.
"""
if artwork_type == "novel":
if source_platform == "ao3":
# AO3: use negative offset range -(200M + work_id) to stay clear
# of Pixiv novel IDs (currently -1 .. -199_999_999).
AO3_OFFSET = 200_000_000
return -(AO3_OFFSET + abs(int(source_id)))
return -abs(int(source_id))
return int(source_id)
def _extract_tags(detail: dict[str, Any]) -> list[tuple[str, str | None]]:
raw_tags = detail.get("tags")
if isinstance(raw_tags, dict):
raw_tags = raw_tags.get("tags")
if not isinstance(raw_tags, list):
return []
result: list[tuple[str, str | None]] = []
for item in raw_tags:
if isinstance(item, str):
result.append((item, None))
continue
if not isinstance(item, dict):
continue
name = item.get("tag") or item.get("name")
if not name:
continue
translation = item.get("translation")
translated_name = None
if isinstance(translation, dict):
translated_name = translation.get("en") or translation.get("zh") or translation.get("romaji")
elif isinstance(translation, str):
translated_name = translation
result.append((str(name), str(translated_name) if translated_name else None))
return result
+76
View File
@@ -0,0 +1,76 @@
from __future__ import annotations
import re
from dataclasses import dataclass
from pathlib import Path
from pixiv_app.core.library import DEFAULT_LIBRARY_DB, DownloadFileRecord, PixivLibrary
IMAGE_SUFFIXES = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif"}
ILLUST_FILE_PATTERN = re.compile(r"(?P<artwork_id>\d+)_p(?P<page_index>\d+)", re.IGNORECASE)
@dataclass
class ImportSummary:
scanned_files: int = 0
imported_files: int = 0
skipped_files: int = 0
imported_artworks: int = 0
class LegacyDownloadImporter:
def __init__(self, *, db_path: str | Path = DEFAULT_LIBRARY_DB) -> None:
self.db_path = Path(db_path)
def scan(self, root: str | Path = "Sakura_Downloads") -> ImportSummary:
root_path = Path(root)
summary = ImportSummary()
if not root_path.exists():
return summary
library = PixivLibrary(self.db_path)
imported_artworks: set[int] = set()
try:
for file_path in root_path.rglob("*"):
if not file_path.is_file() or file_path.suffix.lower() not in IMAGE_SUFFIXES:
continue
summary.scanned_files += 1
match = ILLUST_FILE_PATTERN.search(file_path.stem)
if match is None:
summary.skipped_files += 1
continue
artwork_id = int(match.group("artwork_id"))
page_index = int(match.group("page_index"))
library.ensure_artwork_stub(artwork_id, artwork_type="illust", title=str(artwork_id))
artist_id = self._infer_artist_id(root_path, file_path)
if artist_id is not None:
library.set_artwork_artist(artwork_id, artist_id)
library.record_files(
artwork_id,
[
DownloadFileRecord(
page_index=page_index,
file_path=str(file_path.resolve()),
source_url="",
status="imported",
size_bytes=file_path.stat().st_size,
)
],
)
imported_artworks.add(artwork_id)
summary.imported_files += 1
summary.imported_artworks = len(imported_artworks)
return summary
finally:
library.close()
def _infer_artist_id(self, root_path: Path, file_path: Path) -> int | None:
try:
relative = file_path.relative_to(root_path)
except ValueError:
return None
if not relative.parts:
return None
first = relative.parts[0]
return int(first) if first.isdigit() else None
+128
View File
@@ -0,0 +1,128 @@
from __future__ import annotations
import logging
import os
import threading
from dataclasses import dataclass
from pathlib import Path
from typing import Callable
from pixiv_app.core.library import DEFAULT_LIBRARY_DB, PixivLibrary
OcrExtractor = Callable[[Path], tuple[str, float | None, str]]
@dataclass
class LocalOcrResult:
artwork_id: int
status: str
text_length: int = 0
confidence: float | None = None
error: str | None = None
class LocalOcrIndexer:
def __init__(
self,
*,
db_path: str | Path = DEFAULT_LIBRARY_DB,
enabled: bool | None = None,
max_dimension: int | None = None,
language: str | None = None,
max_concurrency: int | None = None,
) -> None:
self.db_path = Path(db_path)
self.enabled = enabled if enabled is not None else _env_bool("SAKURA_LOCAL_OCR_ENABLED", False)
self.max_dimension = max_dimension or int(os.getenv("SAKURA_LOCAL_OCR_MAX_DIMENSION", "1800"))
self.language = language or os.getenv("SAKURA_LOCAL_OCR_LANGUAGE", "ch")
self._semaphore = threading.BoundedSemaphore(
max_concurrency or int(os.getenv("SAKURA_LOCAL_OCR_MAX_CONCURRENCY", "1"))
)
def index_artwork(self, artwork_id: int, *, extractor: OcrExtractor | None = None) -> LocalOcrResult:
if not self.enabled and extractor is None:
return LocalOcrResult(artwork_id=artwork_id, status="disabled")
if not self._semaphore.acquire(blocking=False):
return LocalOcrResult(artwork_id=artwork_id, status="busy")
library = PixivLibrary(self.db_path)
try:
existing = library.get_ocr_text(artwork_id)
if existing is not None and str(existing["status"]) == "done" and str(existing["ocr_text"]).strip():
return LocalOcrResult(artwork_id=artwork_id, status="done", text_length=len(str(existing["ocr_text"])))
row = library.connection.execute(
"""
SELECT file_path
FROM artwork_files
WHERE artwork_id = ?
ORDER BY page_index ASC
LIMIT 1
""",
(int(artwork_id),),
).fetchone()
if row is None:
return LocalOcrResult(artwork_id=artwork_id, status="failed", error="file_not_found")
path = Path(str(row["file_path"]))
if path.suffix.lower() not in {".jpg", ".jpeg", ".png", ".webp"}:
library.upsert_ocr_text(artwork_id, text="", status="failed", attempts=1)
return LocalOcrResult(artwork_id=artwork_id, status="failed", error="unsupported_file_type")
text, confidence, language = (extractor or self._paddle_ocr_extract)(path)
library.upsert_ocr_text(
artwork_id,
text=text.strip(),
confidence=confidence,
language=language or self.language,
status="done",
attempts=1,
)
logging.info("Local OCR done artwork=%s text_len=%s confidence=%s", artwork_id, len(text.strip()), confidence)
return LocalOcrResult(artwork_id=artwork_id, status="done", text_length=len(text.strip()), confidence=confidence)
except Exception as exc:
library.upsert_ocr_text(artwork_id, text="", status="failed", attempts=1)
logging.warning("Local OCR failed artwork=%s error=%s", artwork_id, exc.__class__.__name__)
return LocalOcrResult(artwork_id=artwork_id, status="failed", error=exc.__class__.__name__)
finally:
library.close()
self._semaphore.release()
def _paddle_ocr_extract(self, path: Path) -> tuple[str, float | None, str]:
from PIL import Image
from paddleocr import PaddleOCR
work_path = path
with Image.open(path) as image:
original_size = image.size
image.thumbnail((self.max_dimension, self.max_dimension))
if image.size != original_size:
tmp = path.with_suffix(".local-ocr-resized.jpg")
image.convert("RGB").save(tmp, quality=90)
work_path = tmp
try:
ocr = PaddleOCR(use_angle_cls=True, lang=self.language, use_gpu=False)
result = ocr.ocr(str(work_path), cls=True)
texts: list[str] = []
scores: list[float] = []
for page in result or []:
for line in page or []:
if len(line) >= 2 and isinstance(line[1], (list, tuple)):
text = str(line[1][0]).strip()
if text:
texts.append(text)
try:
scores.append(float(line[1][1]))
except (TypeError, ValueError, IndexError):
pass
confidence = sum(scores) / len(scores) if scores else None
return "\n".join(texts), confidence, self.language
finally:
if work_path != path:
work_path.unlink(missing_ok=True)
def _env_bool(name: str, default: bool) -> bool:
value = os.getenv(name)
if value is None:
return default
return value.strip().lower() in {"1", "true", "yes", "on"}
+50
View File
@@ -0,0 +1,50 @@
"""
Bridge URL-level plugin parsing with the rest of the app (tasks/GUI logging).
Full TaskQueue integration can map Resource → DownloadTaskSpec per site.
"""
from __future__ import annotations
from dataclasses import dataclass
import logging
from pathlib import Path
from pixiv_app.core.plugin.base import BasePlugin, PluginParseError, Resource
from pixiv_app.core.plugin.manager import PluginManager
logger = logging.getLogger(__name__)
@dataclass
class OrchestrationResult:
plugin_name: str
resources: list[Resource]
class PluginTaskOrchestrator:
"""Resolve plugins and run parse/download without replacing the legacy Pixiv GUI flow."""
def __init__(self, plugin_manager: PluginManager) -> None:
self.plugin_manager = plugin_manager
def get_plugin_for_url(self, url: str) -> BasePlugin | None:
return self.plugin_manager.get_plugin_for_url(url)
def parse_url(self, url: str) -> OrchestrationResult:
plugin = self.get_plugin_for_url(url)
if plugin is None:
raise PluginParseError(f"No plugin registered for: {url}")
resources = plugin.parse(url)
return OrchestrationResult(plugin_name=plugin.name, resources=resources)
def download_resource(
self,
plugin_name: str,
resource: Resource,
save_path: Path,
) -> list[Path]:
plugin = self.plugin_manager.plugins.get(plugin_name)
if plugin is None:
raise PluginParseError(f"Plugin not loaded: {plugin_name}")
return plugin.download(resource, save_path)
+423
View File
@@ -0,0 +1,423 @@
from __future__ import annotations
import asyncio
import re
from dataclasses import dataclass, field
from typing import Any, Callable
from pixiv_app.core.auth import SessionStore
from pixiv_app.core.downloader import create_session, request_json
from pixiv_app.core.gallery_service import LocalGalleryService
from pixiv_app.core.library import DEFAULT_LIBRARY_DB, PixivLibrary
from pixiv_app.core.paths import app_session_file
from pixiv_app.core.x_media_service import XMediaService, _normalize_username
ProgressCallback = Callable[[dict[str, Any]], None]
@dataclass
class OriginFollow:
platform: str
external_id: str
name: str = ""
handle: str = ""
avatar_url: str = ""
profile_url: str = ""
@dataclass
class OriginFollowSyncResult:
platform: str
created: int = 0
updated: int = 0
skipped: int = 0
failed: int = 0
cloud_created: int = 0
cloud_updated: int = 0
cloud_skipped: int = 0
pages: int = 0
errors: list[dict[str, str]] = field(default_factory=list)
progress: list[dict[str, Any]] = field(default_factory=list)
def to_dict(self) -> dict[str, Any]:
return {
"platform": self.platform,
"created": self.created,
"updated": self.updated,
"skipped": self.skipped,
"failed": self.failed,
"cloud_created": self.cloud_created,
"cloud_updated": self.cloud_updated,
"cloud_skipped": self.cloud_skipped,
"pages": self.pages,
"errors": self.errors,
"progress": self.progress,
}
class OriginFollowSyncService:
def __init__(
self,
*,
db_path: str = DEFAULT_LIBRARY_DB,
gallery_service: LocalGalleryService | None = None,
x_media_service: XMediaService | None = None,
) -> None:
self.db_path = db_path
self.gallery_service = gallery_service or LocalGalleryService(db_path=db_path)
self.x_media_service = x_media_service or XMediaService()
def sync(self, platform: str, *, progress: ProgressCallback | None = None) -> dict[str, Any]:
normalized = platform.strip().lower()
if normalized == "pixiv":
return self.sync_pixiv(progress=progress).to_dict()
if normalized == "x":
return self.sync_x(progress=progress).to_dict()
raise ValueError("请选择 Pixiv 或 X。")
def sync_pixiv(self, *, progress: ProgressCallback | None = None) -> OriginFollowSyncResult:
cookie = str(SessionStore(app_session_file()).load().get("cookie", "")).strip()
if not cookie:
raise ValueError("Pixiv Cookie 不存在,请先导入或保存 Pixiv 登录态。")
result = OriginFollowSyncResult(platform="pixiv")
session = create_session(cookie)
try:
user_id = self._fetch_pixiv_current_user_id(session, cookie)
follows = self._fetch_pixiv_following(session, cookie, user_id, result, progress=progress)
finally:
session.close()
self._upsert_pixiv_local(follows, result)
return result
def sync_x(self, *, progress: ProgressCallback | None = None) -> OriginFollowSyncResult:
plugin = self.x_media_service._get_plugin()
if not plugin.validate():
raise ValueError("X Playwright 不可用,请先安装 Playwright 并完成 X 登录。")
result = OriginFollowSyncResult(platform="x")
with plugin._playwright_lock:
follows = asyncio.run(self._fetch_x_following(plugin, result, progress=progress))
self._upsert_x_local(follows, result)
return result
def _emit(
self,
result: OriginFollowSyncResult,
progress: ProgressCallback | None,
payload: dict[str, Any],
) -> None:
result.progress.append(payload)
if progress is not None:
progress(payload)
def _fetch_pixiv_current_user_id(self, session: Any, cookie: str) -> str:
for url in (
"https://www.pixiv.net/ajax/user/self?lang=zh",
"https://www.pixiv.net/ajax/user/self",
):
try:
payload = request_json(session, url, cookie=cookie, context="Pixiv 当前账号")
body = payload.get("body") if isinstance(payload, dict) else {}
user_id = _first_text(body, "userId", "id")
if user_id:
return user_id
except Exception:
continue
try:
response = session.get("https://www.pixiv.net/", timeout=30)
response.raise_for_status()
match = re.search(r'"userData"\s*:\s*\{[^}]*"id"\s*:\s*"?(?P<id>\d+)"?', response.text)
if match:
return match.group("id")
except Exception:
pass
raise ValueError("Pixiv Cookie 可能已失效:无法识别当前登录账号。")
def _fetch_pixiv_following(
self,
session: Any,
cookie: str,
user_id: str,
result: OriginFollowSyncResult,
*,
progress: ProgressCallback | None = None,
) -> list[OriginFollow]:
follows: list[OriginFollow] = []
seen: set[str] = set()
limit = 100
offset = 0
page = 1
while True:
self._emit(result, progress, {"platform": "pixiv", "page": page, "message": f"正在同步第 {page} 页"})
url = (
f"https://www.pixiv.net/ajax/user/{user_id}/following"
f"?offset={offset}&limit={limit}&rest=show&tag=&acceptingRequests=0&lang=zh"
)
try:
payload = request_json(
session,
url,
cookie=cookie,
referer=f"https://www.pixiv.net/users/{user_id}/following",
context=f"Pixiv 关注列表第 {page} 页",
)
except Exception as exc:
if page == 1:
raise ValueError("Pixiv Cookie 可能已失效:无法读取关注列表。") from exc
result.failed += 1
result.errors.append({"id": f"page:{page}", "reason": str(exc)})
break
users = _extract_pixiv_users(payload)
result.pages = page
if not users:
break
for item in users:
follow = _pixiv_follow_from_payload(item)
if follow is None:
result.failed += 1
result.errors.append({"id": "unknown", "reason": "Pixiv 作者记录缺少 userId"})
continue
if follow.external_id in seen:
result.skipped += 1
continue
seen.add(follow.external_id)
follows.append(follow)
if len(users) < limit:
break
offset += limit
page += 1
return follows
async def _fetch_x_following(
self,
plugin: Any,
result: OriginFollowSyncResult,
*,
progress: ProgressCallback | None = None,
) -> list[OriginFollow]:
from plugins.x import selectors
from plugins.x.playwright_client import XPlaywrightClient
client = XPlaywrightClient(plugin.config)
follows: list[OriginFollow] = []
seen: set[str] = set()
try:
await client.start()
page = await client.new_page_goto("https://x.com/home")
try:
await client.raise_if_guest_wall(page)
profile_href = await page.locator(selectors.PROFILE_LINK).first.get_attribute("href", timeout=8000)
username = _x_username_from_href(profile_href or "")
if not username:
raise ValueError("X 未登录:请先在插件管理里完成 X 登录。")
await page.goto(f"https://x.com/{username}{selectors.FOLLOWING_URL_SUFFIX}", wait_until="domcontentloaded")
await client.raise_if_guest_wall(page)
stale_rounds = 0
page_no = 1
while stale_rounds < 4 and page_no <= 80:
self._emit(result, progress, {"platform": "x", "page": page_no, "message": f"正在同步第 {page_no} 页"})
added = await _collect_x_user_cells(page, seen, follows)
result.pages = page_no
stale_rounds = stale_rounds + 1 if added == 0 else 0
await client.human_scroll_page(page)
page_no += 1
if not follows:
raise ValueError("X 未读取到 following 列表,请确认账号已登录且 following 页面可访问。")
finally:
await page.close()
finally:
await client.close()
return follows
def _upsert_pixiv_local(self, follows: list[OriginFollow], result: OriginFollowSyncResult) -> None:
library = PixivLibrary(self.db_path)
try:
for follow in follows:
try:
status = library.upsert_origin_followed_artist(
int(follow.external_id),
name=follow.name,
account=follow.handle,
avatar_url=follow.avatar_url,
)
_apply_local_status(result, status)
except Exception as exc:
result.failed += 1
result.errors.append({"id": follow.external_id, "reason": str(exc)})
finally:
library.close()
def _upsert_x_local(self, follows: list[OriginFollow], result: OriginFollowSyncResult) -> None:
rows = self.x_media_service._load_follow_rows()
now = _now_text()
by_handle = {str(item.get("username", "")).lower(): item for item in rows}
for follow in follows:
handle = _normalize_username(follow.handle or follow.external_id)
if not handle:
result.failed += 1
result.errors.append({"id": follow.external_id, "reason": "X 用户名为空"})
continue
row = by_handle.get(handle.lower())
status = "created"
if row is None:
row = {
"username": handle,
"creator_name": follow.name or handle,
"enabled": True,
"profile_url": follow.profile_url or f"https://x.com/{handle}",
"avatar_url": follow.avatar_url,
"latest_tweet_id": "",
"latest_published_at": "",
"last_checked_at": now,
"last_error": "",
"seen_tweet_ids": [],
"created_at": now,
"updated_at": now,
}
rows.append(row)
by_handle[handle.lower()] = row
else:
changed = (
not bool(row.get("enabled", True))
or (follow.name and follow.name != str(row.get("creator_name") or ""))
or (follow.avatar_url and follow.avatar_url != str(row.get("avatar_url") or ""))
)
status = "updated" if changed else "skipped"
row["username"] = handle
row["creator_name"] = follow.name or str(row.get("creator_name") or handle)
row["enabled"] = True
row["profile_url"] = follow.profile_url or f"https://x.com/{handle}"
row["avatar_url"] = follow.avatar_url or str(row.get("avatar_url") or "")
row["last_checked_at"] = now
row["last_error"] = ""
row["updated_at"] = now
_apply_local_status(result, status)
self.x_media_service._save_follow_rows(rows)
def _apply_local_status(result: OriginFollowSyncResult, status: str) -> None:
if status == "created":
result.created += 1
elif status == "updated":
result.updated += 1
else:
result.skipped += 1
def _extract_pixiv_users(payload: dict[str, Any]) -> list[dict[str, Any]]:
body = payload.get("body") if isinstance(payload, dict) else payload
if isinstance(body, list):
return [item for item in body if isinstance(item, dict)]
if not isinstance(body, dict):
return []
candidates = (
body.get("users"),
body.get("followUsers"),
body.get("userPreviews"),
body.get("thumbnails", {}).get("user") if isinstance(body.get("thumbnails"), dict) else None,
)
for candidate in candidates:
if isinstance(candidate, list):
return [item for item in candidate if isinstance(item, dict)]
return []
def _pixiv_follow_from_payload(item: dict[str, Any]) -> OriginFollow | None:
user_id = _first_text(item, "userId", "user_id", "id")
if not user_id:
user = item.get("user") if isinstance(item.get("user"), dict) else {}
user_id = _first_text(user, "userId", "user_id", "id")
item = {**item, **user}
if not user_id:
return None
name = _first_text(item, "name", "userName", "displayName")
handle = _first_text(item, "userAccount", "account", "screenName")
avatar = _first_text(item, "imageBig", "image", "profileImageUrl", "profile_image_url")
return OriginFollow(
platform="pixiv",
external_id=str(user_id),
name=name,
handle=handle,
avatar_url=avatar,
profile_url=f"https://www.pixiv.net/users/{user_id}",
)
async def _collect_x_user_cells(page: Any, seen: set[str], follows: list[OriginFollow]) -> int:
from plugins.x import selectors
added = 0
cells = await page.locator(selectors.USER_CELL).all()
for cell in cells:
try:
data = await cell.evaluate(
"""(node) => {
const text = node.innerText || "";
const links = Array.from(node.querySelectorAll('a[href^="/"]')).map(a => a.getAttribute("href") || "");
const img = node.querySelector('img[src*="profile_images"]');
return { text, links, avatar: img ? img.getAttribute("src") : "" };
}"""
)
handle = _x_handle_from_cell(data)
if not handle or handle.lower() in seen:
continue
seen.add(handle.lower())
follows.append(
OriginFollow(
platform="x",
external_id=handle,
name=_x_display_name_from_cell(data, handle),
handle=handle,
avatar_url=str(data.get("avatar") or ""),
profile_url=f"https://x.com/{handle}",
)
)
added += 1
except Exception:
continue
return added
def _x_handle_from_cell(data: dict[str, Any]) -> str:
for href in data.get("links") or []:
username = _x_username_from_href(str(href))
if username:
return username
text = str(data.get("text") or "")
match = re.search(r"@([A-Za-z0-9_]{1,15})", text)
return match.group(1) if match else ""
def _x_display_name_from_cell(data: dict[str, Any], handle: str) -> str:
text = str(data.get("text") or "")
for line in [item.strip() for item in text.splitlines() if item.strip()]:
if not line.startswith("@") and line.lower() not in {"follow", "following"}:
return line
return handle
def _x_username_from_href(href: str) -> str:
match = re.match(r"^/?([A-Za-z0-9_]{1,15})(?:/)?$", href.strip())
if not match:
return ""
username = match.group(1)
if username.lower() in {"home", "explore", "notifications", "messages", "settings", "i"}:
return ""
return username
def _first_text(payload: Any, *keys: str) -> str:
if not isinstance(payload, dict):
return ""
for key in keys:
value = payload.get(key)
if value is not None and str(value).strip():
return str(value).strip()
return ""
def _now_text() -> str:
from datetime import datetime, timezone
return datetime.now(timezone.utc).isoformat()
+113
View File
@@ -0,0 +1,113 @@
from __future__ import annotations
import os
import shutil
import sys
from pathlib import Path
APP_NAME = "SakuraDownloader"
LEGACY_APP_NAME = "PixivDownloader"
def is_frozen() -> bool:
return bool(getattr(sys, "frozen", False))
def resource_root() -> Path:
if is_frozen():
return Path(getattr(sys, "_MEIPASS")).resolve()
return Path(__file__).resolve().parents[2]
def executable_dir() -> Path:
if is_frozen():
return Path(sys.executable).resolve().parent
return resource_root()
def data_root() -> Path:
override = os.environ.get("SAKURA_APP_DATA_DIR") or os.environ.get("PIXIV_APP_DATA_DIR")
if override:
root = Path(override)
elif is_frozen():
local_appdata = os.environ.get("LOCALAPPDATA")
root = Path(local_appdata) / APP_NAME if local_appdata else executable_dir() / "data"
legacy_root = Path(local_appdata) / LEGACY_APP_NAME if local_appdata else None
if legacy_root and legacy_root.exists() and not root.exists():
root.mkdir(parents=True, exist_ok=True)
for name in ("pixiv_app_session.json", "runtime", "profiles", "plugins"):
src = legacy_root / name
dst = root / name
if src.exists() and not dst.exists():
if src.is_dir():
import shutil
shutil.copytree(src, dst)
else:
dst.write_bytes(src.read_bytes())
else:
root = resource_root()
root.mkdir(parents=True, exist_ok=True)
return root
def runtime_path(*parts: str) -> Path:
path = data_root() / "runtime"
path.mkdir(parents=True, exist_ok=True)
return path.joinpath(*parts)
def profiles_root() -> Path:
path = data_root() / "profiles"
path.mkdir(parents=True, exist_ok=True)
return path
def plugins_root() -> Path:
return resource_root() / "plugins"
def external_plugins_root() -> Path:
"""Plugins shipped beside the executable, outside the PyInstaller bundle."""
return executable_dir() / "plugins"
def user_plugins_root() -> Path:
path = data_root() / "plugins"
path.mkdir(parents=True, exist_ok=True)
return path
def plugin_roots() -> list[Path]:
roots = [plugins_root(), external_plugins_root(), user_plugins_root()]
result: list[Path] = []
for root in roots:
if root not in result:
result.append(root)
return result
def webui_root() -> Path:
return resource_root() / "pixiv_app" / "webui"
def app_session_file() -> Path:
return data_root() / "pixiv_app_session.json"
def app_library_file() -> Path:
"""Persistent local library DB, writable even when the EXE directory is read-only."""
target = data_root() / "pixiv_app_library.db"
if is_frozen() and not target.exists():
legacy = executable_dir() / "pixiv_app_library.db"
if legacy.is_file():
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(legacy, target)
return target
def downloads_root() -> Path:
path = executable_dir() / "Sakura_Downloads"
path.mkdir(parents=True, exist_ok=True)
return path
+69
View File
@@ -0,0 +1,69 @@
from __future__ import annotations
import os
from pathlib import Path
def playwright_browsers_root() -> Path | None:
local_appdata = os.environ.get("LOCALAPPDATA", "").strip()
if not local_appdata:
return None
return Path(local_appdata) / "ms-playwright"
def configure_playwright_runtime_env() -> Path | None:
root = playwright_browsers_root()
if root is None:
return None
if root.exists() and "PLAYWRIGHT_BROWSERS_PATH" not in os.environ:
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = str(root)
return root
def preferred_chromium_executable() -> Path | None:
root = configure_playwright_runtime_env()
if root is None or not root.exists():
return None
candidates: list[tuple[int, float, Path]] = []
for browser_dir in root.glob("chromium-*"):
if not browser_dir.is_dir():
continue
revision = _parse_revision(browser_dir.name)
for relative in (
Path("chrome-win64") / "chrome.exe",
Path("chrome-win") / "chrome.exe",
):
executable = browser_dir / relative
if executable.is_file():
try:
mtime = executable.stat().st_mtime
except OSError:
mtime = 0.0
candidates.append((revision, mtime, executable))
break
if not candidates:
return None
candidates.sort(key=lambda item: (item[0], item[1]), reverse=True)
return candidates[0][2]
def explain_missing_browser(exc: Exception, *, feature_name: str) -> str:
text = str(exc)
lowered = text.lower()
if "executable doesn't exist" not in lowered and "browsertype.launch_persistent_context" not in lowered:
return text
return (
f"{feature_name} 需要 Playwright Chromium 浏览器,但当前系统里没有可用的浏览器文件。\n"
"请先运行同目录下的 install_playwright_chromium.bat,安装完成后重新打开程序再试。"
)
def _parse_revision(name: str) -> int:
try:
return int(name.rsplit("-", 1)[-1])
except (TypeError, ValueError):
return 0
+24
View File
@@ -0,0 +1,24 @@
"""Plugin system: base types, manager, generator, optional market client."""
from pixiv_app.core.plugin.base import (
BasePlugin,
PluginAuthError,
PluginDownloadError,
PluginError,
PluginManifest,
PluginParseError,
Resource,
)
from pixiv_app.core.plugin.manager import PluginEvent, PluginManager
__all__ = [
"BasePlugin",
"PluginAuthError",
"PluginDownloadError",
"PluginError",
"PluginEvent",
"PluginManager",
"PluginManifest",
"PluginParseError",
"Resource",
]
+119
View File
@@ -0,0 +1,119 @@
"""
Multi-site plugin core abstractions.
解析与下载采用同步接口,便于与现有 requests / 线程模型集成;
若站点 SDK 仅提供 asyncio,可在插件内部使用 asyncio.run 包装。
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
@dataclass
class Resource:
"""Unified resource model returned by plugins."""
id: str
url: str
title: str
author: str
author_id: str
files: list[dict[str, Any]]
metadata: dict[str, Any] = field(default_factory=dict)
thumbnail: str | None = None
created_at: str | None = None
@dataclass
class PluginManifest:
"""Optional manifest shipped as manifest.json next to plugin.py."""
name: str
version: str
domain: str
author: str = ""
description: str = ""
requires_auth: bool = False
min_platform_version: str = "1.0.0"
tags: list[str] = field(default_factory=list)
dependencies: list[str] = field(default_factory=list)
class PluginError(Exception):
"""Base class for plugin failures."""
class PluginAuthError(PluginError):
"""Authentication or authorization failed."""
class PluginParseError(PluginError):
"""URL or API response could not be parsed."""
class PluginDownloadError(PluginError):
"""Download or filesystem error."""
class BasePlugin(ABC):
"""Interface implemented by every site plugin."""
@property
@abstractmethod
def domain(self) -> str:
"""Primary domain, e.g. pixiv.net."""
@property
@abstractmethod
def name(self) -> str:
"""Human-readable plugin name."""
@property
@abstractmethod
def version(self) -> str:
"""Semantic version string."""
@abstractmethod
def can_handle(self, url: str) -> bool:
"""Return True if this plugin should process the URL or raw token."""
@abstractmethod
def parse(self, url: str) -> list[Resource]:
"""Resolve URL into one or more downloadable resources."""
@abstractmethod
def download(self, resource: Resource, save_path: Path) -> list[Path]:
"""Download files for ``resource`` under ``save_path``."""
@abstractmethod
def get_headers(self) -> dict[str, str]:
"""HTTP headers for requests (Cookie, Bearer token, etc.)."""
def validate(self) -> bool:
"""Optional self-check (credentials present, deps installed)."""
return True
def get_config_schema(self) -> dict[str, Any]:
"""Optional JSON-schema-like dict for dynamic GUI forms."""
return {}
def load_config(self) -> dict[str, Any]:
"""Override to load persisted plugin settings."""
return {}
def save_config(self, data: dict[str, Any]) -> None:
"""Override to persist plugin settings."""
__all__ = [
"BasePlugin",
"PluginAuthError",
"PluginDownloadError",
"PluginError",
"PluginManifest",
"PluginParseError",
"Resource",
]
+69
View File
@@ -0,0 +1,69 @@
from __future__ import annotations
import re
from datetime import datetime
from pathlib import Path
from typing import Any
from jinja2 import Environment, FileSystemLoader, select_autoescape
def _class_name_from_plugin_name(name: str) -> str:
parts = re.split(r"[\s_]+", name.strip())
stem = "".join(p[:1].upper() + p[1:].lower() if p else "" for p in parts if p)
return f"{stem}Plugin" if stem else "GeneratedPlugin"
class PluginGenerator:
"""Generate a skeleton plugin directory from Jinja2 templates."""
def __init__(self, template_dir: str | Path | None = None) -> None:
base = Path(__file__).resolve().parent / "templates"
self.template_dir = Path(template_dir) if template_dir else base
self.env = Environment(
loader=FileSystemLoader(str(self.template_dir)),
autoescape=select_autoescape(enabled_extensions=()),
)
def generate(
self,
name: str,
domain: str,
author: str,
description: str,
*,
output_root: str | Path = "plugins",
) -> Path:
slug = name.strip().lower().replace(" ", "_")
plugin_dir = Path(output_root) / slug
plugin_dir.mkdir(parents=True, exist_ok=True)
context: dict[str, Any] = {
"name": name.strip(),
"domain": domain.strip(),
"author": author.strip(),
"description": description.strip(),
"generated_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"class_name": _class_name_from_plugin_name(name),
}
py_template = self.env.get_template("base_plugin.py.jinja")
(plugin_dir / "plugin.py").write_text(py_template.render(**context), encoding="utf-8")
manifest_template = self.env.get_template("manifest.json.jinja")
(plugin_dir / "manifest.json").write_text(manifest_template.render(**context), encoding="utf-8")
init_file = plugin_dir / "__init__.py"
if not init_file.exists():
init_file.write_text('"""Generated plugin."""\n', encoding="utf-8")
return plugin_dir.resolve()
def generate_from_gui(self, form_data: dict[str, str], *, output_root: str | Path = "plugins") -> Path:
return self.generate(
name=form_data["name"],
domain=form_data["domain"],
author=form_data["author"],
description=form_data["description"],
output_root=output_root,
)
+194
View File
@@ -0,0 +1,194 @@
from __future__ import annotations
import importlib.util
import json
import logging
import sys
from collections import defaultdict
from enum import Enum
from pathlib import Path
from typing import Any, Callable
from pixiv_app.core.plugin.base import BasePlugin, PluginManifest
logger = logging.getLogger(__name__)
class PluginEvent(str, Enum):
BEFORE_LOAD = "before_load"
AFTER_LOAD = "after_load"
BEFORE_UNLOAD = "before_unload"
AFTER_UNLOAD = "after_unload"
ERROR = "error"
class PluginManager:
"""Discover and load plugins from filesystem directories."""
def __init__(self, plugin_dirs: list[str | Path]) -> None:
self.plugin_dirs = [Path(d).resolve() for d in plugin_dirs]
self.plugins: dict[str, BasePlugin] = {}
self.manifests: dict[str, PluginManifest] = {}
self.loaded_modules: dict[str, Any] = {}
self._paths: dict[str, Path] = {}
self.load_errors: dict[Path, str] = {}
self.event_handlers: dict[PluginEvent, list[Callable[..., None]]] = defaultdict(list)
def on_event(self, event: PluginEvent, callback: Callable[..., None]) -> None:
self.event_handlers[event].append(callback)
def _emit(self, event: PluginEvent, *args: Any, **kwargs: Any) -> None:
for cb in self.event_handlers.get(event, []):
try:
cb(*args, **kwargs)
except Exception as exc:
logger.exception("Plugin event handler failed: %s", exc)
def discover(self) -> list[Path]:
"""Return paths to plugin.py files."""
found: list[Path] = []
for root in self.plugin_dirs:
if not root.is_dir():
continue
for subdir in sorted(root.iterdir()):
if not subdir.is_dir():
continue
candidate = subdir / "plugin.py"
if candidate.is_file():
found.append(candidate)
return found
def _load_manifest(self, plugin_dir: Path) -> PluginManifest | None:
manifest_path = plugin_dir / "manifest.json"
if not manifest_path.is_file():
return None
try:
data = json.loads(manifest_path.read_text(encoding="utf-8"))
return PluginManifest(
name=str(data.get("name", plugin_dir.name)),
version=str(data.get("version", "0.0.0")),
domain=str(data.get("domain", "")),
author=str(data.get("author", "")),
description=str(data.get("description", "")),
requires_auth=bool(data.get("requires_auth", False)),
min_platform_version=str(data.get("min_platform_version", "1.0.0")),
tags=list(data.get("tags") or []),
dependencies=list(data.get("dependencies") or []),
)
except Exception as exc:
logger.warning("Invalid manifest %s: %s", manifest_path, exc)
return None
def load_plugin(self, plugin_path: Path) -> BasePlugin | None:
"""Load a single plugin from ``.../plugin.py``."""
plugin_path = plugin_path.resolve()
plugin_dir = plugin_path.parent
module_name = f"sakura_dynamic_plugin_{plugin_dir.name}"
module_stem = module_name.rsplit(".", 1)[0]
for loaded_name in list(sys.modules):
if loaded_name == module_name or loaded_name.startswith(f"{module_name}."):
del sys.modules[loaded_name]
self.load_errors.pop(plugin_path, None)
self._emit(PluginEvent.BEFORE_LOAD, plugin_path)
spec = importlib.util.spec_from_file_location(
module_name,
plugin_path,
submodule_search_locations=[str(plugin_dir)],
)
if spec is None or spec.loader is None:
self._emit(PluginEvent.ERROR, plugin_path, "invalid spec")
self.load_errors[plugin_path] = "invalid spec"
return None
module = importlib.util.module_from_spec(spec)
module.__package__ = module_stem
module.__file__ = str(plugin_path)
sys.modules[module_name] = module
try:
spec.loader.exec_module(module)
except Exception as exc:
logger.exception("Failed to load %s", plugin_path)
self._emit(PluginEvent.ERROR, plugin_path, exc)
self.load_errors[plugin_path] = f"{type(exc).__name__}: {exc}"
return None
plugin_class = getattr(module, "plugin_class", None)
if plugin_class is None:
logger.error("Module %s has no plugin_class export", plugin_path)
self._emit(PluginEvent.ERROR, plugin_path, "missing plugin_class")
self.load_errors[plugin_path] = "missing plugin_class"
return None
try:
plugin = plugin_class()
except Exception as exc:
logger.exception("Failed to instantiate plugin from %s", plugin_path)
self._emit(PluginEvent.ERROR, plugin_path, exc)
self.load_errors[plugin_path] = f"{type(exc).__name__}: {exc}"
return None
if not isinstance(plugin, BasePlugin):
logger.error("plugin_class is not BasePlugin: %s", plugin_path)
self.load_errors[plugin_path] = "plugin_class is not BasePlugin"
return None
key = plugin.name
manifest = self._load_manifest(plugin_dir)
if manifest:
self.manifests[key] = manifest
self.plugins[key] = plugin
self.loaded_modules[key] = module
self._paths[key] = plugin_path
self.load_errors.pop(plugin_path, None)
self._emit(PluginEvent.AFTER_LOAD, plugin)
return plugin
def load_all(self) -> dict[str, BasePlugin]:
for path in self.discover():
try:
self.load_plugin(path)
except Exception as exc:
logger.exception("Failed during load_all %s: %s", path, exc)
return self.plugins
def reload_all_from_disk(self) -> None:
"""Unload every plugin entry then scan directories again."""
for name in list(self.plugins.keys()):
self.unload_plugin(name)
self.load_errors.clear()
self.load_all()
def reload_plugin(self, name: str) -> bool:
path = self._paths.get(name)
if path is None:
return False
self.unload_plugin(name)
return self.load_plugin(path) is not None
def get_plugin_path(self, name: str) -> Path | None:
return self._paths.get(name)
def unload_plugin(self, name: str) -> None:
self._emit(PluginEvent.BEFORE_UNLOAD, name)
self.plugins.pop(name, None)
mod = self.loaded_modules.pop(name, None)
self.manifests.pop(name, None)
self._paths.pop(name, None)
if mod is not None and getattr(mod, "__name__", None) in sys.modules:
del sys.modules[mod.__name__]
self._emit(PluginEvent.AFTER_UNLOAD, name)
def get_plugin_for_url(self, url: str) -> BasePlugin | None:
for plugin in self.plugins.values():
if plugin.can_handle(url):
return plugin
return None
def check_dependencies(self, manifest: PluginManifest) -> bool:
for dep in manifest.dependencies:
if dep not in self.plugins:
return False
return True
+29
View File
@@ -0,0 +1,29 @@
"""
Optional plugin market client — stub for future HTTP catalog integration.
"""
from __future__ import annotations
from typing import Any
import requests
class PluginMarketClient:
def __init__(self, api_url: str = "") -> None:
self.api_url = api_url.rstrip("/")
def list_plugins(self) -> list[dict[str, Any]]:
if not self.api_url:
return []
response = requests.get(f"{self.api_url}/plugins", timeout=15)
response.raise_for_status()
data = response.json()
return list(data.get("plugins", []))
def install_plugin(self, _plugin_name: str) -> bool:
"""Download zip and extract into plugins/ — not implemented."""
return False
def check_updates(self) -> list[dict[str, Any]]:
return []
@@ -0,0 +1,31 @@
"""
{{ name }} plugin — auto-generated at {{ generated_at }}.
"""
from __future__ import annotations
from pathlib import Path
from typing import Any
from pixiv_app.core.plugin.base import BasePlugin, Resource
class {{ class_name }}(BasePlugin):
domain = "{{ domain }}"
name = "{{ name }}"
version = "1.0.0"
def can_handle(self, url: str) -> bool:
return "{{ domain }}" in url.lower()
def parse(self, url: str) -> list[Resource]:
raise NotImplementedError("Implement URL parsing and build Resource list.")
def download(self, resource: Resource, save_path: Path) -> list[Path]:
raise NotImplementedError("Download files for resource into save_path.")
def get_headers(self) -> dict[str, str]:
return {"User-Agent": "Mozilla/5.0", "Accept": "*/*"}
plugin_class = {{ class_name }}
@@ -0,0 +1,11 @@
{
"name": "{{ name }}",
"version": "1.0.0",
"domain": "{{ domain }}",
"author": "{{ author }}",
"description": "{{ description }}",
"requires_auth": false,
"tags": ["general"],
"min_platform_version": "1.0.0",
"dependencies": []
}
+292
View File
@@ -0,0 +1,292 @@
from __future__ import annotations
import random
import re
import threading
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass
from typing import Any, Optional
import requests
@dataclass
class ProxyInfo:
ip: str
port: int
protocol: str = "http"
source: str = "manual"
location: str = ""
org: str = ""
last_check: float = 0.0
success_count: int = 0
fail_count: int = 0
@property
def proxy_url(self) -> str:
return f"{self.protocol}://{self.ip}:{self.port}"
@property
def is_alive(self) -> bool:
return self.success_count > 0 or self.fail_count < 3
def parse_proxy_text(text: str) -> list[ProxyInfo]:
proxies: list[ProxyInfo] = []
seen: set[tuple[str, int, str]] = set()
for raw_line in text.splitlines():
line = raw_line.strip()
if not line or line.startswith("#"):
continue
if "://" not in line:
line = f"http://{line}"
match = re.match(r"^(https?)://([\w\.\-]+):(\d{2,5})$", line, re.IGNORECASE)
if not match:
continue
protocol, ip, port_text = match.groups()
key = (ip, int(port_text), protocol.lower())
if key in seen:
continue
seen.add(key)
proxies.append(ProxyInfo(ip=ip, port=int(port_text), protocol=protocol.lower(), source="manual"))
return proxies
class QuakeClient:
def __init__(
self,
api_key: str = "",
cookie: str = "",
mode: str = "api_v3",
request_kwargs: Optional[dict[str, Any]] = None,
):
self.api_key = api_key.strip()
self.cookie = cookie.strip()
self.mode = mode
self.request_kwargs = dict(request_kwargs or {})
self.base_url = "https://quake.360.net/api/v3"
self.web_assoc_url = "https://quake.360.net/api/search/field/association/quake_service"
def _api_headers(self) -> dict[str, str]:
return {
"X-QuakeToken": self.api_key,
"Content-Type": "application/json",
}
def _web_headers(self) -> dict[str, str]:
headers = {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"Origin": "https://quake.360.net",
"Referer": "https://quake.360.net/quake/",
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
),
}
if self.cookie:
headers["Cookie"] = self.cookie
return headers
def search_ips_api(self, query: str, size: int = 100, start: int = 0) -> list[dict[str, Any]]:
if not self.api_key:
return []
url = f"{self.base_url}/search/quake_service"
payload = {"query": query, "start": start, "size": min(size, 500)}
kwargs = dict(self.request_kwargs)
kwargs.setdefault("timeout", 30)
try:
response = requests.post(url, headers=self._api_headers(), json=payload, **kwargs)
if response.status_code != 200:
return []
body = response.json()
return body.get("data", []) if isinstance(body, dict) else []
except Exception:
return []
def search_ips_web_assoc(self, search_content: str) -> list[dict[str, Any]]:
if not self.cookie:
return []
payload = {
"search_content": search_content,
"device": {
"device_type": "PC",
"os": "Windows",
"os_version": "10.0",
"language": "zh_CN",
"network": "4g",
"browser_info": "Chrome",
"fingerprint": "pixiv-proxy-pool",
"user_agent": self._web_headers()["User-Agent"],
"date": time.strftime("%Y/%m/%d %H:%M:%S"),
"UUID": "pixiv-proxy-association",
},
}
kwargs = dict(self.request_kwargs)
kwargs.setdefault("timeout", 30)
try:
response = requests.post(
self.web_assoc_url,
headers=self._web_headers(),
json=payload,
**kwargs,
)
if response.status_code != 200:
return []
text = response.text
ips = sorted(set(re.findall(r"\b(?:\d{1,3}\.){3}\d{1,3}\b", text)))
return [{"ip": ip, "port": 80, "organization": "", "location": {}} for ip in ips]
except Exception:
return []
def get_foreign_proxies(
self,
size: int = 120,
countries: Optional[list[str]] = None,
) -> list[ProxyInfo]:
proxy_ports = [80, 443, 8080, 3128, 8888, 1080, 8118]
port_query = " OR ".join([f"port:{port}" for port in proxy_ports])
country_codes = countries or ["US", "JP", "KR", "SG", "HK", "TW", "DE", "FR", "GB", "CA", "AU"]
country_query = " OR ".join([f'country:"{code}"' for code in country_codes])
query = f"({port_query}) AND ({country_query}) AND response:ok"
if self.mode == "web_assoc":
data = self.search_ips_web_assoc(query)
else:
data = self.search_ips_api(query=query, size=size)
proxies: list[ProxyInfo] = []
seen: set[tuple[str, int, str]] = set()
for item in data:
if not isinstance(item, dict):
continue
ip = item.get("ip")
port = item.get("port")
if not ip or not isinstance(port, int):
continue
protocol = "https" if port in (443, 8443) else "http"
key = (ip, port, protocol)
if key in seen:
continue
seen.add(key)
location_obj = item.get("location", {}) if isinstance(item.get("location"), dict) else {}
location = "/".join(
[str(location_obj.get("country", "")).strip(), str(location_obj.get("province", "")).strip()]
).strip("/")
proxies.append(
ProxyInfo(
ip=ip,
port=port,
protocol=protocol,
source=f"quake:{self.mode}",
location=location,
org=str(item.get("organization", "") or item.get("org", "")),
)
)
return proxies
class ProxyPool:
def __init__(self, test_url: str = "https://httpbin.org/ip", test_timeout: int = 6):
self.proxies: list[ProxyInfo] = []
self.working_proxies: list[ProxyInfo] = []
self.current_index = 0
self.test_url = test_url
self.test_timeout = test_timeout
self.session = requests.Session()
self.session.headers.update({"User-Agent": "SakuraDownloaderProxyPool/1.0"})
self._lock = threading.Lock()
def add_proxies(self, proxies: list[ProxyInfo]) -> None:
with self._lock:
existing = {(p.ip, p.port, p.protocol) for p in self.proxies}
for proxy in proxies:
key = (proxy.ip, proxy.port, proxy.protocol)
if key not in existing:
existing.add(key)
self.proxies.append(proxy)
def verify_proxy(self, proxy: ProxyInfo) -> bool:
target_proxies = {"http": proxy.proxy_url, "https": proxy.proxy_url}
try:
response = self.session.get(
self.test_url,
proxies=target_proxies,
timeout=self.test_timeout,
verify=False,
)
if response.status_code == 200:
with self._lock:
proxy.last_check = time.time()
proxy.success_count += 1
proxy.fail_count = 0
return True
except Exception:
pass
with self._lock:
proxy.fail_count += 1
return False
def verify_all(self, max_workers: int = 16, max_proxies: int = 80) -> list[ProxyInfo]:
with self._lock:
to_verify = list(self.proxies[:max_proxies])
self.working_proxies = []
if not to_verify:
return self.working_proxies
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_map = {executor.submit(self.verify_proxy, proxy): proxy for proxy in to_verify}
for future in as_completed(future_map):
proxy = future_map[future]
try:
if future.result():
with self._lock:
self.working_proxies.append(proxy)
except Exception:
continue
with self._lock:
return list(self.working_proxies)
def collect_until_target(
self,
quake_client: QuakeClient,
target_working: int = 5,
stable_rounds: int = 2,
fetch_batch_size: int = 60,
verify_batch_size: int = 30,
max_rounds: int = 6,
max_workers: int = 16,
countries: Optional[list[str]] = None,
) -> list[ProxyInfo]:
stable_hits = 0
for _ in range(max_rounds):
if stable_hits >= stable_rounds:
break
new_items = quake_client.get_foreign_proxies(size=fetch_batch_size, countries=countries)
if new_items:
self.add_proxies(new_items)
self.verify_all(max_workers=max_workers, max_proxies=verify_batch_size)
with self._lock:
working_count = len(self.working_proxies)
if working_count >= target_working:
stable_hits += 1
else:
stable_hits = 0
with self._lock:
return list(self.working_proxies)
def get_proxy(self, rotate: bool = True) -> Optional[ProxyInfo]:
with self._lock:
if not self.working_proxies:
return None
if not rotate:
return random.choice(self.working_proxies)
proxy = self.working_proxies[self.current_index % len(self.working_proxies)]
self.current_index += 1
return proxy
def mark_failed(self, proxy: ProxyInfo) -> None:
with self._lock:
proxy.fail_count += 1
if proxy.fail_count >= 3 and proxy in self.working_proxies:
self.working_proxies.remove(proxy)
+145
View File
@@ -0,0 +1,145 @@
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from pixiv_app.core.library import DEFAULT_LIBRARY_DB, PixivLibrary
from pixiv_app.core.paths import runtime_path
DEFAULT_THUMBNAIL_DIR = runtime_path("thumbnails")
SUPPORTED_IMAGE_SUFFIXES = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif"}
@dataclass
class ThumbnailResult:
artwork_id: int
page_index: int
source_path: str
thumbnail_path: str
width: int
height: int
generated: bool
class ThumbnailCache:
def __init__(
self,
*,
db_path: str | Path = DEFAULT_LIBRARY_DB,
cache_dir: str | Path = DEFAULT_THUMBNAIL_DIR,
size: tuple[int, int] = (360, 360),
quality: int = 86,
) -> None:
self.db_path = Path(db_path)
self.cache_dir = Path(cache_dir)
self.size = size
self.quality = quality
self.cache_dir.mkdir(parents=True, exist_ok=True)
def ensure_for_artwork(self, artwork_id: int) -> list[ThumbnailResult]:
library = PixivLibrary(self.db_path)
try:
rows = library.connection.execute(
"""
SELECT artwork_id, page_index, file_path
FROM artwork_files
WHERE artwork_id = ?
ORDER BY page_index ASC
""",
(artwork_id,),
).fetchall()
return [result for row in rows if (result := self.ensure_for_file(library, row)) is not None]
finally:
library.close()
def build_missing(self, *, limit: int = 200) -> list[ThumbnailResult]:
library = PixivLibrary(self.db_path)
try:
rows = library.connection.execute(
"""
SELECT f.artwork_id, f.page_index, f.file_path
FROM artwork_files f
LEFT JOIN thumbnail_cache t
ON t.artwork_id = f.artwork_id AND t.page_index = f.page_index
WHERE t.thumbnail_path IS NULL
OR t.source_path != f.file_path
ORDER BY f.updated_at DESC
LIMIT ?
""",
(limit,),
).fetchall()
return [result for row in rows if (result := self.ensure_for_file(library, row)) is not None]
finally:
library.close()
def ensure_for_file(self, library: PixivLibrary, row) -> ThumbnailResult | None:
source_path = Path(str(row["file_path"]))
if source_path.suffix.lower() not in SUPPORTED_IMAGE_SUFFIXES or not source_path.exists():
return None
stat = source_path.stat()
thumbnail_path = self._thumbnail_path(int(row["artwork_id"]), int(row["page_index"]), source_path)
cached = library.connection.execute(
"""
SELECT thumbnail_path, width, height, source_mtime, source_size
FROM thumbnail_cache
WHERE artwork_id = ? AND page_index = ?
""",
(int(row["artwork_id"]), int(row["page_index"])),
).fetchone()
if (
cached
and Path(str(cached["thumbnail_path"])).exists()
and float(cached["source_mtime"]) == stat.st_mtime
and int(cached["source_size"]) == stat.st_size
):
return ThumbnailResult(
artwork_id=int(row["artwork_id"]),
page_index=int(row["page_index"]),
source_path=str(source_path),
thumbnail_path=str(cached["thumbnail_path"]),
width=int(cached["width"]),
height=int(cached["height"]),
generated=False,
)
width, height = self._generate(source_path, thumbnail_path)
library.upsert_thumbnail(
artwork_id=int(row["artwork_id"]),
page_index=int(row["page_index"]),
source_path=str(source_path),
thumbnail_path=str(thumbnail_path),
width=width,
height=height,
source_mtime=stat.st_mtime,
source_size=stat.st_size,
)
return ThumbnailResult(
artwork_id=int(row["artwork_id"]),
page_index=int(row["page_index"]),
source_path=str(source_path),
thumbnail_path=str(thumbnail_path),
width=width,
height=height,
generated=True,
)
def _generate(self, source_path: Path, thumbnail_path: Path) -> tuple[int, int]:
try:
from PIL import Image, ImageOps
except ImportError as exc:
raise RuntimeError("Pillow is required to generate thumbnails. Install it with: pip install Pillow") from exc
thumbnail_path.parent.mkdir(parents=True, exist_ok=True)
with Image.open(source_path) as image:
image = ImageOps.exif_transpose(image)
image.thumbnail(self.size, Image.Resampling.LANCZOS)
if image.mode not in {"RGB", "L"}:
image = image.convert("RGB")
image.save(thumbnail_path, format="JPEG", quality=self.quality, optimize=True)
return image.size
def _thumbnail_path(self, artwork_id: int, page_index: int, source_path: Path) -> Path:
bucket = str(abs(artwork_id) % 1000).zfill(3)
return self.cache_dir / bucket / f"{artwork_id}_{page_index}_{source_path.stem}.jpg"
+405
View File
@@ -0,0 +1,405 @@
from __future__ import annotations
import json
import time
import uuid
from dataclasses import asdict
from datetime import datetime
from pathlib import Path
from typing import Any
from pixiv_app.core.auth import SessionStore
from pixiv_app.core.cookie_import import (
cookie_summary,
cookies_to_playwright,
parse_cookie_text,
save_playwright_cookies,
)
from pixiv_app.core.paths import (
app_session_file,
downloads_root,
plugin_roots,
plugins_root as app_plugins_root,
runtime_path,
)
from pixiv_app.core.plugin.base import Resource
from pixiv_app.core.plugin.manager import PluginManager
def project_root() -> Path:
return app_plugins_root().parent
class XMediaService:
IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp", ".gif"}
VIDEO_EXTS = {".mp4", ".mov", ".webm", ".m4v"}
def __init__(self, *, plugins_root: str | Path | None = None, save_root: str | Path | None = None) -> None:
self.plugins_root = Path(plugins_root) if plugins_root else app_plugins_root()
self.save_root = Path(save_root) if save_root else downloads_root() / "x"
self.cookie_path = runtime_path("x_cookies.json")
self.follow_store_path = runtime_path("x_followed_authors.json")
self._manager: PluginManager | None = None
self._preview_cache: dict[str, list[Resource]] = {}
def import_cookie_text(self, text: str) -> dict[str, Any]:
cookies = parse_cookie_text(text)
x_cookies = cookies_to_playwright(cookies, ("x.com", "twitter.com"))
path = save_playwright_cookies(x_cookies, self.cookie_path)
plugin = self._get_plugin()
if hasattr(plugin, "reset_playwright_session"):
plugin.reset_playwright_session()
return {"count": len(x_cookies), "path": str(path), "summary": cookie_summary(x_cookies)}
def preview_author_media(self, username: str, *, media_type: str = "all", max_items: int = 120) -> dict[str, Any]:
handle = _normalize_username(username)
media_type = _normalize_media_type(media_type)
max_items = max(1, min(int(max_items), 500))
url = f"https://x.com/{handle}/media"
plugin = self._get_plugin()
if not plugin.validate():
raise ValueError("X plugin is unavailable. Install playwright/httpx and run playwright install chromium first.")
resources = plugin.parse(url)
resources = _filter_resources(resources, media_type)[:max_items]
preview_id = uuid.uuid4().hex
self._preview_cache[preview_id] = resources
return {
"preview_id": preview_id,
"username": handle,
"media_type": media_type,
"count": len(resources),
"items": [_resource_to_json(item, i) for i, item in enumerate(resources)],
"created_at": int(time.time()),
}
def download_preview(self, preview_id: str, *, selected_ids: list[str] | None = None) -> dict[str, Any]:
resources = self._preview_cache.get(preview_id)
if resources is None:
raise ValueError("Preview expired. Re-run the X media preview.")
selected = set(selected_ids or [])
if selected:
resources = [item for item in resources if item.id in selected]
if not resources:
raise ValueError("No X media items selected.")
plugin = self._get_plugin()
self.save_root.mkdir(parents=True, exist_ok=True)
results: list[dict[str, Any]] = []
downloaded = 0
failed = 0
for resource in resources:
try:
paths = plugin.download(resource, self.save_root)
downloaded += len(paths)
results.append(
{
"id": resource.id,
"ok": bool(paths),
"paths": [str(path) for path in paths],
"title": resource.title,
"synced_files": 0,
}
)
if not paths:
failed += 1
except Exception as exc: # Keep batch downloads moving.
failed += 1
results.append({"id": resource.id, "ok": False, "error": str(exc), "title": resource.title})
return {
"requested": len(resources),
"downloaded_files": downloaded,
"synced_files": 0,
"failed_items": failed,
"items": results,
}
def list_local_media(self, *, limit: int = 160, offset: int = 0) -> dict[str, Any]:
self.save_root.mkdir(parents=True, exist_ok=True)
limit = max(1, min(int(limit), 500))
offset = max(0, int(offset))
files = [
path
for path in self.save_root.rglob("*")
if path.is_file() and path.suffix.lower() in self.IMAGE_EXTS | self.VIDEO_EXTS
]
files.sort(key=lambda path: path.stat().st_mtime, reverse=True)
page = files[offset : offset + limit]
return {
"total": len(files),
"items": [self._local_media_to_json(path) for path in page],
}
def delete_local_media(self, media_ids: list[str]) -> dict[str, Any]:
deleted: list[str] = []
failed: list[dict[str, str]] = []
for media_id in media_ids:
try:
path = self.resolve_local_media(media_id)
path.unlink()
deleted.append(media_id)
except Exception as exc:
failed.append({"id": media_id, "error": str(exc)})
return {"deleted": deleted, "failed": failed}
def resolve_local_media(self, media_id: str) -> Path:
rel_text = _decode_media_id(media_id)
target = (self.save_root / rel_text).resolve()
root = self.save_root.resolve()
if root != target and root not in target.parents:
raise ValueError("Illegal media path.")
if not target.is_file():
raise ValueError("Local media file does not exist.")
return target
def list_followed_authors(self) -> list[dict[str, Any]]:
rows = [item for item in self._load_follow_rows() if bool(item.get("enabled", True))]
rows.sort(key=lambda item: ((str(item.get("creator_name") or "") or str(item.get("username") or "")).lower()))
return rows
def set_followed_author(self, username: str, *, followed: bool = True, creator_name: str = "") -> dict[str, Any]:
handle = _normalize_username(username)
rows = self._load_follow_rows()
row = next((item for item in rows if str(item.get("username", "")).lower() == handle.lower()), None)
now = _now_text()
if row is None:
row = {
"username": handle,
"creator_name": creator_name.strip() or handle,
"enabled": True,
"profile_url": f"https://x.com/{handle}",
"latest_tweet_id": "",
"latest_published_at": "",
"last_checked_at": "",
"last_error": "",
"seen_tweet_ids": [],
"created_at": now,
}
rows.append(row)
row["username"] = handle
row["creator_name"] = creator_name.strip() or str(row.get("creator_name") or handle)
row["enabled"] = bool(followed)
row["profile_url"] = f"https://x.com/{handle}"
row["updated_at"] = now
if not followed:
row["last_error"] = ""
self._save_follow_rows(rows)
return dict(row)
def sync_followed_authors(self, *, download: bool = True, max_new_per_author: int = 20) -> dict[str, Any]:
followed = self.list_followed_authors()
if not followed:
return {"authors": []}
plugin = self._get_plugin()
if not plugin.validate():
raise ValueError("X plugin is unavailable. Import a valid X cookie and ensure Playwright is installed.")
results: list[dict[str, Any]] = []
rows = self._load_follow_rows()
for row in rows:
if not bool(row.get("enabled", True)):
continue
handle = str(row.get("username") or "").strip()
if not handle:
continue
try:
parse_for_sync = getattr(plugin, "parse_for_sync", plugin.parse)
resources = parse_for_sync(f"https://x.com/{handle}")
grouped = _group_resources_by_tweet(resources)
seen_ids = [str(item) for item in row.get("seen_tweet_ids", []) if str(item).strip()]
seen_set = set(seen_ids)
new_groups = [item for item in grouped if item["tweet_id"] and item["tweet_id"] not in seen_set]
new_groups = new_groups[: max(0, int(max_new_per_author))]
downloaded_files = 0
synced_files = 0
created_alerts = 0
new_ids: list[str] = []
for group in new_groups:
representative = group["resource"]
new_ids.append(group["tweet_id"])
if download:
for resource in group["resources"]:
paths = plugin.download(resource, self.save_root)
downloaded_files += len(paths)
latest_group = grouped[0] if grouped else None
if latest_group is not None:
representative = latest_group["resource"]
row["creator_name"] = representative.author or str(row.get("creator_name") or handle)
row["latest_tweet_id"] = latest_group["tweet_id"]
row["latest_published_at"] = representative.created_at or str(row.get("latest_published_at") or "")
row["last_checked_at"] = _now_text()
row["last_error"] = ""
row["updated_at"] = _now_text()
row["seen_tweet_ids"] = _merge_seen_ids(new_ids, seen_ids)
results.append(
{
"username": handle,
"creator_name": row.get("creator_name") or handle,
"new_posts": len(new_groups),
"downloaded_files": downloaded_files,
"synced_files": synced_files,
"alerts": created_alerts,
"error": "",
}
)
except Exception as exc:
row["last_checked_at"] = _now_text()
row["last_error"] = str(exc)
row["updated_at"] = _now_text()
results.append(
{
"username": handle,
"creator_name": row.get("creator_name") or handle,
"new_posts": 0,
"downloaded_files": 0,
"synced_files": 0,
"alerts": 0,
"error": str(exc),
}
)
self._save_follow_rows(rows)
return {"authors": results}
def _get_plugin(self):
if self._manager is None:
self._manager = PluginManager(plugin_roots() if self.plugins_root == app_plugins_root() else [self.plugins_root])
self._manager.load_all()
plugin = self._manager.plugins.get("X")
if plugin is None:
raise ValueError("X plugin is missing. Confirm plugins/x exists.")
return plugin
def _load_follow_rows(self) -> list[dict[str, Any]]:
if not self.follow_store_path.is_file():
return []
try:
payload = json.loads(self.follow_store_path.read_text(encoding="utf-8"))
except Exception:
return []
if isinstance(payload, list):
return [dict(item) for item in payload if isinstance(item, dict)]
return []
def _save_follow_rows(self, rows: list[dict[str, Any]]) -> None:
self.follow_store_path.parent.mkdir(parents=True, exist_ok=True)
self.follow_store_path.write_text(
json.dumps(rows, ensure_ascii=False, indent=2),
encoding="utf-8",
)
def _session_payload(self) -> dict[str, Any]:
return SessionStore(app_session_file()).load()
def _local_media_to_json(self, path: Path) -> dict[str, Any]:
rel = path.relative_to(self.save_root)
stat = path.stat()
kind = "video" if path.suffix.lower() in self.VIDEO_EXTS else "image"
media_id = _encode_media_id(str(rel).replace("\\", "/"))
return {
"id": media_id,
"name": path.name,
"relative_path": str(rel).replace("\\", "/"),
"kind": kind,
"size": stat.st_size,
"modified_at": int(stat.st_mtime),
"url": f"/media/x-local/{media_id}",
}
def _normalize_username(username: str) -> str:
value = username.strip()
if value.startswith("@"):
value = value[1:]
if "/" in value:
parts = [item for item in value.replace("https://", "").replace("http://", "").split("/") if item]
value = parts[1] if parts and parts[0].lower() in {"x.com", "twitter.com", "www.x.com", "www.twitter.com"} else parts[0]
if not value:
raise ValueError("Please enter an X username.")
if len(value) > 15 or not value.replace("_", "").isalnum():
raise ValueError("Invalid X username format.")
return value
def _normalize_media_type(media_type: str) -> str:
value = (media_type or "all").strip().lower()
if value not in {"all", "image", "video"}:
raise ValueError("media_type must be all, image, or video.")
return value
def _filter_resources(resources: list[Resource], media_type: str) -> list[Resource]:
if media_type == "all":
return resources
return [item for item in resources if any(file.get("type", "image") == media_type for file in item.files)]
def _resource_to_json(resource: Resource, index: int) -> dict[str, Any]:
payload = asdict(resource)
payload["index"] = index
payload["kind"] = "video" if any(file.get("type") == "video" for file in resource.files) else "image"
payload["file_count"] = len(resource.files)
return payload
def _group_resources_by_tweet(resources: list[Resource]) -> list[dict[str, Any]]:
grouped: dict[str, dict[str, Any]] = {}
ordered: list[str] = []
for resource in resources:
tweet_id = str(resource.metadata.get("tweet_id") or resource.id or "")
key = tweet_id or resource.id
if key not in grouped:
grouped[key] = {
"tweet_id": tweet_id,
"resource": resource,
"resources": [resource],
}
ordered.append(key)
else:
grouped[key]["resources"].append(resource)
return [grouped[key] for key in ordered]
def _merge_seen_ids(new_ids: list[str], existing_ids: list[str], *, limit: int = 200) -> list[str]:
merged: list[str] = []
seen: set[str] = set()
for item in [*new_ids, *existing_ids]:
value = str(item).strip()
if not value or value in seen:
continue
seen.add(value)
merged.append(value)
if len(merged) >= limit:
break
return merged
def _encode_media_id(value: str) -> str:
from base64 import urlsafe_b64encode
return urlsafe_b64encode(value.encode("utf-8")).decode("ascii").rstrip("=")
def _decode_media_id(value: str) -> str:
from base64 import urlsafe_b64decode
padding = "=" * (-len(value) % 4)
return urlsafe_b64decode((value + padding).encode("ascii")).decode("utf-8")
def _now_text() -> str:
return datetime.now().isoformat(timespec="seconds")
+1
View File
@@ -0,0 +1 @@
"""GUI layer for Sakura downloader."""
+2794
View File
File diff suppressed because it is too large. Load diff
+343
View File
@@ -0,0 +1,343 @@
from __future__ import annotations
import re
import shutil
from collections.abc import Callable
from datetime import datetime
from pathlib import Path
from tkinter import filedialog, messagebox
import customtkinter as ctk
from pixiv_app.core.paths import plugin_roots, user_plugins_root
from pixiv_app.core.plugin.base import BasePlugin
from pixiv_app.core.plugin.generator import PluginGenerator
from pixiv_app.core.plugin.manager import PluginManager
def _slugify_plugin_name(value: str) -> str:
text = re.sub(r"[^0-9A-Za-z_\-\u4e00-\u9fff]+", "_", value.strip())
text = text.strip("._-")
return text or "imported_plugin"
class PluginPanel(ctk.CTkFrame):
"""Generic plugin list, import, reload, and template tools."""
def __init__(
self,
parent,
plugin_manager: PluginManager,
*,
user_root: Path | None = None,
text_color: str = "#1e3a5f",
muted_color: str = "#6f8dac",
border_color: str = "#d6e8fb",
danger_color: str = "#eb7d7d",
) -> None:
super().__init__(parent, fg_color="transparent")
self.manager = plugin_manager
self.user_root = user_root or user_plugins_root()
self._text_color = text_color
self._muted_color = muted_color
self._border_color = border_color
self._danger_color = danger_color
title_frame = ctk.CTkFrame(self, fg_color="transparent")
title_frame.pack(fill="x", padx=12, pady=(10, 6))
ctk.CTkLabel(
title_frame,
text="插件管理",
font=ctk.CTkFont(size=18, weight="bold"),
text_color=text_color,
).pack(side="left")
btn_row = ctk.CTkFrame(title_frame, fg_color="transparent")
btn_row.pack(side="right")
ctk.CTkButton(btn_row, text="导入 .py", width=88, command=self.import_python_plugin).pack(side="left", padx=4)
ctk.CTkButton(btn_row, text="生成模板", width=88, command=self.open_create_dialog).pack(side="left", padx=4)
ctk.CTkButton(btn_row, text="重新加载", width=88, command=self.reload_all).pack(side="left", padx=4)
hint = (
"导入一个 Python 插件文件即可扩展站点;插件需导出 plugin_class,"
"并继承 pixiv_app.core.plugin.base.BasePlugin。"
)
ctk.CTkLabel(
self,
text=hint,
text_color=muted_color,
font=ctk.CTkFont(size=12),
wraplength=760,
justify="left",
).pack(anchor="w", padx=14, pady=(0, 8))
self.summary_var = ctk.StringVar(value="正在读取插件状态...")
ctk.CTkLabel(
self,
textvariable=self.summary_var,
text_color=text_color,
font=ctk.CTkFont(size=12, weight="bold"),
anchor="w",
).pack(fill="x", padx=14, pady=(0, 8))
path_row = ctk.CTkFrame(self, fg_color="transparent")
path_row.pack(fill="x", padx=12, pady=(0, 8))
ctk.CTkLabel(path_row, text="用户插件目录:", text_color=text_color, font=ctk.CTkFont(size=12, weight="bold")).pack(
side="left"
)
ctk.CTkLabel(path_row, text=str(self.user_root), text_color=muted_color, font=ctk.CTkFont(size=11)).pack(
side="left", fill="x", expand=True
)
self.container = ctk.CTkScrollableFrame(self, fg_color="transparent")
self.container.pack(fill="both", expand=True, padx=8, pady=(0, 8))
self.error_container = ctk.CTkFrame(self, fg_color="transparent")
self.error_container.pack(fill="x", padx=8, pady=(0, 8))
self.log_text = ctk.CTkTextbox(self, height=120, font=ctk.CTkFont(family="Consolas", size=11))
self.log_text.pack(fill="x", padx=12, pady=(0, 12))
self.refresh()
def log(self, message: str) -> None:
stamp = datetime.now().strftime("%H:%M:%S")
self.log_text.insert("end", f"[{stamp}] {message}\n")
self.log_text.see("end")
def refresh(self) -> None:
for child in self.container.winfo_children():
child.destroy()
for child in self.error_container.winfo_children():
child.destroy()
if not self.manager.plugins:
ctk.CTkLabel(
self.container,
text="没有发现插件。请点击「导入 .py」或「生成模板」。",
text_color=self._muted_color,
justify="left",
).pack(anchor="w", pady=8)
for name, plugin in sorted(self.manager.plugins.items(), key=lambda x: x[0].lower()):
self._add_plugin_card(name, plugin)
ready_count = sum(1 for plugin in self.manager.plugins.values() if plugin.validate())
self.summary_var.set(
f"已加载 {len(self.manager.plugins)} 个插件 · 可用 {ready_count} 个 · "
f"加载失败 {len(self.manager.load_errors)} 个"
)
self._add_error_cards()
def _add_error_cards(self) -> None:
if not self.manager.load_errors:
return
title = ctk.CTkLabel(
self.error_container,
text="加载失败的插件",
font=ctk.CTkFont(size=14, weight="bold"),
text_color=self._danger_color,
)
title.pack(anchor="w", padx=4, pady=(8, 4))
for path, error in sorted(self.manager.load_errors.items(), key=lambda x: str(x[0]).lower()):
card = ctk.CTkFrame(self.error_container, corner_radius=10, border_width=1, border_color=self._danger_color)
card.pack(fill="x", pady=4)
ctk.CTkLabel(
card,
text=str(path),
font=ctk.CTkFont(size=11),
text_color=self._text_color,
wraplength=760,
justify="left",
).pack(anchor="w", padx=10, pady=(8, 2))
ctk.CTkLabel(
card,
text=error,
font=ctk.CTkFont(size=11),
text_color=self._danger_color,
wraplength=760,
justify="left",
).pack(anchor="w", padx=10, pady=(0, 8))
def _add_plugin_card(self, name: str, plugin: BasePlugin) -> None:
card = ctk.CTkFrame(self.container, corner_radius=12, border_width=1, border_color=self._border_color)
card.pack(fill="x", pady=6)
top = ctk.CTkFrame(card, fg_color="transparent")
top.pack(fill="x", padx=12, pady=(10, 4))
ctk.CTkLabel(top, text=plugin.name, font=ctk.CTkFont(size=15, weight="bold"), text_color=self._text_color).pack(
side="left"
)
ctk.CTkLabel(top, text=f"v{plugin.version}", font=ctk.CTkFont(size=12), text_color=self._muted_color).pack(
side="left", padx=10
)
is_ready = plugin.validate()
st = "可用" if is_ready else "需配置"
ctk.CTkLabel(
top,
text=st,
font=ctk.CTkFont(size=12, weight="bold"),
text_color="#3f9b72" if is_ready else self._danger_color,
).pack(side="right")
path = self.manager.get_plugin_path(name)
source = "用户插件" if path and self.user_root in path.parents else "内置插件"
ctk.CTkLabel(
card,
text=f"域名 {plugin.domain} 来源 {source}",
font=ctk.CTkFont(size=12),
text_color=self._muted_color,
).pack(anchor="w", padx=12, pady=(0, 4))
if path:
ctk.CTkLabel(
card,
text=str(path),
font=ctk.CTkFont(size=11),
text_color=self._muted_color,
wraplength=760,
justify="left",
).pack(anchor="w", padx=12, pady=(0, 8))
actions = ctk.CTkFrame(card, fg_color="transparent")
actions.pack(fill="x", padx=12, pady=(0, 10))
ctk.CTkButton(actions, text="重载", width=72, command=lambda n=name: self.reload_one(n)).pack(side="left", padx=(0, 6))
if path and self.user_root in path.parents:
ctk.CTkButton(
actions,
text="删除文件",
width=86,
fg_color=self._danger_color,
command=lambda n=name: self.delete_user_plugin(n),
).pack(side="left")
else:
ctk.CTkButton(actions, text="从内存卸载", width=92, command=lambda n=name: self.unload_one(n)).pack(side="left")
def import_python_plugin(self) -> None:
path = filedialog.askopenfilename(
title="导入 Python 插件",
filetypes=[("Python plugin", "*.py"), ("All files", "*.*")],
)
if not path:
return
src = Path(path)
if not src.is_file():
messagebox.showerror("导入失败", "文件不存在。")
return
plugin_dir = self.user_root / _slugify_plugin_name(src.stem)
index = 2
while plugin_dir.exists() and (plugin_dir / "plugin.py").resolve() != src.resolve():
plugin_dir = self.user_root / f"{_slugify_plugin_name(src.stem)}_{index}"
index += 1
plugin_dir.mkdir(parents=True, exist_ok=True)
dst = plugin_dir / "plugin.py"
try:
shutil.copy2(src, dst)
plugin = self.manager.load_plugin(dst)
except Exception as exc:
messagebox.showerror("导入失败", str(exc))
return
if plugin is None:
messagebox.showerror("导入失败", "插件未能加载。请确认文件中导出了 plugin_class。")
return
self.refresh()
self.log(f"已导入插件: {plugin.name} -> {dst}")
messagebox.showinfo("导入完成", f"已导入插件:{plugin.name}\n\n{dst}")
def reload_all(self) -> None:
self.manager.reload_all_from_disk()
self.refresh()
self.log("已重新加载全部插件。")
def reload_one(self, name: str) -> None:
if self.manager.reload_plugin(name):
self.refresh()
self.log(f"已重载插件: {name}")
else:
self.log(f"重载失败: {name}")
def unload_one(self, name: str) -> None:
self.manager.unload_plugin(name)
self.refresh()
self.log(f"已从内存卸载: {name}")
def delete_user_plugin(self, name: str) -> None:
path = self.manager.get_plugin_path(name)
if path is None or self.user_root not in path.parents:
messagebox.showinfo("提示", "只能删除用户导入的插件文件。")
return
if not messagebox.askyesno("确认删除", f"将删除插件目录:\n{path.parent}\n\n是否继续?"):
return
self.manager.unload_plugin(name)
shutil.rmtree(path.parent, ignore_errors=True)
self.manager.reload_all_from_disk()
self.refresh()
self.log(f"已删除用户插件: {name}")
def open_create_dialog(self) -> None:
CreatePluginDialog(self, self.user_root, on_created=lambda: (self.reload_all(), self.refresh()))
class CreatePluginDialog(ctk.CTkToplevel):
def __init__(self, parent, plugins_root: Path, *, on_created: Callable[[], None] | None = None) -> None:
super().__init__(parent)
self.title("生成插件模板")
self.geometry("520x420")
self.plugins_root = plugins_root
self._on_created = on_created
self.generator = PluginGenerator()
pad = {"padx": 16, "pady": 8}
ctk.CTkLabel(self, text="名称").pack(anchor="w", **pad)
self.name_entry = ctk.CTkEntry(self, placeholder_text="例如 MySite")
self.name_entry.pack(fill="x", **pad)
ctk.CTkLabel(self, text="域名").pack(anchor="w", **pad)
self.domain_entry = ctk.CTkEntry(self, placeholder_text="例如 example.com")
self.domain_entry.pack(fill="x", **pad)
ctk.CTkLabel(self, text="作者").pack(anchor="w", **pad)
self.author_entry = ctk.CTkEntry(self)
self.author_entry.pack(fill="x", **pad)
ctk.CTkLabel(self, text="描述").pack(anchor="w", **pad)
self.desc_text = ctk.CTkTextbox(self, height=100)
self.desc_text.pack(fill="x", **pad)
ctk.CTkButton(self, text="生成到用户插件目录", command=self._generate).pack(pady=16)
def _generate(self) -> None:
data = {
"name": self.name_entry.get().strip(),
"domain": self.domain_entry.get().strip(),
"author": self.author_entry.get().strip(),
"description": self.desc_text.get("1.0", "end").strip(),
}
if not data["name"] or not data["domain"]:
messagebox.showerror("校验失败", "请填写名称与域名。")
return
try:
path = self.generator.generate_from_gui(data, output_root=self.plugins_root)
except Exception as exc:
messagebox.showerror("失败", str(exc))
return
messagebox.showinfo("完成", f"已生成: {path}")
self.destroy()
if self._on_created:
self._on_created()
class PluginPanelWindow(ctk.CTkToplevel):
"""Standalone window hosting PluginPanel."""
def __init__(self, parent, *, cookie: str = "") -> None:
super().__init__(parent)
self.title("插件管理")
self.geometry("760x620")
user_root = user_plugins_root()
self.manager = PluginManager(plugin_roots())
self.manager.load_all()
pixiv = self.manager.plugins.get("Pixiv")
if pixiv is not None and hasattr(pixiv, "set_auth"):
pixiv.set_auth(cookie)
panel = PluginPanel(self, self.manager, user_root=user_root)
panel.pack(fill="both", expand=True)
+160
View File
@@ -0,0 +1,160 @@
from __future__ import annotations
import threading
from typing import Any, Callable, Optional
import customtkinter as ctk
class ProxyDialog(ctk.CTkToplevel):
def __init__(
self,
master,
initial_config: dict,
on_save: Callable[[dict], None],
on_action: Optional[Callable[[str, dict], str | dict[str, Any]]] = None,
):
super().__init__(master)
self.title("代理池设置")
self.geometry("760x700")
self.minsize(700, 640)
self.on_save = on_save
self.on_action = on_action
self.initial_config = initial_config
self.use_proxy_var = ctk.BooleanVar(value=bool(initial_config.get("enabled", False)))
self.use_quake_var = ctk.BooleanVar(value=bool(initial_config.get("use_quake", False)))
self.quake_mode_var = ctk.StringVar(value=str(initial_config.get("quake_mode", "api_v3")))
self.status_var = ctk.StringVar(value="准备就绪")
self._build_ui()
self.transient(master)
self.grab_set()
def _build_ui(self) -> None:
container = ctk.CTkFrame(self, fg_color="transparent")
container.pack(fill="both", expand=True, padx=16, pady=16)
ctk.CTkLabel(container, text="代理池设置", font=ctk.CTkFont(size=24, weight="bold")).pack(anchor="w", pady=(0, 6))
ctk.CTkLabel(
container,
text="预校验通过的代理会自动回填到下面的代理列表里,保存后下次可直接使用。",
text_color="#6f8dac",
font=ctk.CTkFont(size=12),
).pack(anchor="w", pady=(0, 12))
toggle_row = ctk.CTkFrame(container, fg_color="#f7fbff", corner_radius=12)
toggle_row.pack(fill="x", pady=(0, 10), padx=2)
ctk.CTkSwitch(toggle_row, text="启用代理池", variable=self.use_proxy_var).pack(anchor="w", padx=12, pady=(8, 6))
ctk.CTkSwitch(toggle_row, text="启用 Quake 自动爬取", variable=self.use_quake_var).pack(anchor="w", padx=12, pady=(0, 10))
mode_row = ctk.CTkFrame(container, fg_color="transparent")
mode_row.pack(fill="x", pady=(0, 8))
ctk.CTkLabel(mode_row, text="Quake 模式:", width=120).pack(side="left")
ctk.CTkOptionMenu(mode_row, values=["api_v3", "web_assoc"], variable=self.quake_mode_var, width=220).pack(side="left")
key_row = ctk.CTkFrame(container, fg_color="transparent")
key_row.pack(fill="x", pady=(0, 8))
ctk.CTkLabel(key_row, text="Quake API Key:", width=120).pack(side="left")
self.api_key_entry = ctk.CTkEntry(key_row, placeholder_text="输入 Quake API Key")
self.api_key_entry.pack(side="left", fill="x", expand=True)
self.api_key_entry.insert(0, str(self.initial_config.get("quake_api_key", "")))
cookie_row = ctk.CTkFrame(container, fg_color="transparent")
cookie_row.pack(fill="x", pady=(0, 8))
ctk.CTkLabel(cookie_row, text="Quake Cookie:", width=120).pack(side="left")
self.cookie_entry = ctk.CTkEntry(cookie_row, placeholder_text="输入 Quake Cookie(web_assoc 模式需要)")
self.cookie_entry.pack(side="left", fill="x", expand=True)
self.cookie_entry.insert(0, str(self.initial_config.get("quake_cookie", "")))
country_row = ctk.CTkFrame(container, fg_color="transparent")
country_row.pack(fill="x", pady=(0, 8))
ctk.CTkLabel(country_row, text="海外国家代码:", width=120).pack(side="left")
self.country_entry = ctk.CTkEntry(country_row, placeholder_text="US,JP,KR,SG,HK,TW,DE,FR,GB,CA,AU")
self.country_entry.pack(side="left", fill="x", expand=True)
self.country_entry.insert(0, str(self.initial_config.get("countries_text", "US,JP,KR,SG,HK,TW,DE,FR,GB,CA,AU")))
tool_row = ctk.CTkFrame(container, fg_color="transparent")
tool_row.pack(fill="x", pady=(8, 6))
self.manual_input = ctk.CTkEntry(tool_row, placeholder_text="快速添加单个代理,如 1.2.3.4:8080")
self.manual_input.pack(side="left", fill="x", expand=True)
ctk.CTkButton(tool_row, text="手动添加", width=110, command=self._add_manual_proxy).pack(side="left", padx=(8, 0))
self.fetch_button = ctk.CTkButton(tool_row, text="开始爬取并预校验", width=170, command=self._trigger_fetch)
self.fetch_button.pack(side="left", padx=(8, 0))
ctk.CTkLabel(
container,
text="代理列表(每行一个,支持 ip:port / http://ip:port / https://ip:port)",
anchor="w",
).pack(fill="x", pady=(8, 0))
self.proxy_text = ctk.CTkTextbox(container, height=360)
self.proxy_text.pack(fill="both", expand=True, pady=(6, 12))
self.proxy_text.insert("1.0", str(self.initial_config.get("manual_text", "")))
status_row = ctk.CTkFrame(container, fg_color="#f7fbff", corner_radius=12)
status_row.pack(fill="x", pady=(0, 10))
ctk.CTkLabel(status_row, textvariable=self.status_var, text_color="#365b85", font=ctk.CTkFont(size=12)).pack(
anchor="w", padx=12, pady=8
)
button_row = ctk.CTkFrame(container, fg_color="transparent")
button_row.pack(fill="x")
ctk.CTkButton(button_row, text="取消", command=self.destroy, width=120).pack(side="right", padx=(8, 0))
ctk.CTkButton(button_row, text="保存", command=self._save, width=120).pack(side="right")
def _collect_config(self) -> dict:
return {
"enabled": bool(self.use_proxy_var.get()),
"use_quake": bool(self.use_quake_var.get()),
"quake_mode": self.quake_mode_var.get().strip() or "api_v3",
"quake_api_key": self.api_key_entry.get().strip(),
"quake_cookie": self.cookie_entry.get().strip(),
"manual_text": self.proxy_text.get("1.0", "end").strip(),
"countries_text": self.country_entry.get().strip(),
}
def _add_manual_proxy(self) -> None:
value = self.manual_input.get().strip()
if not value:
self.status_var.set("请输入代理地址后再添加。")
return
old_text = self.proxy_text.get("1.0", "end").strip()
new_text = f"{old_text}\n{value}".strip()
self.proxy_text.delete("1.0", "end")
self.proxy_text.insert("1.0", new_text)
self.manual_input.delete(0, "end")
self.status_var.set("已加入手动代理列表。")
def _trigger_fetch(self) -> None:
if not self.on_action:
self.status_var.set("当前版本未启用预校验动作。")
return
self.fetch_button.configure(state="disabled", text="处理中...")
self.status_var.set("正在爬取并校验代理,请稍候...")
config = self._collect_config()
def worker() -> None:
try:
result = self.on_action("fetch_preview", config)
except Exception as exc:
result = {"status": f"预校验失败: {exc}"}
self.after(0, lambda: self._finish_fetch(result))
threading.Thread(target=worker, daemon=True).start()
def _finish_fetch(self, result: str | dict[str, Any]) -> None:
self.fetch_button.configure(state="normal", text="开始爬取并预校验")
if isinstance(result, dict):
manual_text = result.get("manual_text")
if isinstance(manual_text, str):
self.proxy_text.delete("1.0", "end")
self.proxy_text.insert("1.0", manual_text)
status = str(result.get("status", "预校验完成"))
self.status_var.set(status)
return
self.status_var.set(str(result))
def _save(self) -> None:
self.on_save(self._collect_config())
self.destroy()
+1
View File
@@ -0,0 +1 @@
"""Local service entry points for Sakura downloader."""
+500
View File
@@ -0,0 +1,500 @@
from __future__ import annotations
import json
import mimetypes
import time
import uuid
from dataclasses import asdict
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from urllib.parse import parse_qs, urlparse
from pixiv_app.core.auth import SessionStore
from pixiv_app.core.cookie_import import cookie_summary, cookies_to_header, cookies_to_playwright, parse_cookie_text
from pixiv_app.core.gallery_service import LocalGalleryService
from pixiv_app.core.library import DEFAULT_LIBRARY_DB
from pixiv_app.core.origin_follow_sync import OriginFollowSyncService
from pixiv_app.core.paths import app_session_file, downloads_root, plugin_roots, webui_root
from pixiv_app.core.plugin.manager import PluginManager
from pixiv_app.core.thumbnails import DEFAULT_THUMBNAIL_DIR
from pixiv_app.core.x_media_service import XMediaService
class GalleryApiServer:
def __init__(
self,
*,
host: str = "127.0.0.1",
port: int = 8765,
db_path: str | Path = DEFAULT_LIBRARY_DB,
thumbnail_dir: str | Path = DEFAULT_THUMBNAIL_DIR,
web_root: str | Path | None = None,
) -> None:
self.host = host
self.port = port
self.service = LocalGalleryService(db_path=db_path, thumbnail_dir=thumbnail_dir)
self.x_media_service = XMediaService()
self.origin_follow_sync_service = OriginFollowSyncService(
db_path=str(db_path),
gallery_service=self.service,
x_media_service=self.x_media_service,
)
self.plugin_manager = PluginManager(plugin_roots())
self.plugin_manager.load_all()
self.plugin_preview_cache: dict[str, tuple[str, list]] = {}
self.web_root = Path(web_root) if web_root else webui_root()
def serve_forever(self) -> None:
service = self.service
x_media_service = self.x_media_service
origin_follow_sync_service = self.origin_follow_sync_service
plugin_manager = self.plugin_manager
plugin_preview_cache = self.plugin_preview_cache
web_root = self.web_root
Handler = type(
"GalleryApiHandlerForServer",
(GalleryApiHandler,),
{
"gallery_service": service,
"x_media_service": x_media_service,
"origin_follow_sync_service": origin_follow_sync_service,
"plugin_manager": plugin_manager,
"plugin_preview_cache": plugin_preview_cache,
"web_root": web_root,
},
)
server = ThreadingHTTPServer((self.host, self.port), Handler)
try:
server.serve_forever()
finally:
server.server_close()
class GalleryApiHandler(BaseHTTPRequestHandler):
gallery_service: LocalGalleryService
x_media_service: XMediaService
origin_follow_sync_service: OriginFollowSyncService
plugin_manager: PluginManager
plugin_preview_cache: dict[str, tuple[str, list]]
web_root: Path
def do_GET(self) -> None:
parsed = urlparse(self.path)
query = parse_qs(parsed.query)
try:
if parsed.path in {"/", "/index.html"}:
self._send_static_file(self.web_root / "index.html")
elif parsed.path.startswith("/assets/"):
self._send_static_file(self.web_root / parsed.path.removeprefix("/assets/"))
elif parsed.path == "/api/health":
self._send_json({"status": "ok"})
elif parsed.path == "/api/session/status":
self._send_json(self._handle_session_status())
elif parsed.path == "/api/plugins/pages":
self._send_json(self._handle_plugin_pages())
elif parsed.path == "/api/artworks":
self._send_json(self._handle_artworks(query))
elif parsed.path == "/api/favorites":
self._send_json(self.gallery_service.list_favorites(limit=_int(query, "limit", 60), offset=_int(query, "offset", 0)))
elif parsed.path.startswith("/api/artworks/"):
self._send_json(self._handle_artwork_detail(parsed.path))
elif parsed.path == "/api/tags":
self._send_json(self.gallery_service.list_tags(query=_first(query, "q"), limit=_int(query, "limit", 100)))
elif parsed.path == "/api/artists":
self._send_json(self.gallery_service.list_artists(query=_first(query, "q"), limit=_int(query, "limit", 100)))
elif parsed.path.startswith("/api/artists/"):
self._send_json(self._handle_artist_detail(parsed.path, query))
elif parsed.path == "/api/followed-artists":
self._send_json(self.gallery_service.list_followed_artists())
elif parsed.path == "/api/thumbnails/build":
self._send_json({"items": self.gallery_service.build_missing_thumbnails(limit=_int(query, "limit", 200))})
elif parsed.path == "/api/import/legacy":
self._send_json(self.gallery_service.scan_legacy_downloads(root=_first(query, "root", "Sakura_Downloads")))
elif parsed.path == "/api/sync/followed":
self._send_json(
self.gallery_service.sync_followed_artists(
max_new_per_artist=_int(query, "max_new", 20),
download=_bool(query, "download", True),
)
)
elif parsed.path == "/api/x/local-media":
self._send_json(
self.x_media_service.list_local_media(
limit=_int(query, "limit", 160),
offset=_int(query, "offset", 0),
)
)
elif parsed.path == "/api/x/follows":
self._send_json(self.x_media_service.list_followed_authors())
elif parsed.path.startswith("/media/thumbnail/"):
self._send_file(self._resolve_media_path(parsed.path, thumbnail=True))
elif parsed.path.startswith("/media/source/"):
self._send_file(self._resolve_media_path(parsed.path, thumbnail=False))
elif parsed.path.startswith("/media/x-local/"):
self._send_file(self.x_media_service.resolve_local_media(parsed.path.rsplit("/", 1)[-1]))
else:
self._send_json({"error": "not found"}, status=404)
except ValueError as exc:
self._send_json({"error": str(exc)}, status=400)
except Exception as exc:
self._send_json({"error": str(exc)}, status=500)
def do_POST(self) -> None:
parsed = urlparse(self.path)
try:
payload = self._read_json_body()
if parsed.path == "/api/favorites":
self._send_json(
self.gallery_service.set_favorite(
int(payload["artwork_id"]),
bool(payload.get("favorite", True)),
str(payload.get("note", "")),
)
)
elif parsed.path == "/api/followed-artists":
self._send_json(
self.gallery_service.set_followed_artist(
payload.get("artist_id", ""),
bool(payload.get("followed", True)),
str(payload.get("name", "")),
)
)
elif parsed.path == "/api/import/legacy":
self._send_json(self.gallery_service.scan_legacy_downloads(root=str(payload.get("root", "Sakura_Downloads"))))
elif parsed.path == "/api/sync/followed":
self._send_json(
self.gallery_service.sync_followed_artists(
cookie=str(payload.get("cookie", "")),
max_new_per_artist=int(payload.get("max_new", 20)),
download=bool(payload.get("download", True)),
)
)
elif parsed.path == "/api/cookies/import":
self._send_json(self._handle_cookie_import(payload))
elif parsed.path == "/api/origin-follows/sync":
self._send_json(
self.origin_follow_sync_service.sync(
str(payload.get("platform", "")),
)
)
elif parsed.path == "/api/x/media/preview":
self._send_json(
self.x_media_service.preview_author_media(
str(payload.get("username", "")),
media_type=str(payload.get("media_type", "all")),
max_items=int(payload.get("max_items", 120)),
)
)
elif parsed.path == "/api/x/media/download":
selected_ids = payload.get("selected_ids")
if selected_ids is not None and not isinstance(selected_ids, list):
raise ValueError("selected_ids 必须是数组。")
self._send_json(
self.x_media_service.download_preview(
str(payload.get("preview_id", "")),
selected_ids=[str(item) for item in selected_ids] if selected_ids is not None else None,
)
)
elif parsed.path == "/api/x/local-media/delete":
media_ids = payload.get("ids", [])
if not isinstance(media_ids, list):
raise ValueError("ids 必须是数组。")
self._send_json(self.x_media_service.delete_local_media([str(item) for item in media_ids]))
elif parsed.path == "/api/x/follows":
self._send_json(
self.x_media_service.set_followed_author(
str(payload.get("username", "")),
followed=bool(payload.get("followed", True)),
creator_name=str(payload.get("name", "")),
)
)
elif parsed.path == "/api/x/follows/sync":
self._send_json(
self.x_media_service.sync_followed_authors(
download=bool(payload.get("download", True)),
max_new_per_author=int(payload.get("max_new", 20)),
)
)
elif parsed.path == "/api/xiaohongshu/preview":
self._send_json(self._handle_plugin_preview("小红书", payload))
elif parsed.path == "/api/xiaohongshu/download":
self._send_json(self._handle_plugin_download(payload))
else:
self._send_json({"error": "not found"}, status=404)
except ValueError as exc:
self._send_json({"error": str(exc)}, status=400)
except Exception as exc:
self._send_json({"error": str(exc)}, status=500)
def do_OPTIONS(self) -> None:
self.send_response(204)
self._send_cors_headers()
self.end_headers()
def log_message(self, format: str, *args) -> None:
return None
def _handle_artworks(self, query: dict[str, list[str]]) -> dict:
artist_id = _first(query, "artist_id")
return self.gallery_service.list_artworks(
query=_first(query, "q"),
search_scope=_first(query, "search_scope") or "all",
tag=_first(query, "tag"),
artist_id=int(artist_id) if artist_id else None,
artwork_type=_first(query, "type"),
include_restricted=_bool(query, "include_restricted", True),
limit=_int(query, "limit", 60),
offset=_int(query, "offset", 0),
ensure_thumbnails=_bool(query, "ensure_thumbnails", True),
)
def _handle_plugin_pages(self) -> dict:
self.plugin_manager.reload_all_from_disk()
items = []
if "X" in self.plugin_manager.plugins:
items.append(
{
"id": "x-media",
"view": "xMedia",
"title": "X 作者媒体",
"description": "按作者名称解析图片或视频并选择下载",
}
)
if "小红书" in self.plugin_manager.plugins:
plugin = self.plugin_manager.plugins["小红书"]
items.append(
{
"id": "xiaohongshu",
"view": "xiaohongshu",
"title": "小红书",
"description": "搜索关键词或解析笔记链接,预览后下载",
"available": bool(plugin.validate()),
}
)
return {"items": items}
def _handle_plugin_preview(self, plugin_name: str, payload: dict) -> dict:
plugin = self.plugin_manager.plugins.get(plugin_name)
if plugin is None:
raise ValueError(f"未安装插件: {plugin_name}")
if not plugin.validate():
raise ValueError(f"{plugin_name} 插件依赖不可用,请先安装 Playwright。")
target = str(payload.get("target", "")).strip()
if not target:
raise ValueError("请输入解析目标。")
limit = max(1, min(int(payload.get("limit", 20)), 50))
resources = plugin.parse(target)[:limit]
preview_id = uuid.uuid4().hex
self.plugin_preview_cache[preview_id] = (plugin_name, resources)
return {
"preview_id": preview_id,
"count": len(resources),
"items": [_resource_to_json(item, i) for i, item in enumerate(resources)],
}
def _handle_plugin_download(self, payload: dict) -> dict:
preview_id = str(payload.get("preview_id", ""))
cached = self.plugin_preview_cache.get(preview_id)
if cached is None:
raise ValueError("预览结果已过期,请重新解析。")
plugin_name, resources = cached
plugin = self.plugin_manager.plugins.get(plugin_name)
if plugin is None:
raise ValueError(f"插件已卸载: {plugin_name}")
selected_ids = payload.get("selected_ids")
selected = set(str(item) for item in selected_ids) if isinstance(selected_ids, list) else set()
if selected:
resources = [item for item in resources if item.id in selected]
save_root = downloads_root()
results = []
downloaded = 0
failed = 0
for resource in resources:
try:
paths = plugin.download(resource, save_root)
downloaded += len(paths)
results.append({
"id": resource.id,
"ok": bool(paths),
"paths": [str(path) for path in paths],
"synced_files": 0,
})
except Exception as exc:
failed += 1
results.append({"id": resource.id, "ok": False, "error": str(exc)})
return {
"requested": len(resources),
"downloaded_files": downloaded,
"synced_files": 0,
"failed_items": failed,
"items": results,
}
def _handle_artwork_detail(self, path: str) -> dict:
artwork_id = int(path.rsplit("/", 1)[-1])
item = self.gallery_service.get_artwork(artwork_id)
if item is None:
raise ValueError(f"Artwork not found: {artwork_id}")
return item
def _handle_artist_detail(self, path: str, query: dict[str, list[str]]) -> dict:
artist_id = int(path.rsplit("/", 1)[-1])
item = self.gallery_service.get_artist(
artist_id,
limit=_int(query, "limit", 60),
offset=_int(query, "offset", 0),
)
if item is None:
raise ValueError(f"Artist not found: {artist_id}")
return item
def _resolve_media_path(self, path: str, *, thumbnail: bool) -> Path:
parts = path.strip("/").split("/")
if len(parts) != 4:
raise ValueError("Expected /media/{thumbnail|source}/{artwork_id}/{page_index}")
artwork_id = int(parts[2])
page_index = int(parts[3])
media_path = (
self.gallery_service.get_thumbnail_path(artwork_id, page_index=page_index)
if thumbnail
else self.gallery_service.get_source_file_path(artwork_id, page_index=page_index)
)
if media_path is None:
raise ValueError("Media file not found")
return media_path
def _handle_cookie_import(self, payload: dict) -> dict:
platform = str(payload.get("platform", "")).strip().lower()
text = str(payload.get("text", ""))
if platform in {"pixiv", "px"}:
cookies = parse_cookie_text(text)
pixiv_cookies = cookies_to_playwright(cookies, ("pixiv.net",))
header = cookies_to_header(pixiv_cookies, ("pixiv.net",))
store = SessionStore(app_session_file())
session_payload = store.load()
session_payload.update(
{
"login_mode": "cookie",
"cookie": header,
"cookie_json": pixiv_cookies,
"login_id": "",
"saved_at": time.strftime("%Y-%m-%d %H:%M:%S"),
}
)
store.save(session_payload)
return {"platform": "pixiv", "count": len(pixiv_cookies), "summary": cookie_summary(pixiv_cookies)}
if platform in {"x", "twitter"}:
result = self.x_media_service.import_cookie_text(text)
result["platform"] = "x"
return result
raise ValueError("platform 必须是 pixiv 或 x。")
def _handle_session_status(self) -> dict:
payload = SessionStore(app_session_file()).load()
pixiv_cookie = str(payload.get("cookie") or "").strip()
cookie_json = payload.get("cookie_json")
cloud = payload.get("cloud_sync") if isinstance(payload.get("cloud_sync"), dict) else {}
return {
"pixiv": {
"has_cookie": bool(pixiv_cookie or cookie_json),
"saved_at": str(payload.get("saved_at") or ""),
"login_mode": str(payload.get("login_mode") or ""),
},
"cloud": {
"enabled": bool(cloud.get("enabled")),
"has_token": bool(str(cloud.get("access_token") or "").strip()),
"base_url": str(cloud.get("base_url") or "").strip(),
},
}
def _send_json(self, payload: dict | list, *, status: int = 200) -> None:
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json; charset=utf-8")
self._send_cors_headers()
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def _send_file(self, path: Path) -> None:
content_type = mimetypes.guess_type(path.name)[0] or "application/octet-stream"
body = path.read_bytes()
self.send_response(200)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def _send_static_file(self, path: Path) -> None:
if not path.exists() or not path.is_file():
self._send_json({"error": "not found"}, status=404)
return
self._send_file(path)
def _send_cors_headers(self) -> None:
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
def _read_json_body(self) -> dict:
length = int(self.headers.get("Content-Length", "0") or "0")
if length == 0:
return {}
body = self.rfile.read(length)
payload = json.loads(body.decode("utf-8"))
if not isinstance(payload, dict):
raise ValueError("JSON body must be an object")
return payload
def _first(query: dict[str, list[str]], key: str, default: str = "") -> str:
values = query.get(key)
if not values:
return default
return values[0]
def _int(query: dict[str, list[str]], key: str, default: int) -> int:
value = _first(query, key)
if not value:
return default
return int(value)
def _bool(query: dict[str, list[str]], key: str, default: bool) -> bool:
value = _first(query, key)
if not value:
return default
return value.lower() in {"1", "true", "yes", "on"}
def _resource_to_json(resource, index: int) -> dict:
payload = asdict(resource)
payload["index"] = index
payload["kind"] = "video" if any(file.get("type") == "video" for file in resource.files) else "image"
payload["file_count"] = len(resource.files)
return payload
def _plugin_platform_slug(plugin_name: str) -> str:
lowered = plugin_name.lower()
if "douyin" in lowered or "抖音" in plugin_name:
return "douyin"
if "xiaohongshu" in lowered or "小红书" in plugin_name:
return "xiaohongshu"
if lowered == "x":
return "x"
return lowered.replace(" ", "_")[:20] or "plugin"
def _resource_file_type(resource, index: int, path: Path) -> str:
if index < len(resource.files):
file_type = str(resource.files[index].get("type") or "").lower()
if file_type == "video":
return "video"
mime_type = mimetypes.guess_type(str(path))[0] or ""
return "video" if mime_type.startswith("video/") else "illust"
+30
View File
@@ -0,0 +1,30 @@
from __future__ import annotations
import argparse
import logging
from pixiv_app.services.gallery_api import GalleryApiServer
def main() -> None:
parser = argparse.ArgumentParser(description="Run local Sakura gallery API")
parser.add_argument("--host", default="127.0.0.1")
parser.add_argument("--port", type=int, default=8765)
parser.add_argument("--db", default="pixiv_app_library.db")
parser.add_argument("--thumbnail-dir", default="runtime/thumbnails")
parser.add_argument("--web-root", default="")
args = parser.parse_args()
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
logging.info("Local gallery API listening on http://%s:%s", args.host, args.port)
GalleryApiServer(
host=args.host,
port=args.port,
db_path=args.db,
thumbnail_dir=args.thumbnail_dir,
web_root=args.web_root or None,
).serve_forever()
if __name__ == "__main__":
main()
+356
View File
@@ -0,0 +1,356 @@
"""
X (Twitter) authentication helper — Playwright **persistent context** only.
- First-time login is always manual (including email verification); no credential stuffing.
- Session persistence is Chromium's user_data_dir (cookies + localStorage live inside it).
- Optional JSON cookie export into ``profiles/x/<profile>/cookies/`` for backup only.
Directory layout (created automatically)::
profiles/
x/
<profile_key>/
chromium_profile/ # user_data_dir → persistent session (real storage)
cookies/ # optional Playwright cookie exports (JSON)
localStorage/ # reserved / notes (Chromium keeps LS inside chromium_profile)
Usage::
auth = XAuthManager(profile_key="default")
await auth.start_session()
page = await auth.get_page()
await page.goto("https://x.com/i/flow/login")
# ... user completes login manually ...
if await auth.is_logged_in():
...
await auth.close()
"""
from __future__ import annotations
import asyncio
import json
import logging
from pathlib import Path
from typing import Any
from playwright.async_api import BrowserContext, Page, Playwright, async_playwright
from pixiv_app.core.paths import profiles_root
from pixiv_app.core.playwright_runtime import configure_playwright_runtime_env, preferred_chromium_executable
logger = logging.getLogger(__name__)
def _default_profiles_root() -> Path:
return profiles_root()
class XAuthManager:
"""
Manage a long-lived Playwright Chromium **persistent** profile for x.com.
Not thread-safe: use one instance per logical browser session, or guard externally.
"""
HOME_URL = "https://x.com/home"
LOGIN_FLOW_HINT = "https://x.com/i/flow/login"
def __init__(
self,
*,
profile_key: str = "default",
profiles_root: Path | None = None,
headless: bool = True,
auto_reconnect: bool = True,
max_health_retries: int = 2,
) -> None:
"""
:param profile_key: Subdirectory under ``profiles/x/`` for multi-account switching.
:param profiles_root: Defaults to ``<project>/profiles``.
:param headless: Headless for automated checks; use ``False`` for first manual login.
:param auto_reconnect: If ``start_session`` finds a dead context, restart Playwright.
:param max_health_retries: Retries when validating session after transient failures.
"""
self._profile_key = profile_key
root = profiles_root or _default_profiles_root()
self._profile_home = root / "x" / profile_key
self._user_data_dir = self._profile_home / "chromium_profile"
self._cookies_dir = self._profile_home / "cookies"
self._localstorage_dir = self._profile_home / "localStorage"
self._headless = headless
self._auto_reconnect = auto_reconnect
self._max_health_retries = max(1, max_health_retries)
self._pw: Playwright | None = None
self._context: BrowserContext | None = None
self._primary_page: Page | None = None
self._ensure_storage_layout()
def _ensure_storage_layout(self) -> None:
"""Create profile dirs. Real cookie/localStorage persistence is under ``chromium_profile``."""
self._user_data_dir.mkdir(parents=True, exist_ok=True)
self._cookies_dir.mkdir(parents=True, exist_ok=True)
self._localstorage_dir.mkdir(parents=True, exist_ok=True)
@property
def user_data_dir(self) -> Path:
"""Playwright ``user_data_dir`` passed to ``launch_persistent_context``."""
return self._user_data_dir
def _context_alive(self) -> bool:
if self._context is None:
return False
try:
_ = self._context.pages
return True
except Exception:
return False
async def start_session(self, *, force_new: bool = False) -> BrowserContext:
"""
Launch or reuse persistent Chromium. Session is restored from disk automatically.
:param force_new: Close existing context and open a fresh one (same profile dir).
"""
if self._context_alive() and not force_new:
return self._context # type: ignore[return-value]
if self._context is not None:
await self.close()
self._pw = await async_playwright().start()
try:
self._context = await self._launch_persistent(self._pw)
except Exception:
await self._pw.stop()
self._pw = None
raise
self._primary_page = None
logger.info("XAuthManager: persistent context ready (%s)", self._user_data_dir)
return self._context
async def _launch_persistent(self, pw: Playwright) -> BrowserContext:
"""Single place for launch_persistent_context configuration."""
self._user_data_dir.mkdir(parents=True, exist_ok=True)
configure_playwright_runtime_env()
base_kw: dict[str, Any] = {
"user_data_dir": str(self._user_data_dir),
"headless": self._headless,
"viewport": {"width": 1280, "height": 800},
"locale": "zh-CN",
"ignore_default_args": ["--enable-automation"],
"args": ["--disable-blink-features=AutomationControlled"],
"user_agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
),
}
import os
env_ch = os.environ.get("X_PLAYWRIGHT_CHANNEL", "").strip()
local_executable = preferred_chromium_executable()
attempts: list[dict[str, Any]] = []
seen: set[str] = set()
for extra in (
{"channel": env_ch} if env_ch else {},
{"executable_path": str(local_executable)} if local_executable else {},
{"channel": "chrome"},
{},
):
key = repr(extra)
if key in seen:
continue
seen.add(key)
attempts.append(extra)
last_err: Exception | None = None
for extra in attempts:
kw = dict(base_kw)
kw.update(extra)
try:
ctx = await pw.chromium.launch_persistent_context(**kw)
await ctx.add_init_script(
"""
try {
Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
} catch (e) {}
"""
)
return ctx
except Exception as exc:
last_err = exc
logger.warning("launch_persistent_context failed (%s): %s", extra, exc)
continue
assert last_err is not None
raise last_err
async def get_context(self) -> BrowserContext:
if not self._context_alive():
if self._auto_reconnect:
return await self.start_session()
raise RuntimeError("No browser session; call start_session() first.")
return self._context # type: ignore[return-value]
async def get_page(self, *, new: bool = False) -> Page:
"""
Return a page for navigation. Default reuses one tab; set ``new=True`` for a fresh tab.
"""
ctx = await self.get_context()
if new:
return await ctx.new_page()
if self._primary_page is not None:
try:
_ = self._primary_page.url
return self._primary_page
except Exception:
self._primary_page = None
if ctx.pages:
self._primary_page = ctx.pages[0]
return self._primary_page
self._primary_page = await ctx.new_page()
return self._primary_page
async def is_logged_in(self, *, page: Page | None = None, navigate: bool = True) -> bool:
"""
Heuristic login detection (DOM + URL). Not guaranteed if X changes UI.
- ``False`` if still on login / signup flow URLs or login UI is visible.
- ``True`` if home timeline / primary column or account UI is present.
"""
p = page if page is not None else await self.get_page()
if navigate:
await p.goto(self.HOME_URL, wait_until="domcontentloaded", timeout=45_000)
await asyncio.sleep(0.6)
url = (p.url or "").lower()
if any(
x in url
for x in (
"/i/flow/login",
"/i/flow/signup",
"/login",
)
):
return False
# Logged-in indicators (best-effort; X changes selectors periodically)
logged_in_selectors = (
'[data-testid="SideNav_AccountSwitcher_Button"]',
'[data-testid="AppTabBar_Home_Link"]',
'[data-testid="primaryColumn"]',
'[data-testid="tweet"]',
)
for sel in logged_in_selectors:
try:
el = await p.wait_for_selector(sel, timeout=2500, state="visible")
if el:
return True
except Exception:
continue
# Login / gate modal text (guest wall)
try:
guest = p.get_by_text(
"Sign in to X",
exact=False,
)
if await guest.count() and await guest.first.is_visible(timeout=800):
return False
except Exception:
pass
return False
async def session_healthy(self) -> bool:
"""True if browser context responds and ``is_logged_in`` succeeds within retries."""
for attempt in range(self._max_health_retries):
try:
if not self._context_alive():
return False
return await self.is_logged_in(navigate=True)
except Exception as exc:
logger.debug("session_healthy attempt %s: %s", attempt + 1, exc)
await asyncio.sleep(1.0)
return False
async def export_playwright_cookies(self, filename: str = "playwright_cookies.json") -> Path:
"""Optional backup: write context cookies to ``profiles/x/<key>/cookies/``."""
ctx = await self.get_context()
cookies = await ctx.cookies()
path = self._cookies_dir / filename
path.write_text(json.dumps(cookies, indent=2), encoding="utf-8")
return path
async def close(self) -> None:
"""Close Playwright; persistent data remains on disk for next run."""
if self._primary_page is not None:
try:
await self._primary_page.close()
except Exception:
pass
self._primary_page = None
if self._context is not None:
try:
await self._context.close()
except Exception:
pass
self._context = None
if self._pw is not None:
try:
await self._pw.stop()
except Exception:
pass
self._pw = None
logger.info("XAuthManager: closed (%s)", self._profile_key)
# --- sync helpers for Tkinter / scripts without asyncio ---
def start_session_sync(self, *, force_new: bool = False) -> BrowserContext:
return asyncio.run(self.start_session(force_new=force_new))
def get_page_sync(self, *, new: bool = False) -> Page:
return asyncio.run(self.get_page(new=new))
def is_logged_in_sync(self, *, navigate: bool = True) -> bool:
return asyncio.run(self.is_logged_in(navigate=navigate))
def close_sync(self) -> None:
asyncio.run(self.close())
async def _demo() -> None:
logging.basicConfig(level=logging.INFO)
auth = XAuthManager(profile_key="default", headless=False)
try:
await auth.start_session()
page = await auth.get_page()
await page.goto(XAuthManager.LOGIN_FLOW_HINT, wait_until="domcontentloaded")
print(
"浏览器已打开登录流程。请在窗口中手动完成登录(含邮箱验证),完成后回到终端按 Enter…",
)
await asyncio.to_thread(input)
ok = await auth.is_logged_in()
print("is_logged_in:", ok)
if ok:
path = await auth.export_playwright_cookies()
print("Cookies 备份:", path)
finally:
await auth.close()
if __name__ == "__main__":
asyncio.run(_demo())
+156
View File
@@ -0,0 +1,156 @@
"""
Helpers for X (Twitter) web login flow in Playwright.
X often opens a combined sheet titled 「注册 X」 even from /i/flow/login; users must switch to
the actual sign-in path or they loop after entering email. These helpers only automate UI clicks
(no credential stuffing, no bypass).
"""
from __future__ import annotations
import asyncio
import re
from playwright.async_api import Page
LOGIN_URL_X = "https://x.com/i/flow/login"
LOGIN_URL_TWITTER = "https://twitter.com/i/flow/login"
# When X shows this, login inside Playwright often cannot proceed (rate limit / bot heuristics).
X_FLOW_ERROR_RECOVERY_HINT_CN = (
"这是 X 端返回的通用错误,常见于自动化浏览器或风控,并不代表本程序损坏。\n\n"
"可行做法(任选):\n"
"1)点击弹窗「重试」后等待几分钟再试,或更换网络(如手机热点);\n"
"2)在插件管理中「清除本 Profile 浏览器数据」后重新「手动登录」;\n"
"3)用本机普通 Chrome 登录 x.com,导出 Cookie 为 JSON,放到 runtime/x_cookies.json,再点「同步到抓取」。\n\n"
"我们无法绕过 X/Google 的风控策略。"
)
async def detect_x_generic_flow_error(page: Page) -> bool:
"""True if the grey modal 「出错了 / Something went wrong」 is likely visible."""
try:
dlg = page.locator('[role="dialog"]')
if await dlg.count() > 0:
text = (await dlg.first.inner_text())[:1200]
low = text.lower()
if ("出错了" in text or "something went wrong" in low) and (
"重试" in text or "reload" in low or "retry" in low or "重新加载" in text
):
return True
except Exception:
pass
return False
async def prefer_login_over_signup(page: Page, *, max_attempts: int = 5) -> None:
"""
If the modal looks like sign-up (注册 X / Sign up for X), try to switch to login.
Safe to call multiple times; no-op when login sheet is already shown.
"""
for _ in range(max_attempts):
await asyncio.sleep(0.35)
# Already looks like login-oriented sheet (best-effort)
try:
login_heading = page.get_by_role("heading", name=re.compile(r"登录.*X|Sign in to X|登录或注册", re.I))
if await login_heading.count() > 0 and await login_heading.first.is_visible(timeout=400):
return
except Exception:
pass
signup_visible = False
try:
su = page.get_by_text(re.compile(r"注册 X|Sign up for X"), exact=False)
if await su.count() > 0:
signup_visible = await su.first.is_visible(timeout=500)
except Exception:
pass
if not signup_visible:
try:
dlg = page.locator('[role="dialog"]')
if await dlg.count() > 0:
t = await dlg.first.inner_text()
if "注册 X" in t or "Sign up for X" in t:
signup_visible = True
except Exception:
pass
if not signup_visible:
return
clicked = await _try_click_login_switch(page)
if not clicked:
break
async def _try_click_login_switch(page: Page) -> bool:
"""Return True if a qualifying control was clicked."""
# Known Twitter/X test ids (may change)
for tid in ("signupSheetSwitchToLogin", "switch_to_login"):
try:
loc = page.locator(f'[data-testid="{tid}"]')
if await loc.count() > 0 and await loc.first.is_visible(timeout=600):
await loc.first.click(timeout=3000)
await asyncio.sleep(0.8)
return True
except Exception:
continue
# Footer / inverse of 「还没有账号?注册」 → often 「登录」 link nearby
try:
inv = page.get_by_text(re.compile(r"已有账号|已有帐户|Already have an account", re.I))
if await inv.count() > 0:
await inv.first.click(timeout=3500)
await asyncio.sleep(0.85)
return True
except Exception:
pass
try:
plain = page.get_by_role("link", name=re.compile(r"^(登录|Sign in)$", re.I))
if await plain.count() > 0:
await plain.first.click(timeout=3500)
await asyncio.sleep(0.85)
return True
except Exception:
pass
try:
href_login = page.locator('a[href*="login"]').filter(has_not_text=re.compile(r"Google|Apple", re.I))
if await href_login.count() > 0 and await href_login.first.is_visible(timeout=600):
await href_login.first.click(timeout=3500)
await asyncio.sleep(0.85)
return True
except Exception:
pass
try:
link = page.locator('a[href="/login"]').first
if await link.is_visible(timeout=600):
await link.click(timeout=3500)
await asyncio.sleep(0.85)
return True
except Exception:
pass
return False
async def goto_x_login_entry(page: Page, *, timeout: float = 90_000) -> None:
"""Navigate to official login flow and try to leave sign-up shell."""
await page.goto(LOGIN_URL_X, wait_until="domcontentloaded", timeout=timeout)
await prefer_login_over_signup(page)
# Some locales keep showing 「注册 X」 on x.com; twitter.com entry occasionally differs.
try:
su = page.get_by_text(re.compile(r"注册 X|Sign up for X"), exact=False)
if await su.count() > 0 and await su.first.is_visible(timeout=700):
await page.goto(LOGIN_URL_TWITTER, wait_until="domcontentloaded", timeout=timeout)
await prefer_login_over_signup(page)
except Exception:
pass
+19
View File
@@ -0,0 +1,19 @@
"""Task models, SQLite-backed queue, batch parser, and watermark helpers."""
from pixiv_app.tasks.models import DownloadTaskSpec, ParsePreview, ParsedLine, TaskPriority
from pixiv_app.tasks.parser import parse_batch_for_mode, split_input_blob
from pixiv_app.tasks.queue import TaskQueue
from pixiv_app.tasks.watermark import advance_watermark, filter_newer_than_watermark, get_last_synced_illust_id
__all__ = [
"DownloadTaskSpec",
"ParsePreview",
"ParsedLine",
"TaskPriority",
"TaskQueue",
"advance_watermark",
"filter_newer_than_watermark",
"get_last_synced_illust_id",
"parse_batch_for_mode",
"split_input_blob",
]
+50
View File
@@ -0,0 +1,50 @@
from __future__ import annotations
from dataclasses import dataclass, field
from enum import IntEnum
from typing import Any, Literal
class TaskPriority(IntEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
TaskStatus = Literal["pending", "running", "done", "failed"]
@dataclass(frozen=True)
class DownloadTaskSpec:
"""Serializable unit stored in SQLite via TaskQueue (payload omits secrets)."""
target_type: Literal["illust", "novel"]
target_id: int
task_kind: str
priority: TaskPriority
page_index: int | None = None
payload: dict[str, Any] = field(default_factory=dict)
def fingerprint(self) -> str:
page_part = self.page_index if self.page_index is not None else -1
return f"{self.task_kind}:{self.target_type}:{self.target_id}:{page_part}"
@dataclass
class ParsedLine:
"""One logical input token after splitting (before expansion to illust ids)."""
category: Literal["user", "illust", "novel", "tag", "keyword"]
id_value: int
tag: str = ""
keyword_text: str = ""
raw_token: str = ""
pixiv_kind: str = "user"
@dataclass
class ParsePreview:
"""Statistics for GUI preview."""
total_lines: int
by_category: dict[str, int]
+89
View File
@@ -0,0 +1,89 @@
from __future__ import annotations
import re
from typing import Literal
from pixiv_app.core.downloader import parse_pixiv_target
from pixiv_app.tasks.models import ParsePreview, ParsedLine
def split_input_blob(blob: str) -> list[str]:
parts = re.split(r"[\s,,;;\n\r]+", blob.strip())
return [p for p in parts if p]
def parse_batch_for_mode(
raw: str,
*,
ui_mode: Literal["user", "illust", "manga", "novel", "keyword"],
) -> tuple[list[ParsedLine], ParsePreview]:
"""
Parse multi-line / comma / space separated input into structured intents.
ui_mode matches GUI combo selection so validation stays aligned with existing rules.
"""
tokens = split_input_blob(raw)
lines: list[ParsedLine] = []
stats: dict[str, int] = {}
def bump(key: str) -> None:
stats[key] = stats.get(key, 0) + 1
if ui_mode == "keyword":
kw = " ".join(tokens) if tokens else raw.strip()
if kw.startswith("#"):
kw = kw[1:].strip()
if kw:
lines.append(ParsedLine(category="keyword", id_value=0, keyword_text=kw, raw_token=raw.strip()))
bump("keyword")
return lines, ParsePreview(total_lines=len(lines), by_category=stats)
for token in tokens:
t = token.strip()
if not t:
continue
if t.startswith("#"):
tag = t[1:].strip()
if tag:
lines.append(ParsedLine(category="tag", id_value=0, tag=tag, raw_token=t))
bump("tag")
continue
try:
kind, num_id = parse_pixiv_target(t)
except ValueError:
continue
if ui_mode == "novel":
if kind == "novel":
lines.append(ParsedLine(category="novel", id_value=num_id, raw_token=t, pixiv_kind=kind))
bump("novel")
elif kind == "novel_series":
lines.append(ParsedLine(category="novel", id_value=num_id, raw_token=t, pixiv_kind=kind))
bump("novel")
elif kind == "unknown":
lines.append(ParsedLine(category="novel", id_value=num_id, raw_token=t, pixiv_kind=kind))
bump("novel")
continue
if ui_mode in {"illust", "manga"}:
if kind == "illust":
lines.append(ParsedLine(category="illust", id_value=num_id, raw_token=t, pixiv_kind=kind))
bump("illust")
elif kind == "unknown":
lines.append(ParsedLine(category="illust", id_value=num_id, raw_token=t, pixiv_kind=kind))
bump("illust")
continue
# ui_mode == "user"
if kind == "user":
lines.append(ParsedLine(category="user", id_value=num_id, raw_token=t, pixiv_kind=kind))
bump("user")
elif kind == "illust":
lines.append(ParsedLine(category="user", id_value=num_id, raw_token=t, pixiv_kind=kind))
bump("user")
elif kind == "unknown":
lines.append(ParsedLine(category="user", id_value=num_id, raw_token=t, pixiv_kind=kind))
bump("user")
return lines, ParsePreview(total_lines=len(lines), by_category=stats)
+238
View File
@@ -0,0 +1,238 @@
from __future__ import annotations
import json
import sqlite3
import threading
import time
from pixiv_app.tasks.models import DownloadTaskSpec
class TaskQueue:
"""SQLite-backed queue with lease/reclaim; thread-safe for concurrent workers."""
def __init__(self, conn: sqlite3.Connection) -> None:
self._conn = conn
self._lock = threading.Lock()
def enqueue(self, tasks: list[DownloadTaskSpec]) -> tuple[int, int]:
inserted = 0
with self._lock:
with self._conn:
for t in tasks:
fp = t.fingerprint()
cur = self._conn.execute(
"""
INSERT OR IGNORE INTO download_tasks (
fingerprint, target_type, target_id, page_index, task_kind,
priority, status, payload_json
)
VALUES (?, ?, ?, ?, ?, ?, 'pending', ?)
""",
(
fp,
t.target_type,
t.target_id,
t.page_index,
t.task_kind,
int(t.priority),
json.dumps(t.payload, ensure_ascii=False),
),
)
inserted += cur.rowcount
skipped = len(tasks) - inserted
return inserted, skipped
def requeue_failed_by_fingerprints(self, fingerprints: list[str]) -> int:
"""Reset `failed` rows to `pending` so a retry can pick them up."""
if not fingerprints:
return 0
placeholders = ",".join("?" * len(fingerprints))
with self._lock:
with self._conn:
cur = self._conn.execute(
f"""
UPDATE download_tasks
SET status = 'pending',
leased_until = NULL,
worker_id = NULL,
last_error = '',
updated_at = CURRENT_TIMESTAMP
WHERE fingerprint IN ({placeholders}) AND status = 'failed'
""",
fingerprints,
)
return int(cur.rowcount or 0)
def requeue_done_by_fingerprints(self, fingerprints: list[str]) -> int:
"""Reset completed rows to `pending` so the user can rerun the same task batch."""
if not fingerprints:
return 0
placeholders = ",".join("?" * len(fingerprints))
with self._lock:
with self._conn:
cur = self._conn.execute(
f"""
UPDATE download_tasks
SET status = 'pending',
attempts = 0,
leased_until = NULL,
worker_id = NULL,
last_error = '',
updated_at = CURRENT_TIMESTAMP
WHERE fingerprint IN ({placeholders}) AND status = 'done'
""",
fingerprints,
)
return int(cur.rowcount or 0)
def count_incomplete_for_fingerprints(self, fingerprints: list[str]) -> int:
"""Rows for these fingerprints that are not successfully finished (`done`)."""
if not fingerprints:
return 0
placeholders = ",".join("?" * len(fingerprints))
with self._lock:
row = self._conn.execute(
f"""
SELECT COUNT(*) FROM download_tasks
WHERE fingerprint IN ({placeholders}) AND status != 'done'
""",
fingerprints,
).fetchone()
return int(row[0]) if row else 0
def lease(
self,
worker_id: str,
*,
limit: int = 4,
lease_seconds: float = 180.0,
) -> list[int]:
"""Grab up to `limit` tasks; reclaims expired leases."""
now = time.time()
deadline = now + lease_seconds
ids: list[int] = []
with self._lock:
with self._conn:
for _ in range(limit):
cur = self._conn.execute(
"""
WITH picked AS (
SELECT id FROM download_tasks
WHERE status = 'pending'
OR (status = 'running' AND leased_until IS NOT NULL AND leased_until < ?)
ORDER BY priority DESC, id ASC
LIMIT 1
)
UPDATE download_tasks
SET status = 'running',
worker_id = ?,
leased_until = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id IN (SELECT id FROM picked)
RETURNING id
""",
(now, worker_id, deadline),
)
row = cur.fetchone()
if row is None:
break
ids.append(int(row[0]))
return ids
def load_task(self, task_id: int) -> sqlite3.Row | None:
with self._lock:
row = self._conn.execute(
"SELECT * FROM download_tasks WHERE id = ?",
(task_id,),
).fetchone()
return row
def mark_done(self, task_id: int) -> None:
with self._lock:
with self._conn:
self._conn.execute(
"""
UPDATE download_tasks
SET status = 'done',
leased_until = NULL,
worker_id = NULL,
last_error = '',
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""",
(task_id,),
)
def mark_failed_final(self, task_id: int, message: str) -> None:
with self._lock:
with self._conn:
self._conn.execute(
"""
UPDATE download_tasks
SET status = 'failed',
leased_until = NULL,
worker_id = NULL,
last_error = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""",
(message[:2000], task_id),
)
def requeue_retry(self, task_id: int, message: str, max_attempts: int = 4) -> bool:
"""Return True if scheduled for another attempt."""
with self._lock:
with self._conn:
row = self._conn.execute(
"SELECT attempts FROM download_tasks WHERE id = ?",
(task_id,),
).fetchone()
if row is None:
return False
attempts = int(row[0]) + 1
if attempts >= max_attempts:
self._conn.execute(
"""
UPDATE download_tasks
SET status = 'failed',
attempts = ?,
leased_until = NULL,
worker_id = NULL,
last_error = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""",
(attempts, message[:2000], task_id),
)
return False
self._conn.execute(
"""
UPDATE download_tasks
SET status = 'pending',
attempts = ?,
leased_until = NULL,
worker_id = NULL,
last_error = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
""",
(attempts, message[:2000], task_id),
)
return True
def count_by_status(self, status: str) -> int:
with self._lock:
row = self._conn.execute(
"SELECT COUNT(*) FROM download_tasks WHERE status = ?",
(status,),
).fetchone()
return int(row[0]) if row else 0
def snapshot_counts(self) -> dict[str, int]:
with self._lock:
rows = self._conn.execute(
"""
SELECT status, COUNT(*) FROM download_tasks GROUP BY status
"""
).fetchall()
return {str(r[0]): int(r[1]) for r in rows}
+34
View File
@@ -0,0 +1,34 @@
from __future__ import annotations
import sqlite3
def get_last_synced_illust_id(conn: sqlite3.Connection, artist_id: int) -> int:
row = conn.execute(
"SELECT last_synced_illust_id FROM artist_sync_watermark WHERE artist_id = ?",
(artist_id,),
).fetchone()
return int(row[0]) if row else 0
def advance_watermark(conn: sqlite3.Connection, artist_id: int, illust_id: int) -> None:
"""Raise watermark to at least illust_id for incremental author sync."""
with conn:
conn.execute(
"""
INSERT INTO artist_sync_watermark (artist_id, last_synced_illust_id, updated_at)
VALUES (?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(artist_id) DO UPDATE SET
last_synced_illust_id = MAX(
artist_sync_watermark.last_synced_illust_id,
excluded.last_synced_illust_id
),
updated_at = CURRENT_TIMESTAMP
""",
(artist_id, illust_id),
)
def filter_newer_than_watermark(work_ids: list[int], watermark: int) -> list[int]:
"""Keep illust ids strictly greater than last synced watermark (Pixiv ids grow over time)."""
return [wid for wid in work_ids if wid > watermark]
+947
View File
@@ -0,0 +1,947 @@
const state = {
view: "home",
query: "",
tag: "",
type: "",
artistId: "",
offset: 0,
limit: 48,
total: 0,
xPreviewId: "",
xPreviewUsername: "",
xPreviewAuthor: "",
xhsPreviewId: "",
};
const els = {
search: document.querySelector("#searchInput"),
type: document.querySelector("#typeFilter"),
status: document.querySelector("#statusLine"),
grid: document.querySelector("#artworkGrid"),
tags: document.querySelector("#tagList"),
artists: document.querySelector("#artistList"),
followed: document.querySelector("#followedList"),
xFollowed: document.querySelector("#xFollowedList"),
title: document.querySelector("#viewTitle"),
subtitle: document.querySelector("#viewSubtitle"),
pageInfo: document.querySelector("#pageInfo"),
dialog: document.querySelector("#detailDialog"),
detail: document.querySelector("#detailContent"),
pixivCookieFile: document.querySelector("#pixivCookieFile"),
pixivSessionStatus: document.querySelector("#pixivSessionStatus"),
refreshSessionButton: document.querySelector("#refreshSessionButton"),
xCookieFile: document.querySelector("#xCookieFile"),
xUsername: document.querySelector("#xUsernameInput"),
xMediaType: document.querySelector("#xMediaType"),
xMaxItems: document.querySelector("#xMaxItems"),
xMediaGrid: document.querySelector("#xMediaGrid"),
xMediaSummary: document.querySelector("#xMediaSummary"),
xSelectAll: document.querySelector("#xSelectAll"),
xFollowUsername: document.querySelector("#xFollowUsername"),
xFollowName: document.querySelector("#xFollowName"),
xLocalGrid: document.querySelector("#xLocalGrid"),
xLocalSummary: document.querySelector("#xLocalSummary"),
xLocalSelectAll: document.querySelector("#xLocalSelectAll"),
xhsNavButton: document.querySelector("#xiaohongshuNavButton"),
xhsTarget: document.querySelector("#xhsTargetInput"),
xhsLimit: document.querySelector("#xhsLimitInput"),
xhsGrid: document.querySelector("#xhsGrid"),
xhsSummary: document.querySelector("#xhsSummary"),
xhsSelectAll: document.querySelector("#xhsSelectAll"),
originFollowSyncDialog: document.querySelector("#originFollowSyncDialog"),
originFollowSyncResult: document.querySelector("#originFollowSyncResult"),
};
function setStatus(text) {
els.status.textContent = text;
}
function isPlaceholderPixivName(name, artistId) {
const value = String(name || "").trim();
if (!value) {
return true;
}
const idText = String(artistId || "").trim();
return Boolean(idText) && value.toLowerCase() === `artist_${idText}`.toLowerCase();
}
function pixivArtistStoredName(item) {
const artistId = item?.artist_id ?? item?.artistId ?? item?.creator_id ?? "";
const name = String(item?.name || item?.artist_name || item?.creator_name || "").trim();
return isPlaceholderPixivName(name, artistId) ? "" : name;
}
function pixivArtistDisplayName(item) {
const artistId = item?.artist_id ?? item?.artistId ?? item?.creator_id ?? "";
const name = pixivArtistStoredName(item);
if (name) {
return name;
}
const account = String(item?.account || item?.artist_account || item?.creator_handle || "").trim().replace(/^@/, "");
if (account) {
return `@${account}`;
}
return artistId ? `Pixiv ID ${artistId}` : "Pixiv 作者";
}
window.addEventListener("unhandledrejection", (event) => {
setStatus(event.reason?.message || "操作失败,请查看后端日志。");
});
async function api(path, options = {}) {
const response = await fetch(path, {
headers: { "Content-Type": "application/json" },
...options,
});
const payload = await response.json();
if (!response.ok || payload.error) {
throw new Error(payload.error || `HTTP ${response.status}`);
}
return payload;
}
async function loadSessionStatus() {
if (!els.pixivSessionStatus) {
return null;
}
try {
const payload = await api("/api/session/status");
const pixiv = payload.pixiv || {};
const cookieText = pixiv.has_cookie
? `已共享桌面端 Pixiv 会话${pixiv.saved_at ? `(${pixiv.saved_at})` : ""}`
: "未检测到桌面端 Pixiv 会话,请先在 EXE 登录或导入 Cookie。";
els.pixivSessionStatus.textContent = cookieText;
return payload;
} catch (error) {
els.pixivSessionStatus.textContent = `会话状态读取失败:${error.message || error}`;
return null;
}
}
function artworkUrl(item, pageIndex = 0, kind = "thumbnail") {
return `/media/${kind}/${item.artwork_id}/${pageIndex}`;
}
function renderArtworkGrid(payload) {
state.total = payload.total || 0;
els.grid.innerHTML = "";
if (!payload.items.length) {
els.grid.innerHTML = `<div class="empty">还没有作品。可以先下载作品,或点击“扫描旧目录”导入已有文件。</div>`;
} else {
els.grid.append(...payload.items.map(renderArtworkCard));
}
const currentPage = Math.floor(state.offset / state.limit) + 1;
const totalPages = Math.max(1, Math.ceil(state.total / state.limit));
els.pageInfo.textContent = `${currentPage} / ${totalPages}`;
}
function renderArtworkCard(item) {
const card = document.createElement("article");
card.className = "artwork-card";
const thumb = item.thumbnail_path
? `<img loading="lazy" src="${artworkUrl(item)}" alt="${escapeHtml(item.title || String(item.artwork_id))}">`
: `<div class="thumb-placeholder">${item.type === "novel" ? "小说" : "暂无缩略图"}</div>`;
card.innerHTML = `
<button class="thumb-button" data-open="${item.artwork_id}">${thumb}</button>
<div class="artwork-meta">
<div class="artwork-title">${escapeHtml(item.title || String(item.artwork_id))}</div>
<div class="artwork-sub">${escapeHtml(item.artist_name || "未知作者")} · ${escapeHtml(item.type || "")}</div>
<div class="card-actions">
<button class="small-button ${item.is_favorite ? "active" : ""}" data-fav="${item.artwork_id}" data-on="${item.is_favorite ? 1 : 0}">
${item.is_favorite ? "已收藏" : "收藏"}
</button>
${item.artist_id ? `<button class="small-button" data-artist="${item.artist_id}">作者</button>` : ""}
${item.artist_id ? `<button class="small-button ${Number(item.is_followed || 0) ? "active" : ""}" data-follow="${item.artist_id}" data-name="${escapeHtml(pixivArtistStoredName(item))}" data-followed="${Number(item.is_followed || 0)}">${Number(item.is_followed || 0) ? "取消关注" : "关注作者"}</button>` : ""}
</div>
</div>
`;
const artworkSub = card.querySelector(".artwork-sub");
if (artworkSub) {
artworkSub.textContent = `${pixivArtistDisplayName(item)} · ${item.type || ""}`;
}
const artworkFollow = card.querySelector("[data-follow]");
if (artworkFollow) {
artworkFollow.dataset.name = pixivArtistStoredName(item);
}
return card;
}
async function loadArtworks() {
const params = new URLSearchParams({
limit: state.limit,
offset: state.offset,
ensure_thumbnails: "true",
});
if (state.query) params.set("q", state.query);
if (state.tag) params.set("tag", state.tag);
if (state.type) params.set("type", state.type);
if (state.artistId) params.set("artist_id", state.artistId);
setStatus("正在加载图库...");
const payload = await api(`/api/artworks?${params}`);
renderArtworkGrid(payload);
setStatus(`已加载 ${payload.items.length} / ${payload.total} 个作品`);
}
async function loadFavorites() {
setStatus("正在加载收藏...");
const params = new URLSearchParams({ limit: state.limit, offset: state.offset });
const payload = await api(`/api/favorites?${params}`);
renderArtworkGrid(payload);
setStatus(`收藏 ${payload.total} 个作品`);
}
async function loadTags() {
const tags = await api("/api/tags?limit=80");
els.tags.innerHTML = "";
if (!tags.length) {
els.tags.innerHTML = `<span class="tag-chip">暂无标签</span>`;
return;
}
for (const tag of tags) {
const button = document.createElement("button");
button.className = `tag-chip ${state.tag === tag.name ? "active" : ""}`;
button.textContent = `${tag.name} ${tag.artwork_count}`;
button.dataset.tag = tag.name;
els.tags.append(button);
}
}
async function loadArtists() {
setStatus("正在加载作者...");
const artists = await api("/api/artists?limit=200");
els.artists.innerHTML = "";
if (!artists.length) {
els.artists.innerHTML = `<div class="empty">暂无作者数据。</div>`;
} else {
els.artists.append(...artists.map(renderArtistRow));
}
setStatus(`已加载 ${artists.length} 位作者`);
}
async function loadFollowed() {
const followed = await api("/api/followed-artists");
els.followed.innerHTML = "";
if (!followed.length) {
els.followed.innerHTML = `<div class="empty">还没有关注作者。</div>`;
} else {
els.followed.append(...followed.map(renderArtistRow));
}
}
async function loadXFollowed() {
const followed = await api("/api/x/follows");
els.xFollowed.innerHTML = "";
if (!followed.length) {
els.xFollowed.innerHTML = `<div class="empty">还没有 X 关注作者。</div>`;
} else {
els.xFollowed.append(...followed.map(renderXFollowRow));
}
}
function renderArtistRow(artist) {
const row = document.createElement("div");
row.className = "artist-row";
const isFollowed = Number(artist.enabled ?? artist.is_followed ?? 0);
row.innerHTML = `
<div>
<strong>${escapeHtml(artist.name || `artist_${artist.artist_id}`)}</strong>
<span>ID ${artist.artist_id} · ${artist.artwork_count || 0} 个作品${artist.last_synced_at ? ` · 上次同步 ${artist.last_synced_at}` : ""}</span>
</div>
<div class="card-actions">
<button class="small-button" data-artist="${artist.artist_id}">详情</button>
<button class="small-button ${isFollowed ? "active" : ""}" data-follow="${artist.artist_id}" data-name="${escapeHtml(artist.name || "")}" data-followed="${isFollowed ? 1 : 0}">${isFollowed ? "取消关注" : "关注"}</button>
</div>
`;
const artistTitle = row.querySelector("strong");
if (artistTitle) {
artistTitle.textContent = pixivArtistDisplayName(artist);
}
const artistFollow = row.querySelector("[data-follow]");
if (artistFollow) {
artistFollow.dataset.name = pixivArtistStoredName(artist);
}
return row;
}
function renderXFollowRow(artist) {
const row = document.createElement("div");
row.className = "artist-row";
const handle = artist.username || artist.creator_handle || "";
const isFollowed = Number(artist.enabled ?? 1);
const latest = artist.latest_published_at ? ` · 最新 ${artist.latest_published_at}` : "";
const checked = artist.last_checked_at ? ` · 检查 ${artist.last_checked_at}` : "";
const error = artist.last_error ? ` · ${escapeHtml(artist.last_error)}` : "";
row.innerHTML = `
<div>
<strong>${escapeHtml(artist.creator_name || handle || "x_author")}</strong>
<span>@${escapeHtml(handle)}${latest}${checked}${error}</span>
</div>
<div class="card-actions">
<button class="small-button" data-open-x="${escapeHtml(handle)}">去预览</button>
<button class="small-button ${isFollowed ? "active" : ""}" data-follow-x="${escapeHtml(handle)}" data-name="${escapeHtml(artist.creator_name || "")}" data-followed-x="${isFollowed ? 1 : 0}">${isFollowed ? "取消关注" : "关注"}</button>
</div>
`;
return row;
}
async function openArtwork(artworkId) {
const payload = await api(`/api/artworks/${artworkId}`);
const artwork = payload.artwork;
const files = payload.files || [];
const tags = payload.tags || [];
const firstFile = files[0];
const image = firstFile
? `<img src="/media/source/${artwork.artwork_id}/${firstFile.page_index}" alt="${escapeHtml(artwork.title)}">`
: `<div class="thumb-placeholder">没有本地文件</div>`;
els.detail.innerHTML = `
<div class="detail-layout">
<div class="detail-media">${image}</div>
<div class="detail-info">
<h2>${escapeHtml(artwork.title || String(artwork.artwork_id))}</h2>
<p>${escapeHtml(artwork.artist_name || "未知作者")} · ${escapeHtml(artwork.type)}</p>
<div class="card-actions">
<button class="small-button ${artwork.is_favorite ? "active" : ""}" data-fav="${artwork.artwork_id}" data-on="${artwork.is_favorite ? 1 : 0}">
${artwork.is_favorite ? "已收藏" : "收藏"}
</button>
${artwork.artist_id ? `<button class="small-button ${Number(artwork.is_followed || 0) ? "active" : ""}" data-follow="${artwork.artist_id}" data-name="${escapeHtml(artwork.artist_name || "")}" data-followed="${Number(artwork.is_followed || 0)}">${Number(artwork.is_followed || 0) ? "取消关注" : "关注作者"}</button>` : ""}
</div>
<div class="detail-tags">
${tags.map((tag) => `<button class="tag-chip" data-tag="${escapeHtml(tag.name)}">${escapeHtml(tag.name)}</button>`).join("")}
</div>
<p>${escapeHtml(stripHtml(artwork.caption || ""))}</p>
<p>页数 ${files.length || artwork.page_count || 0} · 收藏 ${artwork.bookmark_count || 0} · 浏览 ${artwork.view_count || 0}</p>
</div>
</div>
`;
const detailArtist = els.detail.querySelector(".detail-info p");
if (detailArtist) {
detailArtist.textContent = `${pixivArtistDisplayName(artwork)} · ${artwork.type}`;
}
const detailFollow = els.detail.querySelector("[data-follow]");
if (detailFollow) {
detailFollow.dataset.name = pixivArtistStoredName(artwork);
}
els.dialog.showModal();
}
async function showArtist(artistId) {
setActiveView("gallery");
state.artistId = String(artistId);
state.tag = "";
state.offset = 0;
const detail = await api(`/api/artists/${artistId}?limit=1`);
els.title.textContent = detail.artist.name || `作者 ${artistId}`;
els.subtitle.textContent = `作者 ID ${artistId} · ${detail.artist.artwork_count || 0} 个本地作品`;
await loadArtworks();
els.title.textContent = pixivArtistDisplayName(detail.artist);
}
async function toggleFavorite(artworkId, currentOn) {
await api("/api/favorites", {
method: "POST",
body: JSON.stringify({ artwork_id: Number(artworkId), favorite: !Number(currentOn) }),
});
await reloadCurrent();
}
async function followArtist(artistId, name = "") {
await setPixivFollow(artistId, name, true);
}
async function setPixivFollow(artistId, name = "", followed = true) {
await api("/api/followed-artists", {
method: "POST",
body: JSON.stringify({ artist_id: String(artistId).trim(), name, followed }),
});
setStatus(followed ? `已关注作者 ${artistId}` : `已取消关注作者 ${artistId}`);
await Promise.all([loadFollowed(), reloadCurrent()]);
}
async function followXAuthor(username, name = "") {
await setXFollow(username, name, true);
}
async function setXFollow(username, name = "", followed = true) {
const handle = username.trim().replace(/^@/, "");
if (!handle) {
return;
}
await api("/api/x/follows", {
method: "POST",
body: JSON.stringify({ username: handle, name, followed }),
});
setStatus(followed ? `已关注 X 作者 @${handle}` : `已取消关注 X 作者 @${handle}`);
await loadXFollowed();
}
async function scanLegacy() {
setStatus("正在扫描旧下载目录...");
const result = await api("/api/import/legacy", { method: "POST", body: JSON.stringify({ root: "Sakura_Downloads" }) });
setStatus(`扫描完成:导入 ${result.imported_files} 个文件,${result.imported_artworks} 个作品`);
await Promise.all([loadTags(), reloadCurrent()]);
}
async function syncFollowed() {
setStatus("正在同步关注作者...");
try {
const result = await api("/api/sync/followed", {
method: "POST",
body: JSON.stringify({ download: true, max_new: 20 }),
});
const downloaded = result.artists.reduce((sum, item) => sum + (item.downloaded || 0), 0);
setStatus(`同步完成:下载 ${downloaded} 个新作品`);
await Promise.all([loadFollowed(), loadTags(), reloadCurrent()]);
} catch (error) {
setStatus(error.message || "同步关注作者失败");
}
}
async function syncXFollowed() {
setStatus("正在同步 X 关注...");
const result = await api("/api/x/follows/sync", {
method: "POST",
body: JSON.stringify({ download: true, max_new: 20 }),
});
const total = (result.authors || []).reduce((sum, item) => sum + (item.new_posts || 0), 0);
setStatus(`X 同步完成:发现 ${total} 个新动态`);
await loadXFollowed();
}
function openOriginFollowSyncDialog() {
els.originFollowSyncResult.textContent = "请选择要同步的平台。";
els.originFollowSyncDialog.showModal();
}
async function syncOriginFollows(platform) {
const label = platform === "pixiv" ? "Pixiv" : "X";
const button = platform === "pixiv" ? document.querySelector("#syncOriginPixivButton") : document.querySelector("#syncOriginXButton");
if (button?.disabled) {
return;
}
if (button) {
button.disabled = true;
}
setStatus(`正在同步 ${label} 原站关注...`);
els.originFollowSyncResult.textContent = `正在同步 ${label} 原站关注,请稍候...`;
try {
if (platform === "pixiv") {
const session = await loadSessionStatus();
if (session && !session.pixiv?.has_cookie) {
throw new Error("未检测到桌面端 Pixiv 会话,请先在 EXE 登录或导入 Cookie。");
}
}
const result = await api("/api/origin-follows/sync", {
method: "POST",
body: JSON.stringify({ platform }),
});
const lines = [
`${label} 原站关注同步完成`,
`同步页数:${result.pages || 0}`,
`新增:${result.created || 0} 位`,
`更新:${result.updated || 0} 位`,
`跳过:${result.skipped || 0} 位`,
`失败:${result.failed || 0} 位`,
];
if (Array.isArray(result.progress) && result.progress.length) {
lines.push("", "进度:");
for (const item of result.progress.slice(-20)) {
lines.push(item.message || `正在同步第 ${item.page || "?"} 页`);
}
}
if (Array.isArray(result.errors) && result.errors.length) {
lines.push("", "失败原因:");
for (const item of result.errors.slice(0, 10)) {
lines.push(`${item.id || "-"}:${item.reason || ""}`);
}
}
els.originFollowSyncResult.textContent = lines.join("\n");
setStatus(`${label} 原站关注同步完成:新增 ${result.created || 0},更新 ${result.updated || 0},跳过 ${result.skipped || 0},失败 ${result.failed || 0}`);
if (platform === "pixiv") {
await loadFollowed();
} else {
await loadXFollowed();
}
} catch (error) {
const message = error.message || `${label} 原站关注同步失败`;
els.originFollowSyncResult.textContent = message;
setStatus(message);
} finally {
if (button) {
button.disabled = false;
}
}
}
async function followCurrentX() {
const username = els.xUsername.value.trim();
if (!username) {
setStatus("请先输入 X 作者名。");
return;
}
const handle = username.replace(/^@/, "");
const creatorName = state.xPreviewUsername === handle ? state.xPreviewAuthor : "";
await followXAuthor(username, creatorName || handle);
}
async function buildThumbnails() {
setStatus("正在生成缩略图...");
const result = await api("/api/thumbnails/build?limit=500");
setStatus(`缩略图完成:处理 ${result.items.length} 个文件`);
await reloadCurrent();
}
async function loadPluginPages() {
const payload = await api("/api/plugins/pages");
const pages = payload.items || [];
const hasXhs = pages.some((item) => item.view === "xiaohongshu");
els.xhsNavButton.classList.toggle("hidden", !hasXhs);
}
async function importCookieFile(platform, fileInput) {
const file = fileInput.files?.[0];
if (!file) {
setStatus("请先选择 cookies.txt 或 json 文件。");
return;
}
setStatus(`正在导入 ${platform} Cookie...`);
const text = await file.text();
const result = await api("/api/cookies/import", {
method: "POST",
body: JSON.stringify({ platform, text }),
});
setStatus(`Cookie 导入完成:${result.summary || `${result.count || 0} 个 Cookie`}`);
}
async function previewXMedia(event) {
event.preventDefault();
const username = els.xUsername.value.trim();
if (!username) {
setStatus("请输入 X 作者名称。");
return;
}
els.xMediaGrid.innerHTML = `<div class="empty">正在解析 X 媒体页,首次启动 Playwright 可能需要一些时间。</div>`;
els.xMediaSummary.textContent = "解析中...";
const submitButton = event.submitter || document.querySelector("#xMediaForm button[type='submit']");
if (submitButton) {
submitButton.disabled = true;
}
setStatus(`正在解析 X 作者 @${username.replace(/^@/, "")}...`);
try {
const payload = await api("/api/x/media/preview", {
method: "POST",
body: JSON.stringify({
username,
media_type: els.xMediaType.value,
max_items: Number(els.xMaxItems.value || 120),
}),
});
state.xPreviewId = payload.preview_id;
renderXMedia(payload);
setStatus(`X 解析完成:${payload.count} 个媒体项`);
} catch (error) {
state.xPreviewId = "";
els.xMediaSummary.textContent = "解析失败";
els.xMediaGrid.innerHTML = `<div class="empty">${escapeHtml(error.message || "X 解析失败")}</div>`;
setStatus(error.message || "X 解析失败");
} finally {
if (submitButton) {
submitButton.disabled = false;
}
}
}
function renderXMedia(payload) {
els.xMediaGrid.innerHTML = "";
els.xMediaSummary.textContent = `${payload.username} · ${payload.count} 个媒体项`;
if (!payload.items.length) {
els.xMediaGrid.innerHTML = `<div class="empty">没有解析到媒体。可能需要先导入 X Cookie,或该作者媒体页为空。</div>`;
return;
}
els.xMediaGrid.append(...payload.items.map(renderXMediaCard));
els.xSelectAll.checked = true;
}
function renderXMediaCard(item) {
const card = document.createElement("article");
card.className = "x-media-card";
const preview = item.thumbnail || item.files?.[0]?.url || "";
const media = item.kind === "video"
? `<div class="video-tile">VIDEO</div>`
: `<img loading="lazy" referrerpolicy="no-referrer" src="${escapeHtml(preview)}" alt="${escapeHtml(item.title || item.id)}">`;
card.innerHTML = `
<label class="x-check">
<input type="checkbox" data-x-id="${escapeHtml(item.id)}" checked />
<span>${item.kind === "video" ? "视频" : "图片"}</span>
</label>
<div class="x-thumb">${media}</div>
<div class="artwork-meta">
<div class="artwork-title">${escapeHtml(item.title || item.id)}</div>
<div class="artwork-sub">@${escapeHtml(item.author_id || "")} · ${item.file_count || 1} 个文件</div>
</div>
`;
return card;
}
async function downloadXSelected() {
if (!state.xPreviewId) {
setStatus("请先解析 X 作者媒体。");
return;
}
const selected = [...document.querySelectorAll("[data-x-id]:checked")].map((item) => item.dataset.xId);
if (!selected.length) {
setStatus("没有选中的媒体。");
return;
}
setStatus(`正在下载 ${selected.length} 个 X 媒体项...`);
const result = await api("/api/x/media/download", {
method: "POST",
body: JSON.stringify({ preview_id: state.xPreviewId, selected_ids: selected }),
});
setStatus(`X 下载完成:保存 ${result.downloaded_files} 个文件,失败 ${result.failed_items} 项`);
await loadXLocalMedia();
}
async function loadXLocalMedia() {
const payload = await api("/api/x/local-media?limit=160");
els.xLocalGrid.innerHTML = "";
els.xLocalSummary.textContent = `本地 ${payload.total || 0} 个文件`;
if (!payload.items.length) {
els.xLocalGrid.innerHTML = `<div class="empty">还没有本地 X 图片或视频。</div>`;
return;
}
els.xLocalGrid.append(...payload.items.map(renderXLocalCard));
}
function renderXLocalCard(item) {
const card = document.createElement("article");
card.className = "x-media-card";
const media = item.kind === "video"
? `<video src="${escapeHtml(item.url)}" muted preload="metadata" controls></video>`
: `<img loading="lazy" src="${escapeHtml(item.url)}" alt="${escapeHtml(item.name)}">`;
card.innerHTML = `
<label class="x-check">
<input type="checkbox" data-x-local-id="${escapeHtml(item.id)}" />
<span>${item.kind === "video" ? "视频" : "图片"}</span>
</label>
<div class="x-thumb">${media}</div>
<div class="artwork-meta">
<div class="artwork-title">${escapeHtml(item.name)}</div>
<div class="artwork-sub">${escapeHtml(item.relative_path)} · ${formatBytes(item.size)}</div>
</div>
`;
return card;
}
async function deleteXLocalSelected() {
const selected = [...document.querySelectorAll("[data-x-local-id]:checked")].map((item) => item.dataset.xLocalId);
if (!selected.length) {
setStatus("没有选中的本地 X 文件。");
return;
}
setStatus(`正在删除 ${selected.length} 个本地 X 文件...`);
const result = await api("/api/x/local-media/delete", {
method: "POST",
body: JSON.stringify({ ids: selected }),
});
setStatus(`删除完成:${result.deleted.length} 个,失败 ${result.failed.length} 个`);
els.xLocalSelectAll.checked = false;
await loadXLocalMedia();
}
async function previewXhs(event) {
event.preventDefault();
const target = els.xhsTarget.value.trim();
if (!target) {
setStatus("请输入小红书关键词或笔记链接。");
return;
}
els.xhsGrid.innerHTML = `<div class="empty">正在解析小红书内容...</div>`;
els.xhsSummary.textContent = "解析中...";
const payload = await api("/api/xiaohongshu/preview", {
method: "POST",
body: JSON.stringify({ target, limit: Number(els.xhsLimit.value || 20) }),
});
state.xhsPreviewId = payload.preview_id;
renderXhs(payload);
setStatus(`小红书解析完成:${payload.count} 个项目`);
}
function renderXhs(payload) {
els.xhsGrid.innerHTML = "";
els.xhsSummary.textContent = `${payload.count} 个项目`;
if (!payload.items.length) {
els.xhsGrid.innerHTML = `<div class="empty">没有解析到内容,可能需要先登录或换一个关键词。</div>`;
return;
}
els.xhsGrid.append(...payload.items.map(renderXhsCard));
els.xhsSelectAll.checked = true;
}
function renderXhsCard(item) {
const card = document.createElement("article");
card.className = "x-media-card";
const preview = item.thumbnail || item.metadata?.cover_url || "";
const media = preview
? `<img loading="lazy" referrerpolicy="no-referrer" src="${escapeHtml(preview)}" alt="${escapeHtml(item.title || item.id)}">`
: `<div class="thumb-placeholder">暂无预览</div>`;
card.innerHTML = `
<label class="x-check">
<input type="checkbox" data-xhs-id="${escapeHtml(item.id)}" checked />
<span>笔记</span>
</label>
<div class="x-thumb">${media}</div>
<div class="artwork-meta">
<div class="artwork-title">${escapeHtml(item.title || item.id)}</div>
<div class="artwork-sub">${escapeHtml(item.author || "")} · ${item.file_count || 0} 个文件</div>
</div>
`;
return card;
}
async function downloadXhsSelected() {
if (!state.xhsPreviewId) {
setStatus("请先解析小红书内容。");
return;
}
const selected = [...document.querySelectorAll("[data-xhs-id]:checked")].map((item) => item.dataset.xhsId);
if (!selected.length) {
setStatus("没有选中的小红书项目。");
return;
}
setStatus(`正在下载 ${selected.length} 个小红书项目...`);
const result = await api("/api/xiaohongshu/download", {
method: "POST",
body: JSON.stringify({ preview_id: state.xhsPreviewId, selected_ids: selected }),
});
setStatus(`小红书下载完成:保存 ${result.downloaded_files} 个文件,失败 ${result.failed_items} 项`);
}
function setActiveView(view) {
state.view = view;
document.querySelectorAll(".nav-button").forEach((button) => {
button.classList.toggle("active", button.dataset.view === view);
});
document.querySelectorAll(".view").forEach((section) => section.classList.remove("active"));
if (view === "home") {
document.querySelector("#homeView").classList.add("active");
} else if (view === "artists") {
document.querySelector("#artistsView").classList.add("active");
} else if (view === "sync") {
document.querySelector("#syncView").classList.add("active");
} else if (view === "xMedia") {
document.querySelector("#xMediaView").classList.add("active");
} else if (view === "xiaohongshu") {
document.querySelector("#xiaohongshuView").classList.add("active");
} else {
document.querySelector("#galleryView").classList.add("active");
}
}
async function reloadCurrent() {
if (state.view === "home") {
setStatus("首页已就绪。选择一个入口开始整理或同步内容。");
} else if (state.view === "favorites") {
els.title.textContent = "本地收藏";
els.subtitle.textContent = "你标记收藏的作品";
await loadFavorites();
} else if (state.view === "artists") {
await loadArtists();
} else if (state.view === "sync") {
await Promise.all([loadFollowed(), loadXFollowed()]);
} else if (state.view === "xMedia") {
await loadXLocalMedia();
setStatus("X 作者媒体页已就绪。");
} else if (state.view === "xiaohongshu") {
setStatus("小红书插件页已就绪。");
} else {
if (!state.artistId) {
els.title.textContent = state.tag ? `标签:${state.tag}` : "全部作品";
els.subtitle.textContent = state.query ? `搜索:${state.query}` : "本地已入库作品";
}
await loadArtworks();
}
}
function clearFilters() {
state.query = "";
state.tag = "";
state.type = "";
state.artistId = "";
state.offset = 0;
els.search.value = "";
els.type.value = "";
}
function escapeHtml(value) {
return String(value ?? "").replace(/[&<>"']/g, (char) => ({
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#39;",
})[char]);
}
function stripHtml(value) {
const tmp = document.createElement("div");
tmp.innerHTML = value;
return tmp.textContent || tmp.innerText || "";
}
function formatBytes(value) {
const size = Number(value || 0);
if (size < 1024) return `${size} B`;
if (size < 1024 * 1024) return `${(size / 1024).toFixed(1)} KB`;
return `${(size / 1024 / 1024).toFixed(1)} MB`;
}
document.querySelectorAll(".nav-button").forEach((button) => {
button.addEventListener("click", async () => {
setActiveView(button.dataset.view);
state.offset = 0;
if (state.view !== "gallery") {
state.artistId = "";
}
await reloadCurrent();
});
});
els.search.addEventListener("input", debounce(async () => {
state.query = els.search.value.trim();
state.offset = 0;
setActiveView("gallery");
await reloadCurrent();
}, 280));
els.type.addEventListener("change", async () => {
state.type = els.type.value;
state.offset = 0;
setActiveView("gallery");
await reloadCurrent();
});
document.addEventListener("click", async (event) => {
const target = event.target.closest("button");
if (!target) return;
if (target.dataset.viewAction) {
setActiveView(target.dataset.viewAction);
state.offset = 0;
await reloadCurrent();
return;
}
if (target.dataset.open) await openArtwork(target.dataset.open);
if (target.dataset.fav) await toggleFavorite(target.dataset.fav, target.dataset.on);
if (target.dataset.artist) await showArtist(target.dataset.artist);
if (target.dataset.tag) {
state.tag = target.dataset.tag;
state.artistId = "";
state.offset = 0;
setActiveView("gallery");
await loadTags();
await reloadCurrent();
}
if (target.dataset.follow) {
const nextFollowed = target.dataset.followed === "1" ? false : true;
await setPixivFollow(target.dataset.follow, target.dataset.name || "", nextFollowed);
}
if (target.dataset.followX) {
const nextFollowed = target.dataset.followedX === "1" ? false : true;
await setXFollow(target.dataset.followX, target.dataset.name || "", nextFollowed);
}
if (target.dataset.openX) {
els.xUsername.value = `@${target.dataset.openX}`;
setActiveView("xMedia");
setStatus(`已填入 @${target.dataset.openX},可以直接解析最新媒体。`);
}
});
document.querySelector("#clearFiltersButton").addEventListener("click", async () => {
clearFilters();
await loadTags();
await reloadCurrent();
});
document.querySelector("#prevPageButton").addEventListener("click", async () => {
state.offset = Math.max(0, state.offset - state.limit);
await reloadCurrent();
});
document.querySelector("#nextPageButton").addEventListener("click", async () => {
if (state.offset + state.limit < state.total) {
state.offset += state.limit;
await reloadCurrent();
}
});
document.querySelector("#scanButton").addEventListener("click", scanLegacy);
document.querySelector("#homeScanButton")?.addEventListener("click", scanLegacy);
document.querySelector("#originFollowSyncButton").addEventListener("click", openOriginFollowSyncDialog);
document.querySelector("#closeOriginFollowSyncDialogButton").addEventListener("click", () => els.originFollowSyncDialog.close());
document.querySelector("#syncOriginPixivButton").addEventListener("click", () => syncOriginFollows("pixiv"));
document.querySelector("#syncOriginXButton").addEventListener("click", () => syncOriginFollows("x"));
document.querySelector("#syncButton").addEventListener("click", syncFollowed);
document.querySelector("#syncXButton").addEventListener("click", syncXFollowed);
document.querySelector("#buildThumbsButton").addEventListener("click", buildThumbnails);
document.querySelector("#homeThumbButton")?.addEventListener("click", buildThumbnails);
document.querySelector("#closeDialogButton").addEventListener("click", () => els.dialog.close());
document.querySelector("#importPixivCookieButton")?.addEventListener("click", () => importCookieFile("pixiv", els.pixivCookieFile));
els.refreshSessionButton?.addEventListener("click", loadSessionStatus);
document.querySelector("#importXCookieButton").addEventListener("click", () => importCookieFile("x", els.xCookieFile));
document.querySelector("#xMediaForm").addEventListener("submit", previewXMedia);
document.querySelector("#followCurrentXButton").addEventListener("click", followCurrentX);
document.querySelector("#downloadXSelectedButton").addEventListener("click", downloadXSelected);
document.querySelector("#refreshXLocalButton").addEventListener("click", loadXLocalMedia);
document.querySelector("#deleteXLocalButton").addEventListener("click", deleteXLocalSelected);
document.querySelector("#xhsForm").addEventListener("submit", previewXhs);
document.querySelector("#downloadXhsSelectedButton").addEventListener("click", downloadXhsSelected);
els.xSelectAll.addEventListener("change", () => {
document.querySelectorAll("[data-x-id]").forEach((item) => {
item.checked = els.xSelectAll.checked;
});
});
els.xLocalSelectAll.addEventListener("change", () => {
document.querySelectorAll("[data-x-local-id]").forEach((item) => {
item.checked = els.xLocalSelectAll.checked;
});
});
els.xhsSelectAll.addEventListener("change", () => {
document.querySelectorAll("[data-xhs-id]").forEach((item) => {
item.checked = els.xhsSelectAll.checked;
});
});
document.querySelector("#followForm").addEventListener("submit", async (event) => {
event.preventDefault();
const id = document.querySelector("#followArtistId").value.trim();
const name = document.querySelector("#followArtistName").value.trim();
if (!id) return;
await followArtist(id, name);
event.target.reset();
});
document.querySelector("#xFollowForm").addEventListener("submit", async (event) => {
event.preventDefault();
const username = els.xFollowUsername.value.trim();
const name = els.xFollowName.value.trim();
if (!username) return;
await followXAuthor(username, name);
event.target.reset();
});
function debounce(fn, delay) {
let timer = 0;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}
async function boot() {
try {
await Promise.all([loadPluginPages(), loadSessionStatus(), loadTags(), loadArtworks(), loadFollowed(), loadXFollowed()]);
} catch (error) {
setStatus(error.message);
}
}
boot();
+284
View File
@@ -0,0 +1,284 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Sakura 本地媒体库</title>
<link rel="stylesheet" href="/assets/styles.css" />
</head>
<body>
<aside class="sidebar">
<div class="brand">
<div class="brand-mark">SK</div>
<div>
<h1>Sakura</h1>
<p>本地媒体库</p>
</div>
</div>
<nav class="nav">
<button class="nav-button active" data-view="home">首页</button>
<button class="nav-button" data-view="gallery">图库</button>
<button class="nav-button" data-view="favorites">收藏</button>
<button class="nav-button" data-view="artists">作者</button>
<button class="nav-button" data-view="sync">关注同步</button>
</nav>
<section class="panel plugin-panel">
<h2>插件页面</h2>
<div id="pluginNav" class="nav compact-nav">
<button class="nav-button" data-view="xMedia">X 作者媒体</button>
<button id="xiaohongshuNavButton" class="nav-button hidden" data-view="xiaohongshu">小红书</button>
</div>
</section>
<section class="panel">
<h2>标签</h2>
<div id="tagList" class="tag-list"></div>
</section>
</aside>
<main class="main">
<header class="toolbar">
<div class="search-wrap">
<input id="searchInput" type="search" placeholder="搜索标题、作者、简介或标签" />
<select id="typeFilter" aria-label="类型筛选">
<option value="">全部类型</option>
<option value="illust">插画</option>
<option value="manga">漫画</option>
<option value="novel">小说</option>
<option value="video">视频</option>
</select>
</div>
<div class="toolbar-actions">
<button id="buildThumbsButton" class="ghost-button">生成缩略图</button>
<button id="scanButton" class="primary-button">扫描旧目录</button>
</div>
</header>
<section id="statusLine" class="status-line">准备就绪</section>
<section id="homeView" class="view active">
<div class="welcome-card">
<div>
<p class="eyebrow">本地优先</p>
<h2>今天想看什么?</h2>
<p>插画、漫画、小说和视频都保存在本机。你可以直接浏览、搜索和收藏。</p>
</div>
<button class="primary-button" data-view-action="gallery">打开图库</button>
</div>
<div class="quick-grid">
<button class="quick-card" data-view-action="gallery">
<strong>查看本地图库</strong>
<span>按标题、作者、类型和标签筛选已经入库的作品。</span>
</button>
<button class="quick-card" data-view-action="sync">
<strong>同步关注作者</strong>
<span>读取 Pixiv / X 关注列表,把新作品下载到本地媒体库。</span>
</button>
<button class="quick-card" id="homeScanButton" type="button">
<strong>导入旧下载目录</strong>
<span>把 Sakura_Downloads 里的旧文件重新扫描进图库。</span>
</button>
<button class="quick-card" id="homeThumbButton" type="button">
<strong>修复缩略图</strong>
<span>图片不显示时,重新生成缺失的预览图。</span>
</button>
</div>
<div class="help-steps">
<article>
<b>1</b>
<div>
<strong>在 EXE 主界面导入 Cookie</strong>
<span>Pixiv、X、小红书等登录态会保存到本地运行目录。</span>
</div>
</article>
<article>
<b>2</b>
<div>
<strong>选择平台和目标后开始抓取</strong>
<span>作者、单作品、关键词、插件链接都可以从主界面发起。</span>
</div>
</article>
<article>
<b>3</b>
<div>
<strong>回到图库整理内容</strong>
<span>收藏、关注作者、生成缩略图和查看详情都在这里完成。</span>
</div>
</article>
</div>
</section>
<section id="galleryView" class="view">
<div class="section-head">
<div>
<h2 id="viewTitle">全部作品</h2>
<p id="viewSubtitle">本地已入库作品</p>
</div>
<button id="clearFiltersButton" class="ghost-button">清除筛选</button>
</div>
<div id="artworkGrid" class="artwork-grid"></div>
<div class="pager">
<button id="prevPageButton" class="ghost-button">上一页</button>
<span id="pageInfo">0 / 0</span>
<button id="nextPageButton" class="ghost-button">下一页</button>
</div>
</section>
<section id="artistsView" class="view">
<div class="section-head">
<div>
<h2>作者</h2>
<p>按本地作品数量排序</p>
</div>
</div>
<div id="artistList" class="artist-list"></div>
</section>
<section id="syncView" class="view">
<div class="import-strip">
<div>
<strong>同步原站关注</strong>
<span>读取当前 Pixiv / X 登录账号的关注列表,保存到本地。</span>
</div>
<button id="originFollowSyncButton" class="primary-button" type="button">同步原站关注</button>
</div>
<div class="section-head">
<div>
<h2>关注同步</h2>
<p>使用已保存 Cookie 同步关注作者的新作品</p>
</div>
<button id="syncButton" class="primary-button">同步关注作者</button>
</div>
<div class="import-strip session-strip">
<div>
<strong>Pixiv 登录态</strong>
<span id="pixivSessionStatus">正在读取桌面端已保存的 Pixiv 会话...</span>
</div>
<button id="refreshSessionButton" class="ghost-button" type="button">刷新会话状态</button>
</div>
<form id="followForm" class="follow-form">
<input id="followArtistId" inputmode="text" placeholder="作者 ID、作者名或主页链接" />
<input id="followArtistName" placeholder="备注名称,可选" />
<button class="ghost-button" type="submit">添加关注</button>
</form>
<div id="followedList" class="artist-list"></div>
<div class="section-head">
<div>
<h2>X 关注</h2>
<p>按时间线检查 X 作者的最新媒体推文,并保存到本地媒体库。</p>
</div>
<button id="syncXButton" class="primary-button" type="button">同步 X 关注</button>
</div>
<form id="xFollowForm" class="follow-form">
<input id="xFollowUsername" placeholder="X 用户名,例如 @nasa" />
<input id="xFollowName" placeholder="备注名称,可选" />
<button class="ghost-button" type="submit">添加 X 关注</button>
</form>
<div id="xFollowedList" class="artist-list"></div>
</section>
<section id="xMediaView" class="view">
<div class="section-head">
<div>
<h2>X 作者媒体</h2>
<p>输入作者名称,解析媒体页中的图片或视频,勾选后下载到本地。</p>
</div>
</div>
<div class="import-strip">
<div>
<strong>X Cookie</strong>
<span>导入 Get cookies.txt 导出的 txt/json,会写入 runtime/x_cookies.json。</span>
</div>
<label class="file-button">
选择 txt
<input id="xCookieFile" type="file" accept=".txt,.json" />
</label>
<button id="importXCookieButton" class="ghost-button" type="button">导入 X Cookie</button>
</div>
<form id="xMediaForm" class="x-media-form">
<input id="xUsernameInput" placeholder="作者名称,例如 nasa 或 @nasa" autocomplete="off" />
<select id="xMediaType">
<option value="all">图片和视频</option>
<option value="image">只看图片</option>
<option value="video">只看视频</option>
</select>
<input id="xMaxItems" type="number" min="1" max="500" value="120" />
<button class="primary-button" type="submit">解析媒体</button>
</form>
<div class="x-actions">
<label><input id="xSelectAll" type="checkbox" checked /> 全选</label>
<button id="followCurrentXButton" class="ghost-button" type="button">关注当前作者</button>
<button id="downloadXSelectedButton" class="primary-button" type="button">下载选中</button>
<span id="xMediaSummary">尚未解析</span>
</div>
<div id="xMediaGrid" class="x-media-grid"></div>
<div class="section-head x-local-head">
<div>
<h2>本地 X 媒体</h2>
<p>查看已下载的 X 图片和视频,可删除本地文件。</p>
</div>
<div class="toolbar-actions">
<button id="refreshXLocalButton" class="ghost-button" type="button">刷新</button>
<button id="deleteXLocalButton" class="ghost-button danger-button" type="button">删除选中</button>
</div>
</div>
<div class="x-actions">
<label><input id="xLocalSelectAll" type="checkbox" /> 全选本地文件</label>
<span id="xLocalSummary">等待加载</span>
</div>
<div id="xLocalGrid" class="x-media-grid"></div>
</section>
<section id="xiaohongshuView" class="view">
<div class="section-head">
<div>
<h2>小红书</h2>
<p>输入关键词或笔记链接,先预览再勾选下载。</p>
</div>
</div>
<form id="xhsForm" class="x-media-form">
<input id="xhsTargetInput" placeholder="关键词,或 xiaohongshu.com/explore/... 链接" autocomplete="off" />
<input id="xhsLimitInput" type="number" min="1" max="50" value="20" />
<button class="primary-button" type="submit">解析预览</button>
</form>
<div class="x-actions">
<label><input id="xhsSelectAll" type="checkbox" checked /> 全选</label>
<button id="downloadXhsSelectedButton" class="primary-button" type="button">下载选中</button>
<span id="xhsSummary">等待解析</span>
</div>
<div id="xhsGrid" class="x-media-grid"></div>
</section>
</main>
<dialog id="originFollowSyncDialog" class="detail-dialog">
<div class="dialog-head">
<div>
<strong>同步原站关注</strong>
<span>选择平台后,会读取当前登录账号的关注列表并保存到本地。</span>
</div>
<button id="closeOriginFollowSyncDialogButton" class="ghost-button" type="button">关闭</button>
</div>
<div class="dialog-actions">
<button id="syncOriginPixivButton" class="primary-button" type="button">Pixiv</button>
<button id="syncOriginXButton" class="primary-button" type="button">X</button>
</div>
<pre id="originFollowSyncResult" class="sync-result"></pre>
</dialog>
<dialog id="detailDialog" class="detail-dialog">
<button id="closeDialogButton" class="close-button" aria-label="关闭">×</button>
<div id="detailContent"></div>
</dialog>
<script src="/assets/app.js"></script>
</body>
</html>
+756
View File
@@ -0,0 +1,756 @@
:root {
color-scheme: light;
--bg: #f6f7f9;
--surface: #ffffff;
--surface-soft: #faf5f7;
--line: #e5dce1;
--text: #242229;
--muted: #706b73;
--primary: #cc5f91;
--primary-strong: #af477a;
--accent: #277d78;
--ok: #3f9b72;
--shadow: 0 10px 28px rgba(48, 39, 44, 0.09);
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
display: grid;
grid-template-columns: 248px minmax(0, 1fr);
background: var(--bg);
color: var(--text);
font-family: "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
letter-spacing: 0;
}
button,
input,
select {
font: inherit;
}
button {
cursor: pointer;
}
.sidebar {
min-height: 100vh;
border-right: 1px solid var(--line);
background: #ffffff;
padding: 22px 18px;
position: sticky;
top: 0;
overflow-y: auto;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 24px;
}
.brand-mark {
width: 46px;
height: 46px;
display: grid;
place-items: center;
background: var(--primary);
color: white;
border-radius: 8px;
font-weight: 700;
}
.brand h1,
.brand p,
.panel h2,
.section-head h2,
.section-head p {
margin: 0;
}
.brand h1 {
font-size: 19px;
font-weight: 750;
}
.brand p,
.section-head p {
color: var(--muted);
font-size: 13px;
margin-top: 3px;
}
.nav {
display: grid;
gap: 8px;
margin-bottom: 22px;
}
.nav-button,
.ghost-button,
.primary-button {
border: 1px solid var(--line);
border-radius: 10px;
min-height: 40px;
padding: 0 14px;
background: var(--surface);
color: var(--text);
}
.nav-button {
text-align: left;
font-weight: 650;
}
.nav-button.active,
.primary-button {
background: var(--primary);
border-color: var(--primary);
color: white;
box-shadow: 0 8px 18px rgba(204, 95, 145, 0.2);
}
.primary-button:hover {
background: var(--primary-strong);
}
.ghost-button:hover,
.nav-button:hover {
border-color: var(--primary);
color: var(--primary);
}
.danger-button:hover {
border-color: var(--accent);
color: var(--accent);
}
.panel {
border-top: 1px solid var(--line);
padding-top: 18px;
}
.plugin-panel {
margin-bottom: 18px;
}
.panel h2 {
font-size: 14px;
margin-bottom: 12px;
}
.compact-nav {
margin-bottom: 0;
}
.tag-list {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.tag-chip {
border: 1px solid var(--line);
background: #fff7fb;
border-radius: 999px;
padding: 6px 10px;
color: var(--muted);
max-width: 100%;
}
.tag-chip.active {
color: white;
background: var(--accent);
border-color: var(--accent);
}
.main {
min-width: 0;
padding: 24px 30px 40px;
}
.welcome-card {
border: 1px solid var(--line);
border-radius: 8px;
background: #ffffff;
box-shadow: var(--shadow);
padding: 28px 30px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
margin-bottom: 18px;
}
.welcome-card h2 {
margin: 0 0 8px;
font-size: 28px;
line-height: 1.22;
}
.welcome-card p {
margin: 0;
color: var(--muted);
max-width: 660px;
line-height: 1.7;
}
.eyebrow {
color: var(--primary) !important;
font-size: 13px;
font-weight: 750;
margin-bottom: 6px !important;
}
.quick-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 14px;
margin-bottom: 18px;
}
.quick-card {
border: 1px solid var(--line);
border-radius: 8px;
background: var(--surface);
min-height: 132px;
padding: 18px;
text-align: left;
box-shadow: 0 8px 22px rgba(130, 66, 98, 0.08);
}
.quick-card:hover {
border-color: var(--primary);
transform: translateY(-1px);
}
.quick-card strong,
.quick-card span {
display: block;
}
.quick-card strong {
font-size: 17px;
margin-bottom: 8px;
}
.quick-card span {
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.help-steps {
display: grid;
gap: 12px;
}
.help-steps article {
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.86);
padding: 16px;
display: flex;
gap: 14px;
align-items: flex-start;
}
.help-steps b {
width: 30px;
height: 30px;
border-radius: 999px;
background: #ffe3ef;
color: var(--primary);
display: grid;
place-items: center;
}
.help-steps strong,
.help-steps span {
display: block;
}
.help-steps span {
color: var(--muted);
margin-top: 4px;
}
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 14px;
}
.search-wrap {
display: grid;
grid-template-columns: minmax(220px, 420px) 150px;
gap: 10px;
width: min(100%, 610px);
}
.search-wrap input,
.search-wrap select,
.follow-form input,
.x-media-form input,
.x-media-form select {
height: 42px;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--surface);
color: var(--text);
padding: 0 12px;
min-width: 0;
}
.toolbar-actions {
display: flex;
gap: 10px;
}
.status-line {
min-height: 32px;
color: var(--muted);
font-size: 13px;
background: rgba(255, 255, 255, 0.72);
border: 1px solid rgba(240, 213, 226, 0.8);
border-radius: 8px;
padding: 8px 12px;
margin-bottom: 14px;
}
.view {
display: none;
}
.view.active {
display: block;
}
.hidden {
display: none !important;
}
.section-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.section-head h2 {
font-size: 24px;
}
.artwork-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
gap: 16px;
}
.artwork-card {
border: 1px solid var(--line);
border-radius: 12px;
background: var(--surface);
overflow: hidden;
box-shadow: 0 8px 22px rgba(130, 66, 98, 0.08);
}
.thumb-button {
border: 0;
padding: 0;
width: 100%;
aspect-ratio: 1 / 1;
background: #dde5ec;
display: block;
}
.thumb-button img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.thumb-placeholder {
height: 100%;
display: grid;
place-items: center;
color: var(--muted);
padding: 16px;
text-align: center;
}
.artwork-meta {
padding: 10px;
display: grid;
gap: 7px;
}
.artwork-title {
font-weight: 650;
line-height: 1.35;
min-height: 38px;
}
.artwork-sub {
color: var(--muted);
font-size: 12px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.card-actions {
display: flex;
gap: 8px;
}
.small-button {
border: 1px solid var(--line);
border-radius: 7px;
background: var(--surface-soft);
min-height: 30px;
padding: 0 9px;
color: var(--text);
}
.small-button.active {
background: var(--accent);
border-color: var(--accent);
color: white;
}
.pager {
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
margin: 22px 0 0;
}
.artist-list {
display: grid;
gap: 10px;
}
.artist-row {
border: 1px solid var(--line);
border-radius: 12px;
background: var(--surface);
min-height: 66px;
padding: 12px 14px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.artist-row strong {
display: block;
}
.artist-row span {
color: var(--muted);
font-size: 13px;
}
.follow-form {
display: grid;
grid-template-columns: 160px minmax(180px, 320px) 110px;
gap: 10px;
margin-bottom: 16px;
}
.import-strip {
border: 1px solid var(--line);
border-radius: 12px;
background: var(--surface);
padding: 12px;
display: grid;
grid-template-columns: minmax(220px, 1fr) auto auto;
gap: 10px;
align-items: center;
margin-bottom: 16px;
}
.import-strip strong,
.import-strip span {
display: block;
}
.import-strip span,
.x-actions {
color: var(--muted);
font-size: 13px;
}
.file-button {
border: 1px solid var(--line);
border-radius: 8px;
min-height: 38px;
padding: 0 14px;
display: inline-grid;
place-items: center;
background: var(--surface-soft);
color: var(--text);
cursor: pointer;
}
.file-button input {
display: none;
}
.x-media-form {
display: grid;
grid-template-columns: minmax(220px, 1fr) 150px 120px;
gap: 10px;
margin-bottom: 14px;
}
#xMediaForm {
grid-template-columns: minmax(220px, 1fr) 150px 110px 120px;
}
.x-actions {
min-height: 38px;
display: flex;
align-items: center;
gap: 14px;
margin-bottom: 14px;
}
.x-actions label {
display: flex;
align-items: center;
gap: 6px;
}
.x-media-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 14px;
}
.x-media-card {
border: 1px solid var(--line);
border-radius: 8px;
background: var(--surface);
overflow: hidden;
position: relative;
}
.x-local-head {
border-top: 1px solid var(--line);
padding-top: 22px;
margin-top: 28px;
}
.x-check {
position: absolute;
z-index: 2;
left: 8px;
top: 8px;
min-height: 30px;
display: flex;
align-items: center;
gap: 6px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.92);
padding: 4px 8px;
font-size: 12px;
}
.x-thumb {
width: 100%;
aspect-ratio: 1 / 1;
background: #101820;
display: grid;
place-items: center;
}
.x-thumb img {
width: 100%;
height: 100%;
object-fit: cover;
display: block;
}
.x-thumb video {
width: 100%;
height: 100%;
object-fit: contain;
background: #101820;
display: block;
}
.video-tile {
width: 100%;
height: 100%;
display: grid;
place-items: center;
color: white;
background: #223140;
font-weight: 700;
}
.detail-dialog {
width: min(980px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
border: 0;
border-radius: 14px;
padding: 0;
box-shadow: var(--shadow);
}
.detail-dialog::backdrop {
background: rgba(9, 18, 28, 0.5);
}
.dialog-head {
display: flex;
gap: 16px;
align-items: flex-start;
justify-content: space-between;
padding: 20px 20px 0;
}
.dialog-head strong,
.dialog-head span {
display: block;
}
.dialog-head span {
color: var(--muted);
margin-top: 4px;
}
.dialog-actions {
display: flex;
flex-wrap: wrap;
gap: 10px;
padding: 18px 20px 0;
}
.sync-result {
background: #fff7fa;
border: 1px solid var(--line);
border-radius: 8px;
color: var(--text);
margin: 18px 20px 20px;
max-height: 260px;
overflow: auto;
padding: 12px;
white-space: pre-wrap;
}
.close-button {
position: absolute;
right: 12px;
top: 12px;
width: 34px;
height: 34px;
border: 1px solid var(--line);
border-radius: 50%;
background: var(--surface);
z-index: 2;
}
.detail-layout {
display: grid;
grid-template-columns: minmax(260px, 52%) minmax(260px, 1fr);
min-height: 520px;
}
.detail-media {
background: #101820;
display: grid;
place-items: center;
padding: 20px;
}
.detail-media img {
max-width: 100%;
max-height: 72vh;
object-fit: contain;
}
.detail-info {
padding: 28px;
overflow: auto;
}
.detail-info h2 {
margin: 0 0 8px;
font-size: 26px;
}
.detail-info p {
color: var(--muted);
}
.detail-tags {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin: 16px 0;
}
.empty {
border: 1px dashed var(--line);
border-radius: 14px;
padding: 34px;
background: var(--surface);
color: var(--muted);
text-align: center;
}
@media (max-width: 860px) {
body {
grid-template-columns: 1fr;
}
.sidebar {
position: static;
min-height: auto;
}
.quick-grid {
grid-template-columns: 1fr;
}
.welcome-card {
align-items: stretch;
flex-direction: column;
}
.toolbar,
.section-head,
.artist-row {
align-items: stretch;
flex-direction: column;
}
.search-wrap,
.follow-form,
.import-strip,
.x-media-form {
grid-template-columns: 1fr;
}
.toolbar-actions {
flex-wrap: wrap;
}
.detail-layout {
grid-template-columns: 1fr;
}
}