commit
9c5fef1a4d
412 files changed
+94582
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Local service entry points for Sakura downloader."""
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -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()
|
||||
@@ -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())
|
||||
@@ -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
|
||||
Reference in new issue
Block a user