fix: TikTok 发布成功后正确记为已上传,并禁止并发重复上传
vendor 的 upload_video 成功后没有 return True,被误判失败;同时立即上传会抢正在上传的任务导致重复发布。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
0815e05094
commit
c7a7275229
@ -428,16 +428,23 @@ def claim_tiktok_job_manual_upload(job_id: str) -> dict[str, Any] | None:
|
||||
return None
|
||||
if not str(row["clip_path"] or "").strip():
|
||||
return None
|
||||
if str(row["tiktok_video_id"] or "").strip():
|
||||
if str(row["tiktok_video_id"] or "").strip() or str(row["tiktok_publish_id"] or "").strip():
|
||||
return None
|
||||
stale_before = now - 180
|
||||
if st == "uploading" and float(row["updated_at"] or 0) >= stale_before:
|
||||
return None
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE tiktok_jobs
|
||||
SET status = 'uploading', upload_at = ?, attempts = attempts + 1,
|
||||
error = '', finished_at = 0, updated_at = ?
|
||||
WHERE id = ? AND tiktok_video_id = ''
|
||||
WHERE id = ? AND IFNULL(tiktok_video_id,'') = '' AND IFNULL(tiktok_publish_id,'') = ''
|
||||
AND (
|
||||
status IN ('scheduled', 'recorded', 'failed')
|
||||
OR (status = 'uploading' AND updated_at < ?)
|
||||
)
|
||||
""",
|
||||
(now, now, job_id),
|
||||
(now, now, job_id, stale_before),
|
||||
)
|
||||
if conn.total_changes == 0:
|
||||
conn.rollback()
|
||||
|
||||
@ -170,7 +170,16 @@ class TikTokPipeline:
|
||||
job = tiktok_db.claim_tiktok_job_manual_upload(job_id)
|
||||
if not job:
|
||||
raise ValueError("任务不可立即上传(需要成片且未发布;上传中卡住的可强制重试)")
|
||||
await self._do_upload(job)
|
||||
try:
|
||||
await self._do_upload(job)
|
||||
except Exception as exc:
|
||||
tiktok_db.update_tiktok_job(
|
||||
job_id,
|
||||
status="failed",
|
||||
error=str(exc)[:500],
|
||||
finished_at=time.time(),
|
||||
)
|
||||
raise
|
||||
fresh = tiktok_db.get_tiktok_job(job_id)
|
||||
if not fresh:
|
||||
raise ValueError("任务不存在")
|
||||
|
||||
@ -54,18 +54,23 @@ def _upload_sync(
|
||||
|
||||
session_user = channel_pk
|
||||
visibility = _privacy_to_visibility(privacy_level)
|
||||
import contextlib
|
||||
import io
|
||||
|
||||
buf = io.StringIO()
|
||||
try:
|
||||
ok = upload_video(
|
||||
session_user,
|
||||
str(path.resolve()),
|
||||
(title or "video")[:2200],
|
||||
schedule_time=0,
|
||||
allow_comment=int(allow_comment),
|
||||
allow_duet=int(allow_duet),
|
||||
allow_stitch=int(allow_stitch),
|
||||
visibility_type=visibility,
|
||||
proxy=proxy or None,
|
||||
)
|
||||
with contextlib.redirect_stdout(buf), contextlib.redirect_stderr(buf):
|
||||
ok = upload_video(
|
||||
session_user,
|
||||
str(path.resolve()),
|
||||
(title or "video")[:2200],
|
||||
schedule_time=0,
|
||||
allow_comment=int(allow_comment),
|
||||
allow_duet=int(allow_duet),
|
||||
allow_stitch=int(allow_stitch),
|
||||
visibility_type=visibility,
|
||||
proxy=proxy or None,
|
||||
)
|
||||
except KeyError as exc:
|
||||
key = str(exc).strip("'\"")
|
||||
if key == "project":
|
||||
@ -75,15 +80,23 @@ def _upload_sync(
|
||||
raise ValueError(f"TikTok 上传失败:缺少字段 {key}") from exc
|
||||
except SystemExit as exc:
|
||||
raise ValueError("TikTok Cookie 缺少 sessionid,请重新粘贴") from exc
|
||||
if not ok:
|
||||
raise ValueError("TikTok 上传失败,请检查 Cookie 是否过期或 Node.js 是否可用")
|
||||
return {
|
||||
"ok": True,
|
||||
"publish_id": f"cookie-{uuid.uuid4().hex[:12]}",
|
||||
"video_id": "",
|
||||
"mode": "cookie",
|
||||
"privacy_level": privacy_level,
|
||||
}
|
||||
log = buf.getvalue()
|
||||
if log:
|
||||
print(log, end="")
|
||||
published = "Published successfully" in log
|
||||
if published or ok is True:
|
||||
return {
|
||||
"ok": True,
|
||||
"publish_id": f"cookie-{uuid.uuid4().hex[:12]}",
|
||||
"video_id": "",
|
||||
"mode": "cookie",
|
||||
"privacy_level": privacy_level,
|
||||
"dc_id": working_dc,
|
||||
}
|
||||
tail = " ".join(line.strip() for line in log.strip().splitlines()[-4:] if line.strip())[:240]
|
||||
raise ValueError(
|
||||
tail or "TikTok 上传失败,请检查 Cookie 是否过期或 Node.js 是否可用"
|
||||
)
|
||||
|
||||
|
||||
async def upload_tiktok_mp4(
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import time
|
||||
|
||||
from app import tiktok_db
|
||||
from app import tiktok_session
|
||||
|
||||
@ -14,12 +16,31 @@ def test_save_and_load_session_cookies(temp_db, tmp_path, monkeypatch):
|
||||
assert "tt-target-idc" in names
|
||||
|
||||
|
||||
def test_manual_upload_rejects_in_flight_uploading(temp_db, tmp_path):
|
||||
ch = tiktok_db.create_tiktok_channel(name="tt3")
|
||||
job = tiktok_db.create_tiktok_job(session_id="", room_id="r1", channel_pk=ch["id"])
|
||||
clip = tmp_path / "a.mp4"
|
||||
clip.write_bytes(b"x")
|
||||
tiktok_db.update_tiktok_job(job["id"], status="uploading", clip_path=str(clip))
|
||||
assert tiktok_db.claim_tiktok_job_manual_upload(job["id"]) is None
|
||||
|
||||
|
||||
def test_manual_upload_allows_stuck_uploading(temp_db, tmp_path):
|
||||
ch = tiktok_db.create_tiktok_channel(name="tt3")
|
||||
job = tiktok_db.create_tiktok_job(session_id="", room_id="r1", channel_pk=ch["id"])
|
||||
clip = tmp_path / "a.mp4"
|
||||
clip.write_bytes(b"x")
|
||||
tiktok_db.update_tiktok_job(job["id"], status="uploading", clip_path=str(clip))
|
||||
with tiktok_db._lock:
|
||||
conn = tiktok_db._connect()
|
||||
try:
|
||||
conn.execute(
|
||||
"UPDATE tiktok_jobs SET updated_at = ? WHERE id = ?",
|
||||
(time.time() - 200, job["id"]),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
claimed = tiktok_db.claim_tiktok_job_manual_upload(job["id"])
|
||||
assert claimed is not None
|
||||
assert claimed["status"] == "uploading"
|
||||
|
||||
@ -120,6 +120,16 @@ def _patch_vendor_upload() -> None:
|
||||
text = text.replace(old, new)
|
||||
changed = True
|
||||
|
||||
old_ret = (
|
||||
'\tif not uploaded:\n'
|
||||
'\t\tprint("[-] Could not upload video")\n'
|
||||
'\t\treturn False\n'
|
||||
)
|
||||
new_ret = old_ret + '\treturn True\n'
|
||||
if old_ret in text and '\treturn True\n' not in text[text.find(old_ret): text.find(old_ret) + 200]:
|
||||
text = text.replace(old_ret, new_ret, 1)
|
||||
changed = True
|
||||
|
||||
if changed:
|
||||
path.write_text(text, encoding="utf-8")
|
||||
print("patched:", path)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user