| 1 | """instagram 的命令参数与执行逻辑。""" |
| 2 | from __future__ import annotations |
| 3 | import json |
| 4 | from dataclasses import dataclass |
| 5 | from pathlib import Path |
| 6 | from uploader.meta_uploader.insgraph_uploader.main import ( |
| 7 | INSTAGRAM_PROXY, |
| 8 | InstagramUploader, |
| 9 | instagram_cookie_auth, |
| 10 | instagram_setup, |
| 11 | ) |
| 12 | from uploader.meta_uploader.insgraph_uploader.posts import instagram_post_action |
| 13 | from sau_commands.arguments import ( |
| 14 | SCHEDULE_FORMAT, |
| 15 | add_runtime_flags, |
| 16 | existing_file_path, |
| 17 | parse_image_files, |
| 18 | parse_tags, |
| 19 | resolve_account_file, |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | def register(platform_parsers): |
| 24 | """声明本平台命令、参数及合法选项。""" |
| 25 | schedule_help = SCHEDULE_FORMAT.replace("%", "%%") |
| 26 | instagram_parser = platform_parsers.add_parser("instagram", help="Instagram independent web operations") |
| 27 | instagram_actions = instagram_parser.add_subparsers(dest="action", required=True) |
| 28 | for action_name in ("login", "check"): |
| 29 | action_parser = instagram_actions.add_parser(action_name, help=f"Instagram {action_name}") |
| 30 | action_parser.add_argument("--account", required=True) |
| 31 | action_parser.add_argument("--proxy", default=INSTAGRAM_PROXY, help="代理地址;不传时直连") |
| 32 | if action_name == "login": |
| 33 | add_runtime_flags(action_parser) |
| 34 | action_parser.set_defaults(headless=False) |
| 35 | instagram_video = instagram_actions.add_parser("upload-video", help="向 Instagram 发布视频/Reel") |
| 36 | instagram_video.add_argument("--account", required=True) |
| 37 | instagram_video.add_argument("--file", required=True, type=existing_file_path) |
| 38 | instagram_video.add_argument("--thumbnail", type=existing_file_path, help="Reel 自定义封面(JPG/JPEG/PNG)") |
| 39 | instagram_video.add_argument("--title", required=True) |
| 40 | instagram_video.add_argument("--desc", default="") |
| 41 | instagram_video.add_argument("--tags", default="") |
| 42 | instagram_video.add_argument("--proxy", default=INSTAGRAM_PROXY, help="代理地址;不传时直连") |
| 43 | instagram_video.add_argument("--result", type=Path) |
| 44 | add_runtime_flags(instagram_video) |
| 45 | instagram_video.set_defaults(headless=False) |
| 46 | instagram_note = instagram_actions.add_parser("upload-note", help="向 Instagram 发布图片或轮播") |
| 47 | instagram_note.add_argument("--account", required=True) |
| 48 | instagram_note.add_argument("--images", required=True, nargs="+", type=existing_file_path) |
| 49 | instagram_note.add_argument("--title", required=True) |
| 50 | instagram_note.add_argument("--note", default="") |
| 51 | instagram_note.add_argument("--tags", default="") |
| 52 | instagram_note.add_argument("--proxy", default=INSTAGRAM_PROXY, help="代理地址;不传时直连") |
| 53 | instagram_note.add_argument("--result", type=Path) |
| 54 | add_runtime_flags(instagram_note) |
| 55 | instagram_note.set_defaults(headless=False) |
| 56 | for action_name in ("verify", "delete"): |
| 57 | action_parser = instagram_actions.add_parser(action_name, help="核对 Instagram 原作品" if action_name == "verify" else "核对后将 Instagram 原作品移至最近删除") |
| 58 | action_parser.add_argument("--account", required=True) |
| 59 | action_parser.add_argument("--post-url", required=True) |
| 60 | action_parser.add_argument("--expected-text", required=True) |
| 61 | action_parser.add_argument("--proxy", default=INSTAGRAM_PROXY, help="代理地址;不传时直连") |
| 62 | action_parser.add_argument("--result", type=Path) |
| 63 | add_runtime_flags(action_parser) |
| 64 | action_parser.set_defaults(headless=False) |
| 65 | |
| 66 | |
| 67 | @dataclass(slots=True) |
| 68 | class InstagramUploadRequest: |
| 69 | account_name: str |
| 70 | title: str |
| 71 | description: str |
| 72 | tags: list[str] |
| 73 | video_file: Path | None = None |
| 74 | image_files: list[Path] | None = None |
| 75 | thumbnail: Path | None = None |
| 76 | proxy: str | None = INSTAGRAM_PROXY |
| 77 | headless: bool = False |
| 78 | result_file: Path | None = None |
| 79 | |
| 80 | |
| 81 | async def login_instagram_account(account_name: str, *, headless: bool = False, proxy: str | None = INSTAGRAM_PROXY) -> dict: |
| 82 | account_file = resolve_account_file("instagram", account_name) |
| 83 | return await instagram_setup(account_file, handle=True, return_detail=True, headless=headless, proxy=proxy) |
| 84 | |
| 85 | |
| 86 | async def check_instagram_account(account_name: str, *, proxy: str | None = INSTAGRAM_PROXY) -> bool: |
| 87 | account_file = resolve_account_file("instagram", account_name) |
| 88 | return await instagram_cookie_auth(account_file, proxy=proxy) |
| 89 | |
| 90 | |
| 91 | async def upload_instagram_content(request: InstagramUploadRequest) -> dict: |
| 92 | account_file = resolve_account_file("instagram", request.account_name) |
| 93 | app = InstagramUploader( |
| 94 | account_file, |
| 95 | title=request.title, |
| 96 | description=request.description, |
| 97 | tags=request.tags, |
| 98 | video_file=request.video_file, |
| 99 | image_files=request.image_files, |
| 100 | thumbnail=request.thumbnail, |
| 101 | proxy=request.proxy, |
| 102 | headless=request.headless, |
| 103 | result_file=request.result_file, |
| 104 | ) |
| 105 | return await app.upload() |
| 106 | |
| 107 | |
| 108 | async def run(args): |
| 109 | """执行本平台命令;发布结果由平台实现核验。""" |
| 110 | if args.action == "login": |
| 111 | result = await login_instagram_account(args.account, headless=args.headless, proxy=args.proxy) |
| 112 | if not result["success"]: |
| 113 | raise RuntimeError(result.get("message", "Instagram login failed")) |
| 114 | print(f"Instagram login flow completed: {result['account_file']}") |
| 115 | return 0 |
| 116 | if args.action == "check": |
| 117 | is_valid = await check_instagram_account(args.account, proxy=args.proxy) |
| 118 | print("valid" if is_valid else "invalid") |
| 119 | return 0 if is_valid else 1 |
| 120 | if args.action in {"upload-video", "upload-note"}: |
| 121 | request = InstagramUploadRequest( |
| 122 | account_name=args.account, |
| 123 | title=args.title, |
| 124 | description=args.desc if args.action == "upload-video" else args.note, |
| 125 | tags=parse_tags(args.tags), |
| 126 | video_file=args.file if args.action == "upload-video" else None, |
| 127 | image_files=parse_image_files(args.images) if args.action == "upload-note" else None, |
| 128 | thumbnail=args.thumbnail if args.action == "upload-video" else None, |
| 129 | proxy=args.proxy, |
| 130 | headless=args.headless, |
| 131 | result_file=args.result, |
| 132 | ) |
| 133 | result = await upload_instagram_content(request) |
| 134 | print(json.dumps(result, ensure_ascii=False, indent=2)) |
| 135 | return 0 |
| 136 | if args.action in {"verify", "delete"}: |
| 137 | result = await instagram_post_action( |
| 138 | resolve_account_file("instagram", args.account), |
| 139 | post_url=args.post_url, |
| 140 | expected_text=args.expected_text, |
| 141 | delete=args.action == "delete", |
| 142 | proxy=args.proxy, |
| 143 | headless=args.headless, |
| 144 | result_file=args.result, |
| 145 | ) |
| 146 | print(json.dumps(result, ensure_ascii=False, indent=2)) |
| 147 | return 0 |
| 148 | |
| 149 | raise RuntimeError(f"Unsupported Instagram action: {args.action}") |
| 150 |