From e13d56e8639538221cc0be03fca9d781a33fbdc2 Mon Sep 17 00:00:00 2001 From: liuweiqing Date: Thu, 17 Oct 2024 16:04:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BF=AB=E6=89=8B=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uploader/ks_uploader/main.py | 75 ++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/uploader/ks_uploader/main.py b/uploader/ks_uploader/main.py index dd7470b..8785028 100644 --- a/uploader/ks_uploader/main.py +++ b/uploader/ks_uploader/main.py @@ -5,6 +5,7 @@ from playwright.async_api import Playwright, async_playwright import os import asyncio +from conf import LOCAL_CHROME_PATH from utils.base_social_media import set_init_script from utils.files_times import get_absolute_path from utils.log import kuaishou_logger @@ -68,6 +69,7 @@ class KSVideo(object): self.publish_date = publish_date self.account_file = account_file self.date_format = '%Y-%m-%d %H:%M' + self.local_executable_path = LOCAL_CHROME_PATH async def handle_upload_error(self, page): kuaishou_logger.error("视频出错了,重新上传中") @@ -75,10 +77,19 @@ class KSVideo(object): async def upload(self, playwright: Playwright) -> None: # 使用 Chromium 浏览器启动一个浏览器实例 - browser = await playwright.chromium.launch(headless=False) - # 创建一个浏览器上下文,使用指定的 cookie 文件 + print(self.local_executable_path) + if self.local_executable_path: + browser = await playwright.chromium.launch( + headless=False, + executable_path=self.local_executable_path, + ) + else: + browser = await playwright.chromium.launch( + headless=False + ) # 创建一个浏览器上下文,使用指定的 cookie 文件 context = await browser.new_context(storage_state=f"{self.account_file}") context = await set_init_script(context) + context.on("close", lambda: context.storage_state(path=self.account_file)) # 创建一个新的页面 page = await context.new_page() @@ -99,8 +110,8 @@ class KSVideo(object): await asyncio.sleep(2) - if not await page.get_by_text("封面编辑").count(): - raise Exception("似乎没有跳转到到编辑页面") + # if not await page.get_by_text("封面编辑").count(): + # raise Exception("似乎没有跳转到到编辑页面") await asyncio.sleep(1) @@ -110,7 +121,7 @@ class KSVideo(object): await new_feature_button.click() kuaishou_logger.info("正在填充标题和话题...") - await page.get_by_text('填写描述').locator("xpath=following-sibling::div").click() + await page.get_by_text("描述").locator("xpath=following-sibling::div").click() kuaishou_logger.info("clear existing title") await page.keyboard.press("Backspace") await page.keyboard.press("Control+KeyA") @@ -122,23 +133,31 @@ class KSVideo(object): # 快手只能添加3个话题 for index, tag in enumerate(self.tags[:3], start=1): kuaishou_logger.info("正在添加第%s个话题" % index) - await page.locator('span:text("#话题")').click() - await page.type('div.clGhv3UpdEo-', tag, delay=100) + await page.keyboard.type(f"#{tag} ") await asyncio.sleep(2) - await page.locator('div.FZcv90s7kFs- > div').nth(0).click() - while True: + max_retries = 60 # 设置最大重试次数,最大等待时间为 2 分钟 + retry_count = 0 + + while retry_count < max_retries: try: - number = await page.locator('div > span:text("上传成功")').count() - if number > 0: + # 获取包含 '上传中' 文本的元素数量 + number = await page.locator("text=上传中").count() + + if number == 0: kuaishou_logger.success("视频上传完毕") break else: - kuaishou_logger.info("正在上传视频中...") + if retry_count % 5 == 0: + kuaishou_logger.info("正在上传视频中...") await asyncio.sleep(2) - except: - kuaishou_logger.info("正在上传视频中...") - await asyncio.sleep(2) + except Exception as e: + kuaishou_logger.error(f"检查上传状态时发生错误: {e}") + await asyncio.sleep(2) # 等待 2 秒后重试 + retry_count += 1 + + if retry_count == max_retries: + kuaishou_logger.warning("超过最大重试次数,视频上传可能未完成。") # 定时任务 if self.publish_date != 0: @@ -146,25 +165,27 @@ class KSVideo(object): # 判断视频是否发布成功 while True: - # 判断视频是否发布成功 try: - publish_button = page.get_by_role('button', name="发布", exact=True) - if await publish_button.count(): + publish_button = page.get_by_text("发布", exact=True) + if await publish_button.count() > 0: await publish_button.click() await asyncio.sleep(1) - confirm_button = page.locator("button > span:text('确认发布')") - if await confirm_button.count(): - await page.locator("button > span:text('确认发布')").click() + confirm_button = page.get_by_text("确认发布") + if await confirm_button.count() > 0: + await confirm_button.click() - await page.wait_for_url("https://cp.kuaishou.com/article/manage/video?status=2&from=publish", - timeout=1500) + # 等待页面跳转,确认发布成功 + await page.wait_for_url( + "https://cp.kuaishou.com/article/manage/video?status=2&from=publish", + timeout=5000, + ) kuaishou_logger.success("视频发布成功") break - except: - kuaishou_logger.info("视频正在发布中...") + except Exception as e: + kuaishou_logger.info(f"视频正在发布中... 错误: {e}") await page.screenshot(full_page=True) - await asyncio.sleep(0.5) + await asyncio.sleep(1) await context.storage_state(path=self.account_file) # 保存cookie kuaishou_logger.info('cookie更新完毕!') @@ -191,5 +212,3 @@ class KSVideo(object): await page.keyboard.type(str(publish_date_hour)) await page.keyboard.press("Enter") await asyncio.sleep(1) - -