forked from dreammis/social-auto-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsau_cli.py
More file actions
835 lines (698 loc) · 33.6 KB
/
sau_cli.py
File metadata and controls
835 lines (698 loc) · 33.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
from __future__ import annotations
import argparse
import asyncio
import sys
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Iterable, Sequence
from conf import BASE_DIR
from uploader.bilibili_uploader.runtime import run_biliup_command
from uploader.douyin_uploader.main import (
DOUYIN_PUBLISH_STRATEGY_IMMEDIATE,
DOUYIN_PUBLISH_STRATEGY_SCHEDULED,
DouYinNote,
DouYinVideo,
cookie_auth as douyin_cookie_auth,
douyin_setup,
)
from uploader.ks_uploader.main import (
KUAISHOU_PUBLISH_STRATEGY_IMMEDIATE,
KUAISHOU_PUBLISH_STRATEGY_SCHEDULED,
KSNote,
KSVideo,
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,
cookie_auth as xxhs_cookie_auth,
xiaohongshu_setup,
)
SCHEDULE_FORMAT = "%Y-%m-%d %H:%M"
@dataclass(slots=True)
class DouyinVideoUploadRequest:
account_name: str
video_file: Path
title: str
description: str
tags: list[str]
publish_date: datetime | int
thumbnail_file: Path | None = None
product_link: str = ""
product_title: str = ""
publish_strategy: str = DOUYIN_PUBLISH_STRATEGY_IMMEDIATE
debug: bool = True
headless: bool = True
@dataclass(slots=True)
class DouyinNoteUploadRequest:
account_name: str
image_files: list[Path]
title: str
note: str
tags: list[str]
publish_date: datetime | int
publish_strategy: str = DOUYIN_PUBLISH_STRATEGY_IMMEDIATE
debug: bool = True
headless: bool = True
@dataclass(slots=True)
class KuaishouVideoUploadRequest:
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 = KUAISHOU_PUBLISH_STRATEGY_IMMEDIATE
debug: bool = True
headless: bool = True
@dataclass(slots=True)
class KuaishouNoteUploadRequest:
account_name: str
image_files: list[Path]
title: str
note: str
tags: list[str]
publish_date: datetime | int
publish_strategy: str = KUAISHOU_PUBLISH_STRATEGY_IMMEDIATE
debug: bool = True
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
video_file: Path
title: str
description: str
tid: int
tags: list[str]
publish_date: datetime | int
@dataclass(slots=True)
class XhsVideoUploadRequest:
account_name: str
video_file: Path
title: str
tags: list[str]
publish_date: datetime | int
debug: bool = True
headless: bool = True
def has_interactive_terminal() -> bool:
return sys.stdin.isatty() and sys.stdout.isatty()
def resolve_runtime_home() -> Path:
return Path(BASE_DIR)
def resolve_account_file(platform: str, account_name: str) -> Path:
account_file = resolve_runtime_home() / "cookies" / f"{platform}_{account_name}.json"
account_file.parent.mkdir(exist_ok=True)
return account_file
def parse_tags(raw_tags: str | None) -> list[str]:
if not raw_tags:
return []
tags: list[str] = []
for item in raw_tags.split(","):
cleaned = item.strip().lstrip("#")
if cleaned:
tags.append(cleaned)
return tags
def parse_image_files(raw_files: Iterable[Path]) -> list[Path]:
return [Path(file) for file in raw_files]
def parse_schedule(raw_schedule: str | None) -> datetime | int:
if not raw_schedule:
return 0
return datetime.strptime(raw_schedule, SCHEDULE_FORMAT)
async def login_douyin_account(account_name: str, headless: bool = True) -> dict:
account_file = resolve_account_file("douyin", account_name)
return await douyin_setup(str(account_file), handle=True, return_detail=True, headless=headless)
async def check_douyin_account(account_name: str) -> bool:
account_file = resolve_account_file("douyin", account_name)
if not account_file.exists():
return False
return await douyin_cookie_auth(str(account_file))
async def login_kuaishou_account(account_name: str, headless: bool = True) -> dict:
account_file = resolve_account_file("kuaishou", account_name)
return await ks_setup(str(account_file), handle=True, return_detail=True, headless=headless)
async def check_kuaishou_account(account_name: str) -> bool:
account_file = resolve_account_file("kuaishou", account_name)
if not account_file.exists():
return False
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():
return {
"success": False,
"message": (
"Bilibili login requires a local interactive terminal. "
f"Please run `sau bilibili login --account {account_name}` yourself in a local terminal. "
"If the terminal QR code does not render completely, open `./qrcode.png` and scan that image."
),
"account_file": str(account_file),
}
result = run_biliup_command(["-u", str(account_file), "login"], interactive=True)
success = result.returncode == 0
return {
"success": success,
"message": (result.stderr or result.stdout or "").strip() or "Bilibili login completed" if success else (result.stderr or result.stdout or "").strip() or "Bilibili login failed",
"account_file": str(account_file),
}
async def check_bilibili_account(account_name: str) -> bool:
account_file = resolve_account_file("bilibili", account_name)
if not account_file.exists():
return False
result = run_biliup_command(["-u", str(account_file), "renew"])
return result.returncode == 0
async def login_xhs_account(account_name: str, headless: bool = True) -> dict:
account_file = resolve_account_file("xhs", account_name)
return await xiaohongshu_setup(str(account_file), handle=True, return_detail=True, headless=headless)
async def check_xhs_account(account_name: str) -> bool:
account_file = resolve_account_file("xhs", account_name)
if not account_file.exists():
return False
return await xxhs_cookie_auth(str(account_file))
async def upload_video(request: DouyinVideoUploadRequest) -> Path:
account_file = resolve_account_file("douyin", request.account_name)
is_ready = await douyin_setup(str(account_file), handle=False)
if not is_ready:
raise RuntimeError(
f"Douyin cookie is missing or expired: {account_file}. Run `sau douyin login --account {request.account_name}` first."
)
app = DouYinVideo(
request.title,
str(request.video_file),
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,
publish_strategy=request.publish_strategy,
debug=request.debug,
headless=request.headless,
)
await app.douyin_upload_video()
return account_file
async def upload_note(request: DouyinNoteUploadRequest) -> Path:
account_file = resolve_account_file("douyin", request.account_name)
is_ready = await douyin_setup(str(account_file), handle=False)
if not is_ready:
raise RuntimeError(
f"Douyin cookie is missing or expired: {account_file}. Run `sau douyin login --account {request.account_name}` first."
)
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,
account_file=str(account_file),
publish_strategy=request.publish_strategy,
debug=request.debug,
headless=request.headless,
)
await app.douyin_upload_note()
return account_file
async def upload_kuaishou_video(request: KuaishouVideoUploadRequest) -> Path:
account_file = resolve_account_file("kuaishou", request.account_name)
is_ready = await ks_setup(str(account_file), handle=False)
if not is_ready:
raise RuntimeError(
f"Kuaishou cookie is missing or expired: {account_file}. Run `sau kuaishou login --account {request.account_name}` first."
)
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),
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_kuaishou_note(request: KuaishouNoteUploadRequest) -> Path:
account_file = resolve_account_file("kuaishou", request.account_name)
is_ready = await ks_setup(str(account_file), handle=False)
if not is_ready:
raise RuntimeError(
f"Kuaishou cookie is missing or expired: {account_file}. Run `sau kuaishou login --account {request.account_name}` first."
)
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,
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_bilibili_video(request: BilibiliVideoUploadRequest) -> Path:
account_file = resolve_account_file("bilibili", request.account_name)
if not account_file.exists():
raise RuntimeError(
f"Bilibili account file is missing: {account_file}. Run `sau bilibili login --account {request.account_name}` first."
)
arguments = [
"-u",
str(account_file),
"upload",
str(request.video_file),
"--title",
request.title,
"--desc",
request.description,
"--tid",
str(request.tid),
]
if request.tags:
arguments.extend(["--tag", ",".join(request.tags)])
if isinstance(request.publish_date, datetime):
arguments.extend(["--dtime", str(int(request.publish_date.timestamp()))])
result = run_biliup_command(arguments)
if result.returncode != 0:
raise RuntimeError((result.stderr or result.stdout or "").strip() or "Bilibili upload failed")
return account_file
async def upload_xhs_video(request: XhsVideoUploadRequest) -> Path:
account_file = resolve_account_file("xhs", request.account_name)
is_ready = await xiaohongshu_setup(str(account_file), handle=False)
if not is_ready:
raise RuntimeError(
f"XHS cookie is missing or expired: {account_file}. Run `sau xhs login --account {request.account_name}` first."
)
app = XiaoHongShuVideo(
title=request.title,
file_path=str(request.video_file),
tags=request.tags,
publish_date=request.publish_date,
account_file=str(account_file),
)
await app.main()
return account_file
def existing_file_path(value: str) -> Path:
path = Path(value)
if not path.is_file():
raise argparse.ArgumentTypeError(f"File not found: {value}")
return path
def schedule_value(value: str):
try:
return parse_schedule(value)
except ValueError as exc:
raise argparse.ArgumentTypeError(
f"Invalid schedule '{value}'. Expected format: {SCHEDULE_FORMAT}"
) from exc
def add_runtime_flags(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
headless_group = parser.add_mutually_exclusive_group()
headless_group.add_argument("--headed", dest="headless", action="store_false", help="Run with browser UI")
headless_group.add_argument("--headless", dest="headless", action="store_true", help="Run in headless mode")
parser.set_defaults(headless=True)
def build_parser() -> argparse.ArgumentParser:
schedule_help = SCHEDULE_FORMAT.replace("%", "%%")
parser = argparse.ArgumentParser(
prog="sau",
description="CLI for social-auto-upload.",
)
platform_parsers = parser.add_subparsers(dest="platform", required=True)
douyin_parser = platform_parsers.add_parser("douyin", help="Douyin operations")
douyin_actions = douyin_parser.add_subparsers(dest="action", required=True)
for action_name in ("login", "check"):
action_parser = douyin_actions.add_parser(action_name, help=f"Douyin {action_name}")
action_parser.add_argument("--account", required=True, help="Douyin user-defined account_name")
if action_name == "login":
add_runtime_flags(action_parser)
upload_video_parser = douyin_actions.add_parser("upload-video", help="Upload one video to Douyin")
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")
upload_video_parser.add_argument("--product-link", default="", help="Optional product link")
upload_video_parser.add_argument("--product-title", default="", help="Optional product title")
add_runtime_flags(upload_video_parser)
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("--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)
kuaishou_parser = platform_parsers.add_parser("kuaishou", help="Kuaishou operations")
kuaishou_actions = kuaishou_parser.add_subparsers(dest="action", required=True)
for action_name in ("login", "check"):
action_parser = kuaishou_actions.add_parser(action_name, help=f"Kuaishou {action_name}")
action_parser.add_argument("--account", required=True, help="Kuaishou user-defined account_name")
if action_name == "login":
add_runtime_flags(action_parser)
kuaishou_upload_video_parser = kuaishou_actions.add_parser("upload-video", help="Upload one video to Kuaishou")
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")
add_runtime_flags(kuaishou_upload_video_parser)
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("--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)
for action_name in ("login", "check"):
action_parser = bilibili_actions.add_parser(action_name, help=f"Bilibili {action_name}")
action_parser.add_argument("--account", required=True, help="Bilibili user-defined account_name")
bilibili_upload_video_parser = bilibili_actions.add_parser("upload-video", help="Upload one video to Bilibili")
bilibili_upload_video_parser.add_argument("--account", required=True, help="Bilibili user-defined account_name")
bilibili_upload_video_parser.add_argument("--file", required=True, type=existing_file_path, help="Video file path")
bilibili_upload_video_parser.add_argument("--title", required=True, help="Video title")
bilibili_upload_video_parser.add_argument("--desc", required=True, help="Video description")
bilibili_upload_video_parser.add_argument("--tid", required=True, type=int, help="Bilibili category id")
bilibili_upload_video_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2")
bilibili_upload_video_parser.add_argument("--schedule", type=schedule_value, help=f"Schedule time in {schedule_help}")
xhs_parser = platform_parsers.add_parser("xhs", help="XHS operations")
xhs_actions = xhs_parser.add_subparsers(dest="action", required=True)
for action_name in ("login", "check"):
action_parser = xhs_actions.add_parser(action_name, help=f"XHS {action_name}")
action_parser.add_argument("--account", required=True, help="XHS user-defined account_name")
if action_name == "login":
add_runtime_flags(action_parser)
xhs_upload_video_parser = xhs_actions.add_parser("upload-video", help="Upload one video to XHS")
xhs_upload_video_parser.add_argument("--account", required=True, help="XHS user-defined account_name")
xhs_upload_video_parser.add_argument("--file", required=True, type=existing_file_path, help="Video file path")
xhs_upload_video_parser.add_argument("--title", required=True, help="Video title")
xhs_upload_video_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2")
xhs_upload_video_parser.add_argument("--schedule", type=schedule_value, help=f"Schedule time in {schedule_help}")
add_runtime_flags(xhs_upload_video_parser)
return parser
async def dispatch(args: argparse.Namespace) -> int:
if args.platform == "douyin":
if args.action == "login":
result = await login_douyin_account(args.account, headless=args.headless)
if not result["success"]:
raise RuntimeError(result["message"])
print(f"Douyin login flow completed: {result['account_file']}")
return 0
if args.action == "check":
is_valid = await check_douyin_account(args.account)
print("valid" if is_valid else "invalid")
return 0 if is_valid else 1
publish_strategy = DOUYIN_PUBLISH_STRATEGY_SCHEDULED if args.schedule else DOUYIN_PUBLISH_STRATEGY_IMMEDIATE
if args.action == "upload-video":
request = DouyinVideoUploadRequest(
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,
product_link=args.product_link,
product_title=args.product_title,
publish_strategy=publish_strategy,
debug=args.debug,
headless=args.headless,
)
await upload_video(request)
print(f"Douyin video upload submitted: {request.video_file}")
return 0
if args.action == "upload-note":
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,
publish_strategy=publish_strategy,
debug=args.debug,
headless=args.headless,
)
await upload_note(request)
print(f"Douyin note upload submitted: {len(request.image_files)} images")
return 0
raise RuntimeError(f"Unsupported Douyin action: {args.action}")
if args.platform == "kuaishou":
if args.action == "login":
result = await login_kuaishou_account(args.account, headless=args.headless)
if not result["success"]:
raise RuntimeError(result["message"])
print(f"Kuaishou login flow completed: {result['account_file']}")
return 0
if args.action == "check":
is_valid = await check_kuaishou_account(args.account)
print("valid" if is_valid else "invalid")
return 0 if is_valid else 1
publish_strategy = KUAISHOU_PUBLISH_STRATEGY_SCHEDULED if args.schedule else KUAISHOU_PUBLISH_STRATEGY_IMMEDIATE
if args.action == "upload-video":
request = KuaishouVideoUploadRequest(
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_kuaishou_video(request)
print(f"Kuaishou video upload submitted: {request.video_file}")
return 0
if args.action == "upload-note":
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,
publish_strategy=publish_strategy,
debug=args.debug,
headless=args.headless,
)
await upload_kuaishou_note(request)
print(f"Kuaishou note upload submitted: {len(request.image_files)} images")
return 0
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)
if not result["success"]:
raise RuntimeError(result["message"])
print(f"Bilibili login flow completed: {result['account_file']}")
return 0
if args.action == "check":
is_valid = await check_bilibili_account(args.account)
print("valid" if is_valid else "invalid")
return 0 if is_valid else 1
if args.action == "upload-video":
request = BilibiliVideoUploadRequest(
account_name=args.account,
video_file=args.file,
title=args.title,
description=args.desc,
tid=args.tid,
tags=parse_tags(args.tags),
publish_date=args.schedule or 0,
)
await upload_bilibili_video(request)
print(f"Bilibili video upload submitted: {request.video_file}")
return 0
raise RuntimeError(f"Unsupported Bilibili action: {args.action}")
if args.platform == "xhs":
if args.action == "login":
result = await login_xhs_account(args.account, headless=args.headless)
if not result["success"]:
raise RuntimeError(result["message"])
print(f"XHS login flow completed: {result['account_file']}")
return 0
if args.action == "check":
is_valid = await check_xhs_account(args.account)
print("valid" if is_valid else "invalid")
return 0 if is_valid else 1
if args.action == "upload-video":
request = XhsVideoUploadRequest(
account_name=args.account,
video_file=args.file,
title=args.title,
tags=parse_tags(args.tags),
publish_date=args.schedule or 0,
debug=args.debug,
headless=args.headless,
)
await upload_xhs_video(request)
print(f"XHS video upload submitted: {request.video_file}")
return 0
raise RuntimeError(f"Unsupported XHS action: {args.action}")
raise RuntimeError(f"Unsupported platform: {args.platform}")
def main(argv: Sequence[str] | None = None) -> int:
parser = build_parser()
args = parser.parse_args(list(argv) if argv is not None else None)
try:
return asyncio.run(dispatch(args))
except Exception as exc:
print(str(exc), file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())