小红书重构
1. 图文上传 2. 视频上传(封面,定时) 3. cli 4. skill 5. 无头模式
This commit is contained in:
+193
-2
@@ -26,6 +26,14 @@ from uploader.ks_uploader.main import (
|
||||
cookie_auth as kuaishou_cookie_auth,
|
||||
ks_setup,
|
||||
)
|
||||
from uploader.xiaohongshu_uploader.main import (
|
||||
XIAOHONGSHU_PUBLISH_STRATEGY_IMMEDIATE,
|
||||
XIAOHONGSHU_PUBLISH_STRATEGY_SCHEDULED,
|
||||
XiaoHongShuNote,
|
||||
XiaoHongShuVideo,
|
||||
cookie_auth as xiaohongshu_cookie_auth,
|
||||
xiaohongshu_setup,
|
||||
)
|
||||
|
||||
SCHEDULE_FORMAT = "%Y-%m-%d %H:%M"
|
||||
|
||||
@@ -35,6 +43,7 @@ class DouyinVideoUploadRequest:
|
||||
account_name: str
|
||||
video_file: Path
|
||||
title: str
|
||||
description: str
|
||||
tags: list[str]
|
||||
publish_date: datetime | int
|
||||
thumbnail_file: Path | None = None
|
||||
@@ -49,6 +58,7 @@ class DouyinVideoUploadRequest:
|
||||
class DouyinNoteUploadRequest:
|
||||
account_name: str
|
||||
image_files: list[Path]
|
||||
title: str
|
||||
note: str
|
||||
tags: list[str]
|
||||
publish_date: datetime | int
|
||||
@@ -62,6 +72,7 @@ class KuaishouVideoUploadRequest:
|
||||
account_name: str
|
||||
video_file: Path
|
||||
title: str
|
||||
description: str
|
||||
tags: list[str]
|
||||
publish_date: datetime | int
|
||||
thumbnail_file: Path | None = None
|
||||
@@ -74,6 +85,7 @@ class KuaishouVideoUploadRequest:
|
||||
class KuaishouNoteUploadRequest:
|
||||
account_name: str
|
||||
image_files: list[Path]
|
||||
title: str
|
||||
note: str
|
||||
tags: list[str]
|
||||
publish_date: datetime | int
|
||||
@@ -82,6 +94,33 @@ class KuaishouNoteUploadRequest:
|
||||
headless: bool = True
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class XiaohongshuVideoUploadRequest:
|
||||
account_name: str
|
||||
video_file: Path
|
||||
title: str
|
||||
description: str
|
||||
tags: list[str]
|
||||
publish_date: datetime | int
|
||||
thumbnail_file: Path | None = None
|
||||
publish_strategy: str = XIAOHONGSHU_PUBLISH_STRATEGY_IMMEDIATE
|
||||
debug: bool = True
|
||||
headless: bool = True
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class XiaohongshuNoteUploadRequest:
|
||||
account_name: str
|
||||
image_files: list[Path]
|
||||
title: str
|
||||
note: str
|
||||
tags: list[str]
|
||||
publish_date: datetime | int
|
||||
publish_strategy: str = XIAOHONGSHU_PUBLISH_STRATEGY_IMMEDIATE
|
||||
debug: bool = True
|
||||
headless: bool = True
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class BilibiliVideoUploadRequest:
|
||||
account_name: str
|
||||
@@ -153,6 +192,18 @@ async def check_kuaishou_account(account_name: str) -> bool:
|
||||
return await kuaishou_cookie_auth(str(account_file))
|
||||
|
||||
|
||||
async def login_xiaohongshu_account(account_name: str, headless: bool = True) -> dict:
|
||||
account_file = resolve_account_file("xiaohongshu", account_name)
|
||||
return await xiaohongshu_setup(str(account_file), handle=True, return_detail=True, headless=headless)
|
||||
|
||||
|
||||
async def check_xiaohongshu_account(account_name: str) -> bool:
|
||||
account_file = resolve_account_file("xiaohongshu", account_name)
|
||||
if not account_file.exists():
|
||||
return False
|
||||
return await xiaohongshu_cookie_auth(str(account_file))
|
||||
|
||||
|
||||
async def login_bilibili_account(account_name: str) -> dict:
|
||||
account_file = resolve_account_file("bilibili", account_name)
|
||||
if not has_interactive_terminal():
|
||||
@@ -197,6 +248,7 @@ async def upload_video(request: DouyinVideoUploadRequest) -> Path:
|
||||
request.tags,
|
||||
request.publish_date,
|
||||
str(account_file),
|
||||
desc=request.description,
|
||||
thumbnail_portrait_path=str(request.thumbnail_file) if request.thumbnail_file else None,
|
||||
productLink=request.product_link,
|
||||
productTitle=request.product_title,
|
||||
@@ -218,6 +270,7 @@ async def upload_note(request: DouyinNoteUploadRequest) -> Path:
|
||||
|
||||
app = DouYinNote(
|
||||
image_paths=[str(path) for path in request.image_files],
|
||||
title=request.title,
|
||||
note=request.note,
|
||||
tags=request.tags,
|
||||
publish_date=request.publish_date,
|
||||
@@ -241,6 +294,7 @@ async def upload_kuaishou_video(request: KuaishouVideoUploadRequest) -> Path:
|
||||
app = KSVideo(
|
||||
title=request.title,
|
||||
file_path=str(request.video_file),
|
||||
desc=request.description,
|
||||
tags=request.tags,
|
||||
publish_date=request.publish_date,
|
||||
account_file=str(account_file),
|
||||
@@ -263,6 +317,55 @@ async def upload_kuaishou_note(request: KuaishouNoteUploadRequest) -> Path:
|
||||
|
||||
app = KSNote(
|
||||
image_paths=[str(path) for path in request.image_files],
|
||||
title=request.title,
|
||||
note=request.note,
|
||||
tags=request.tags,
|
||||
publish_date=request.publish_date,
|
||||
account_file=str(account_file),
|
||||
publish_strategy=request.publish_strategy,
|
||||
debug=request.debug,
|
||||
headless=request.headless,
|
||||
)
|
||||
await app.main()
|
||||
return account_file
|
||||
|
||||
|
||||
async def upload_xiaohongshu_video(request: XiaohongshuVideoUploadRequest) -> Path:
|
||||
account_file = resolve_account_file("xiaohongshu", request.account_name)
|
||||
is_ready = await xiaohongshu_setup(str(account_file), handle=False)
|
||||
if not is_ready:
|
||||
raise RuntimeError(
|
||||
f"Xiaohongshu cookie is missing or expired: {account_file}. Run `sau xiaohongshu login --account {request.account_name}` first."
|
||||
)
|
||||
|
||||
app = XiaoHongShuVideo(
|
||||
title=request.title,
|
||||
file_path=str(request.video_file),
|
||||
desc=request.description,
|
||||
tags=request.tags,
|
||||
publish_date=request.publish_date,
|
||||
account_file=str(account_file),
|
||||
thumbnail_path=str(request.thumbnail_file) if request.thumbnail_file else None,
|
||||
publish_strategy=request.publish_strategy,
|
||||
debug=request.debug,
|
||||
headless=request.headless,
|
||||
)
|
||||
await app.main()
|
||||
return account_file
|
||||
|
||||
|
||||
async def upload_xiaohongshu_note(request: XiaohongshuNoteUploadRequest) -> Path:
|
||||
account_file = resolve_account_file("xiaohongshu", request.account_name)
|
||||
is_ready = await xiaohongshu_setup(str(account_file), handle=False)
|
||||
if not is_ready:
|
||||
raise RuntimeError(
|
||||
f"Xiaohongshu cookie is missing or expired: {account_file}. Run `sau xiaohongshu login --account {request.account_name}` first."
|
||||
)
|
||||
|
||||
app = XiaoHongShuNote(
|
||||
image_paths=[str(path) for path in request.image_files],
|
||||
title=request.title,
|
||||
desc=request.note,
|
||||
note=request.note,
|
||||
tags=request.tags,
|
||||
publish_date=request.publish_date,
|
||||
@@ -350,6 +453,7 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
upload_video_parser.add_argument("--account", required=True, help="Douyin user-defined account_name")
|
||||
upload_video_parser.add_argument("--file", required=True, type=existing_file_path, help="Video file path")
|
||||
upload_video_parser.add_argument("--title", required=True, help="Video title")
|
||||
upload_video_parser.add_argument("--desc", default="", help="Optional video description")
|
||||
upload_video_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2")
|
||||
upload_video_parser.add_argument("--schedule", type=schedule_value, help=f"Schedule time in {schedule_help}")
|
||||
upload_video_parser.add_argument("--thumbnail", type=existing_file_path, help="Optional thumbnail path")
|
||||
@@ -360,7 +464,8 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
upload_note_parser = douyin_actions.add_parser("upload-note", help="Upload one note to Douyin")
|
||||
upload_note_parser.add_argument("--account", required=True, help="Douyin user-defined account_name")
|
||||
upload_note_parser.add_argument("--images", required=True, nargs="+", type=existing_file_path, help="Image file paths")
|
||||
upload_note_parser.add_argument("--note", required=True, help="Note content")
|
||||
upload_note_parser.add_argument("--title", required=True, help="Note title")
|
||||
upload_note_parser.add_argument("--note", default="", help="Optional note content")
|
||||
upload_note_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2")
|
||||
upload_note_parser.add_argument("--schedule", type=schedule_value, help=f"Schedule time in {schedule_help}")
|
||||
add_runtime_flags(upload_note_parser)
|
||||
@@ -378,6 +483,7 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
kuaishou_upload_video_parser.add_argument("--account", required=True, help="Kuaishou user-defined account_name")
|
||||
kuaishou_upload_video_parser.add_argument("--file", required=True, type=existing_file_path, help="Video file path")
|
||||
kuaishou_upload_video_parser.add_argument("--title", required=True, help="Video title")
|
||||
kuaishou_upload_video_parser.add_argument("--desc", default="", help="Optional video description")
|
||||
kuaishou_upload_video_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2")
|
||||
kuaishou_upload_video_parser.add_argument("--schedule", type=schedule_value, help=f"Schedule time in {schedule_help}")
|
||||
kuaishou_upload_video_parser.add_argument("--thumbnail", type=existing_file_path, help="Optional thumbnail path")
|
||||
@@ -386,11 +492,40 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
kuaishou_upload_note_parser = kuaishou_actions.add_parser("upload-note", help="Upload one note to Kuaishou")
|
||||
kuaishou_upload_note_parser.add_argument("--account", required=True, help="Kuaishou user-defined account_name")
|
||||
kuaishou_upload_note_parser.add_argument("--images", required=True, nargs="+", type=existing_file_path, help="Image file paths")
|
||||
kuaishou_upload_note_parser.add_argument("--note", required=True, help="Note content")
|
||||
kuaishou_upload_note_parser.add_argument("--title", required=True, help="Note title")
|
||||
kuaishou_upload_note_parser.add_argument("--note", default="", help="Optional note content")
|
||||
kuaishou_upload_note_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2")
|
||||
kuaishou_upload_note_parser.add_argument("--schedule", type=schedule_value, help=f"Schedule time in {schedule_help}")
|
||||
add_runtime_flags(kuaishou_upload_note_parser)
|
||||
|
||||
xiaohongshu_parser = platform_parsers.add_parser("xiaohongshu", help="Xiaohongshu operations")
|
||||
xiaohongshu_actions = xiaohongshu_parser.add_subparsers(dest="action", required=True)
|
||||
|
||||
for action_name in ("login", "check"):
|
||||
action_parser = xiaohongshu_actions.add_parser(action_name, help=f"Xiaohongshu {action_name}")
|
||||
action_parser.add_argument("--account", required=True, help="Xiaohongshu user-defined account_name")
|
||||
if action_name == "login":
|
||||
add_runtime_flags(action_parser)
|
||||
|
||||
xiaohongshu_upload_video_parser = xiaohongshu_actions.add_parser("upload-video", help="Upload one video to Xiaohongshu")
|
||||
xiaohongshu_upload_video_parser.add_argument("--account", required=True, help="Xiaohongshu user-defined account_name")
|
||||
xiaohongshu_upload_video_parser.add_argument("--file", required=True, type=existing_file_path, help="Video file path")
|
||||
xiaohongshu_upload_video_parser.add_argument("--title", required=True, help="Video title")
|
||||
xiaohongshu_upload_video_parser.add_argument("--desc", default="", help="Optional video description")
|
||||
xiaohongshu_upload_video_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2")
|
||||
xiaohongshu_upload_video_parser.add_argument("--schedule", type=schedule_value, help=f"Schedule time in {schedule_help}")
|
||||
xiaohongshu_upload_video_parser.add_argument("--thumbnail", type=existing_file_path, help="Optional thumbnail path")
|
||||
add_runtime_flags(xiaohongshu_upload_video_parser)
|
||||
|
||||
xiaohongshu_upload_note_parser = xiaohongshu_actions.add_parser("upload-note", help="Upload one note to Xiaohongshu")
|
||||
xiaohongshu_upload_note_parser.add_argument("--account", required=True, help="Xiaohongshu user-defined account_name")
|
||||
xiaohongshu_upload_note_parser.add_argument("--images", required=True, nargs="+", type=existing_file_path, help="Image file paths")
|
||||
xiaohongshu_upload_note_parser.add_argument("--title", required=True, help="Note title")
|
||||
xiaohongshu_upload_note_parser.add_argument("--note", default="", help="Optional note content")
|
||||
xiaohongshu_upload_note_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2")
|
||||
xiaohongshu_upload_note_parser.add_argument("--schedule", type=schedule_value, help=f"Schedule time in {schedule_help}")
|
||||
add_runtime_flags(xiaohongshu_upload_note_parser)
|
||||
|
||||
bilibili_parser = platform_parsers.add_parser("bilibili", help="Bilibili operations")
|
||||
bilibili_actions = bilibili_parser.add_subparsers(dest="action", required=True)
|
||||
|
||||
@@ -430,6 +565,7 @@ async def dispatch(args: argparse.Namespace) -> int:
|
||||
account_name=args.account,
|
||||
video_file=args.file,
|
||||
title=args.title,
|
||||
description=args.desc,
|
||||
tags=parse_tags(args.tags),
|
||||
publish_date=args.schedule or 0,
|
||||
thumbnail_file=args.thumbnail,
|
||||
@@ -447,6 +583,7 @@ async def dispatch(args: argparse.Namespace) -> int:
|
||||
request = DouyinNoteUploadRequest(
|
||||
account_name=args.account,
|
||||
image_files=parse_image_files(args.images),
|
||||
title=args.title,
|
||||
note=args.note,
|
||||
tags=parse_tags(args.tags),
|
||||
publish_date=args.schedule or 0,
|
||||
@@ -480,6 +617,7 @@ async def dispatch(args: argparse.Namespace) -> int:
|
||||
account_name=args.account,
|
||||
video_file=args.file,
|
||||
title=args.title,
|
||||
description=args.desc,
|
||||
tags=parse_tags(args.tags),
|
||||
publish_date=args.schedule or 0,
|
||||
thumbnail_file=args.thumbnail,
|
||||
@@ -495,6 +633,7 @@ async def dispatch(args: argparse.Namespace) -> int:
|
||||
request = KuaishouNoteUploadRequest(
|
||||
account_name=args.account,
|
||||
image_files=parse_image_files(args.images),
|
||||
title=args.title,
|
||||
note=args.note,
|
||||
tags=parse_tags(args.tags),
|
||||
publish_date=args.schedule or 0,
|
||||
@@ -508,6 +647,58 @@ async def dispatch(args: argparse.Namespace) -> int:
|
||||
|
||||
raise RuntimeError(f"Unsupported Kuaishou action: {args.action}")
|
||||
|
||||
if args.platform == "xiaohongshu":
|
||||
if args.action == "login":
|
||||
result = await login_xiaohongshu_account(args.account, headless=args.headless)
|
||||
if not result["success"]:
|
||||
raise RuntimeError(result["message"])
|
||||
print(f"Xiaohongshu login flow completed: {result['account_file']}")
|
||||
return 0
|
||||
|
||||
if args.action == "check":
|
||||
is_valid = await check_xiaohongshu_account(args.account)
|
||||
print("valid" if is_valid else "invalid")
|
||||
return 0 if is_valid else 1
|
||||
|
||||
publish_strategy = (
|
||||
XIAOHONGSHU_PUBLISH_STRATEGY_SCHEDULED if args.schedule else XIAOHONGSHU_PUBLISH_STRATEGY_IMMEDIATE
|
||||
)
|
||||
|
||||
if args.action == "upload-video":
|
||||
request = XiaohongshuVideoUploadRequest(
|
||||
account_name=args.account,
|
||||
video_file=args.file,
|
||||
title=args.title,
|
||||
description=args.desc,
|
||||
tags=parse_tags(args.tags),
|
||||
publish_date=args.schedule or 0,
|
||||
thumbnail_file=args.thumbnail,
|
||||
publish_strategy=publish_strategy,
|
||||
debug=args.debug,
|
||||
headless=args.headless,
|
||||
)
|
||||
await upload_xiaohongshu_video(request)
|
||||
print(f"Xiaohongshu video upload submitted: {request.video_file}")
|
||||
return 0
|
||||
|
||||
if args.action == "upload-note":
|
||||
request = XiaohongshuNoteUploadRequest(
|
||||
account_name=args.account,
|
||||
image_files=parse_image_files(args.images),
|
||||
title=args.title,
|
||||
note=args.note,
|
||||
tags=parse_tags(args.tags),
|
||||
publish_date=args.schedule or 0,
|
||||
publish_strategy=publish_strategy,
|
||||
debug=args.debug,
|
||||
headless=args.headless,
|
||||
)
|
||||
await upload_xiaohongshu_note(request)
|
||||
print(f"Xiaohongshu note upload submitted: {len(request.image_files)} images")
|
||||
return 0
|
||||
|
||||
raise RuntimeError(f"Unsupported Xiaohongshu action: {args.action}")
|
||||
|
||||
if args.platform == "bilibili":
|
||||
if args.action == "login":
|
||||
result = await login_bilibili_account(args.account)
|
||||
|
||||
Reference in New Issue
Block a user