| 1 | """facebook 的命令参数与执行逻辑。""" |
| 2 | from __future__ import annotations |
| 3 | import json |
| 4 | from dataclasses import dataclass |
| 5 | from pathlib import Path |
| 6 | from uploader.base_video import BaseVideoUploader |
| 7 | from uploader.meta_uploader.facebook_uploader.main import ( |
| 8 | FACEBOOK_PROXY, |
| 9 | FacebookVideo, |
| 10 | FacebookPost, |
| 11 | facebook_cookie_auth, |
| 12 | facebook_setup, |
| 13 | ) |
| 14 | from uploader.meta_uploader.facebook_uploader.posts import facebook_post_action |
| 15 | from sau_commands.arguments import ( |
| 16 | SCHEDULE_FORMAT, |
| 17 | add_runtime_flags, |
| 18 | existing_file_path, |
| 19 | parse_tags, |
| 20 | resolve_account_file, |
| 21 | ) |
| 22 | |
| 23 | |
| 24 | def register(platform_parsers): |
| 25 | """声明本平台命令、参数及合法选项。""" |
| 26 | schedule_help = SCHEDULE_FORMAT.replace("%", "%%") |
| 27 | facebook_parser = platform_parsers.add_parser("facebook", help="Facebook personal post operations") |
| 28 | facebook_actions = facebook_parser.add_subparsers(dest="action", required=True) |
| 29 | |
| 30 | for action in ("verify", "delete"): |
| 31 | post_parser = facebook_actions.add_parser(action, help="核对原作品" if action == "verify" else "核对原作品后移至垃圾箱(30 天后平台删除)") |
| 32 | post_parser.add_argument("--account", required=True) |
| 33 | post_parser.add_argument("--post-url", required=True, help="原作品的 Facebook 链接") |
| 34 | post_parser.add_argument("--owner-id", required=True, help="原作品所属个人主页的数字 ID") |
| 35 | post_parser.add_argument("--expected-text", required=True, help="原作品中用于交叉核验的正文") |
| 36 | post_parser.add_argument("--result", type=Path, help="保存查询或删除结果 JSON") |
| 37 | post_parser.add_argument("--from-trash", action="store_true", help="原作品已在垃圾箱,从垃圾箱核验") |
| 38 | if action == "delete": |
| 39 | post_parser.add_argument("--permanent", action="store_true", help="将核实的原作品从垃圾箱永久删除(无法恢复)") |
| 40 | else: |
| 41 | post_parser.add_argument("--deleted", action="store_true", help="只查询垃圾箱原 ID 是否已删除,不执行删除") |
| 42 | post_parser.add_argument("--proxy", default=FACEBOOK_PROXY, help="代理地址;不传时直连") |
| 43 | add_runtime_flags(post_parser) |
| 44 | post_parser.set_defaults(headless=False) |
| 45 | |
| 46 | for action_name in ("login", "check"): |
| 47 | action_parser = facebook_actions.add_parser(action_name, help=f"Facebook {action_name}") |
| 48 | action_parser.add_argument("--account", required=True, help="Facebook user-defined account_name") |
| 49 | action_parser.add_argument( |
| 50 | "--proxy", |
| 51 | default=FACEBOOK_PROXY, |
| 52 | help="浏览器代理地址;不传时直连,传空字符串可覆盖环境变量并直连", |
| 53 | ) |
| 54 | if action_name == "login": |
| 55 | add_runtime_flags(action_parser) |
| 56 | # Facebook 登录必须在可见 Chrome 中完成,避免无头模式下用户看不到登录挑战。 |
| 57 | action_parser.set_defaults(headless=False) |
| 58 | |
| 59 | facebook_upload_video_parser = facebook_actions.add_parser( |
| 60 | "upload-video", help="Upload one video to a Facebook personal post" |
| 61 | ) |
| 62 | facebook_upload_video_parser.add_argument("--account", required=True, help="Facebook user-defined account_name") |
| 63 | facebook_upload_video_parser.add_argument("--file", required=True, type=existing_file_path, help="Video file path") |
| 64 | facebook_upload_video_parser.add_argument("--title", required=True, help="Post title, merged into Facebook caption") |
| 65 | facebook_upload_video_parser.add_argument("--desc", default="", help="Optional post description") |
| 66 | facebook_upload_video_parser.add_argument("--tags", default="", help="Comma-separated tags, such as tag1,tag2") |
| 67 | facebook_upload_video_parser.add_argument( |
| 68 | "--proxy", |
| 69 | default=FACEBOOK_PROXY, |
| 70 | help="浏览器代理地址;不传时直连,传空字符串可覆盖环境变量并直连", |
| 71 | ) |
| 72 | add_runtime_flags(facebook_upload_video_parser) |
| 73 | |
| 74 | facebook_note = facebook_actions.add_parser("upload-note", help="向个人主页发布图片及正文") |
| 75 | facebook_note.add_argument("--account", required=True) |
| 76 | facebook_note.add_argument("--images", required=True, nargs="+", type=existing_file_path) |
| 77 | facebook_note.add_argument("--title", required=True) |
| 78 | facebook_note.add_argument("--note", default="") |
| 79 | facebook_note.add_argument("--tags", default="") |
| 80 | facebook_note.add_argument("--proxy", default=FACEBOOK_PROXY, help="代理地址;不传时直连") |
| 81 | add_runtime_flags(facebook_note) |
| 82 | facebook_note.set_defaults(headless=False) |
| 83 | |
| 84 | |
| 85 | @dataclass(slots=True) |
| 86 | class FacebookVideoUploadRequest: |
| 87 | account_name: str |
| 88 | video_file: Path |
| 89 | title: str |
| 90 | description: str |
| 91 | tags: list[str] |
| 92 | proxy: str | None = FACEBOOK_PROXY |
| 93 | debug: bool = True |
| 94 | headless: bool = True |
| 95 | |
| 96 | |
| 97 | async def login_facebook_account( |
| 98 | account_name: str, *, headless: bool = False, proxy: str | None = FACEBOOK_PROXY |
| 99 | ) -> dict: |
| 100 | account_file = resolve_account_file("facebook", account_name) |
| 101 | return await facebook_setup( |
| 102 | account_file, |
| 103 | handle=True, |
| 104 | return_detail=True, |
| 105 | headless=headless, |
| 106 | proxy=proxy, |
| 107 | ) |
| 108 | |
| 109 | |
| 110 | async def check_facebook_account( |
| 111 | account_name: str, *, proxy: str | None = FACEBOOK_PROXY |
| 112 | ) -> bool: |
| 113 | account_file = resolve_account_file("facebook", account_name) |
| 114 | if not account_file.exists(): |
| 115 | return False |
| 116 | return await facebook_cookie_auth(account_file, proxy=proxy) |
| 117 | |
| 118 | |
| 119 | async def upload_facebook_video(request: FacebookVideoUploadRequest) -> Path: |
| 120 | account_file = resolve_account_file("facebook", request.account_name) |
| 121 | is_ready = await facebook_setup( |
| 122 | account_file, |
| 123 | handle=False, |
| 124 | proxy=request.proxy, |
| 125 | ) |
| 126 | if not is_ready: |
| 127 | raise RuntimeError( |
| 128 | f"Facebook 登录态不存在或已失效: {account_file}. " |
| 129 | f"请先执行 `sau facebook login --account {request.account_name}`。" |
| 130 | ) |
| 131 | |
| 132 | app = FacebookVideo( |
| 133 | title=request.title, |
| 134 | file_path=request.video_file, |
| 135 | description=request.description, |
| 136 | tags=request.tags, |
| 137 | account_file=account_file, |
| 138 | proxy=request.proxy, |
| 139 | headless=request.headless, |
| 140 | ) |
| 141 | await app.main() |
| 142 | return account_file |
| 143 | |
| 144 | |
| 145 | async def run(args): |
| 146 | """执行本平台命令;发布结果由平台实现核验。""" |
| 147 | if args.action == "upload-note": |
| 148 | for image in args.images: |
| 149 | BaseVideoUploader.validate_image_file(image) |
| 150 | app = FacebookPost(args.title, args.images, args.note, parse_tags(args.tags), |
| 151 | resolve_account_file("facebook", args.account), proxy=args.proxy, headless=args.headless) |
| 152 | await app.main() |
| 153 | print("Facebook image post submitted") |
| 154 | return 0 |
| 155 | if args.action in {"verify", "delete"}: |
| 156 | result = await facebook_post_action( |
| 157 | resolve_account_file("facebook", args.account), post_url=args.post_url, |
| 158 | owner_id=args.owner_id, expected_text=args.expected_text, |
| 159 | delete=args.action == "delete", proxy=args.proxy, headless=args.headless, |
| 160 | permanent=getattr(args, "permanent", False), from_trash=args.from_trash, |
| 161 | deleted=getattr(args, "deleted", False), |
| 162 | ) |
| 163 | if args.result: |
| 164 | args.result.parent.mkdir(parents=True, exist_ok=True) |
| 165 | args.result.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8") |
| 166 | print(json.dumps(result, ensure_ascii=False, indent=2)) |
| 167 | return 0 |
| 168 | |
| 169 | if args.action == "login": |
| 170 | result = await login_facebook_account( |
| 171 | args.account, headless=args.headless, proxy=args.proxy |
| 172 | ) |
| 173 | if not result["success"]: |
| 174 | raise RuntimeError(result["message"]) |
| 175 | print(f"Facebook login flow completed: {result['account_file']}") |
| 176 | return 0 |
| 177 | |
| 178 | if args.action == "check": |
| 179 | is_valid = await check_facebook_account(args.account, proxy=args.proxy) |
| 180 | print("valid" if is_valid else "invalid") |
| 181 | return 0 if is_valid else 1 |
| 182 | |
| 183 | if args.action == "upload-video": |
| 184 | request = FacebookVideoUploadRequest( |
| 185 | account_name=args.account, |
| 186 | video_file=args.file, |
| 187 | title=args.title, |
| 188 | description=args.desc, |
| 189 | tags=parse_tags(args.tags), |
| 190 | proxy=args.proxy, |
| 191 | debug=args.debug, |
| 192 | headless=args.headless, |
| 193 | ) |
| 194 | await upload_facebook_video(request) |
| 195 | print(f"Facebook video upload submitted: {request.video_file}") |
| 196 | return 0 |
| 197 | |
| 198 | raise RuntimeError(f"Unsupported Facebook action: {args.action}") |
| 199 |