| 1 | """大鱼号视频与文章共用的预览、确认发表及提交结果记录。""" |
| 2 | import json |
| 3 | from pathlib import Path |
| 4 | from urllib.parse import parse_qs, parse_qsl, urlencode, urlsplit |
| 5 | |
| 6 | from patchright.async_api import Page, expect |
| 7 | from uploader.dayu_uploader.verification import check_dayu_article_content, wait_dayu_publication |
| 8 | from uploader.dayu_uploader.schedule import read_dayu_schedule |
| 9 | |
| 10 | |
| 11 | async def submit_dayu_publication(page: Page, result: dict, result_file: Path, |
| 12 | article_html: str | None = None) -> None: |
| 13 | """提交动作各执行一次;每次远端写入前后保存阶段与作品标识,供只读查询。""" |
| 14 | result["status"] = "preview_unknown" |
| 15 | result_file.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8") |
| 16 | # 主表单“发表”仅生成预览;用精确按钮名区分后续的“确认发表”。 |
| 17 | save_url = "https://mp.dayu.com/dashboard/save-draft" |
| 18 | async def preserve_article_html(route): |
| 19 | # 仅替换本次文章正文,保留表单实填的标题、来源、封面及会话字段;不改变平台过滤规则。 |
| 20 | fields = parse_qsl(route.request.post_data or "", keep_blank_values=True) |
| 21 | if route.request.method != "POST" or sum(key == "content" for key, _ in fields) != 1: |
| 22 | await route.abort() |
| 23 | raise RuntimeError("文章预览请求的正文结构变化,拒绝提交") |
| 24 | if result.get("origin_id") and dict(fields).get("draft_id") != result["origin_id"]: |
| 25 | await route.abort() |
| 26 | raise RuntimeError("预览请求与待恢复的草稿 ID 不一致") |
| 27 | await route.fallback(post_data=urlencode([ |
| 28 | (key, article_html if key == "content" else value) for key, value in fields |
| 29 | ])) |
| 30 | if article_html is not None: |
| 31 | await page.route(save_url, preserve_article_html) |
| 32 | try: |
| 33 | async with page.expect_response( |
| 34 | lambda r: urlsplit(r.url).path == "/dashboard/save-draft" and r.request.method == "POST", |
| 35 | timeout=45000, |
| 36 | ) as draft_event: |
| 37 | await page.get_by_role("button", name="发表", exact=True).click() |
| 38 | finally: |
| 39 | if article_html is not None: |
| 40 | await page.unroute(save_url, preserve_article_html) |
| 41 | draft_response = await draft_event.value |
| 42 | if not draft_response.ok: |
| 43 | raise RuntimeError(f"发表预览保存失败: HTTP {draft_response.status}") |
| 44 | draft = (await draft_response.json()).get("data", {}) |
| 45 | if not draft.get("_id"): |
| 46 | raise RuntimeError("发表预览未返回草稿 ID,拒绝确认发表") |
| 47 | if result.get("origin_id") and draft["_id"] != result["origin_id"]: |
| 48 | raise RuntimeError("预览响应返回了其他草稿 ID,拒绝确认发表") |
| 49 | result.update(origin_id=draft["_id"], status="preview_ready") |
| 50 | result_file.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8") |
| 51 | # 在不可逆的确认发表前读服务端设置,防止 UI 显示正确但原稿时间未保存。 |
| 52 | await read_dayu_schedule(page.context, result["origin_id"], result.get("schedule")) |
| 53 | if article_html is not None: |
| 54 | preview_url = draft.get("previewUrl", "") |
| 55 | parsed = urlsplit(preview_url) |
| 56 | if (parsed.scheme != "https" or parsed.hostname != "mparticle.uc.cn" |
| 57 | or parsed.path != "/preview.html" or parse_qs(parsed.query).get("wm_aid") != [draft["_id"]]): |
| 58 | raise RuntimeError("文章预览地址与本次草稿不符,拒绝确认发表") |
| 59 | preview = await page.context.new_page() |
| 60 | try: |
| 61 | await preview.goto(preview_url, wait_until="domcontentloaded", timeout=45000) |
| 62 | # 服务端预览的 article-content 是正文区域,排除页头标题、作者及页面其他图片。 |
| 63 | body = preview.locator(".article-content") |
| 64 | await expect(body).to_have_count(1, timeout=30000) |
| 65 | await check_dayu_article_content(body, result["manifest"]) |
| 66 | # 预览图使用懒加载;逐张滚动并核对解码,不能把尚未加载的截图作为效果证据。 |
| 67 | images = body.locator("img") |
| 68 | for index in range(await images.count()): |
| 69 | image = images.nth(index) |
| 70 | await image.scroll_into_view_if_needed() |
| 71 | await expect(image).to_have_js_property("complete", True, timeout=30000) |
| 72 | if await image.evaluate("image => image.naturalWidth") <= 0: |
| 73 | raise RuntimeError(f"文章预览第 {index + 1} 张图片无法加载") |
| 74 | result["style_check"]["preview_checked"] = True |
| 75 | result_file.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8") |
| 76 | finally: |
| 77 | await preview.close() |
| 78 | # 预览页的最终提交按钮有独立名称,不能复用主表单的“发表”定位。 |
| 79 | confirm = page.get_by_role("button", name="确认发表", exact=True) |
| 80 | await expect(confirm).to_be_visible(timeout=30000) |
| 81 | # 响应超时不能再点确认。保留 origin_id 后可查询本次记录,无需重新上传。 |
| 82 | result["status"] = "submission_unknown" |
| 83 | result_file.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8") |
| 84 | async with page.expect_response( |
| 85 | lambda r: urlsplit(r.url).path == "/dashboard/submit-article" and r.request.method == "POST", |
| 86 | timeout=45000, |
| 87 | ) as submit_event: |
| 88 | await confirm.click() |
| 89 | response = await submit_event.value |
| 90 | if not response.ok: |
| 91 | raise RuntimeError(f"发表结果未确认: HTTP {response.status},请核查原作品") |
| 92 | submitted = (await response.json()).get("data", {}) |
| 93 | if not submitted.get("content_id"): |
| 94 | raise RuntimeError("发表响应未返回 content_id,结果未知,请核查原作品") |
| 95 | # 最终发表响应返回本次作品 ID 即成功;审核和公开访问不属于发布命令的完成条件。 |
| 96 | result.update(content_id=str(submitted["content_id"]), article_id=submitted.get("_id")) |
| 97 | result_file.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8") |
| 98 | if result.get("schedule"): |
| 99 | # content_id 确认已提交,再从作品列表复核原作品及安排;草稿不能算定时提交成功。 |
| 100 | record = await wait_dayu_publication(page.context, content_id=result["content_id"], |
| 101 | origin_id=result["origin_id"], title=result.get("title"), |
| 102 | schedule=result["schedule"]) |
| 103 | result.update(schedule_verified=True, platform_status=record["status"]) |
| 104 | result.update(success=True, status="scheduled") |
| 105 | else: |
| 106 | result.update(success=True, status="submitted") |
| 107 | result_file.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8") |
| 108 |