From cd1a82fbb9008687db75fc5d1e52da8338d768dd Mon Sep 17 00:00:00 2001 From: "pookz@stme" Date: Fri, 23 Aug 2024 15:14:37 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E7=BB=9F=E4=B8=80=E5=91=BD=E5=90=8Dthumbn?= =?UTF-8?q?ail=202.=20=E6=9B=B4=E6=8D=A2=E5=AE=8C=E6=88=90=E5=AE=9A?= =?UTF-8?q?=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/upload_video_to_douyin.py | 6 +++++- uploader/douyin_uploader/main.py | 14 +++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/examples/upload_video_to_douyin.py b/examples/upload_video_to_douyin.py index 08023a5..142f012 100644 --- a/examples/upload_video_to_douyin.py +++ b/examples/upload_video_to_douyin.py @@ -18,9 +18,13 @@ if __name__ == '__main__': cookie_setup = asyncio.run(douyin_setup(account_file, handle=False)) for index, file in enumerate(files): title, tags = get_title_and_hashtags(str(file)) + thumbnail_path = file.with_suffix('.png') # 打印视频文件名、标题和 hashtag print(f"视频文件名:{file}") print(f"标题:{title}") print(f"Hashtag:{tags}") - app = DouYinVideo(title, file, tags, publish_datetimes[index], account_file) + if thumbnail_path.exists(): + app = DouYinVideo(title, file, tags, publish_datetimes[index], account_file, thumbnail_path=thumbnail_path) + else: + app = DouYinVideo(title, file, tags, publish_datetimes[index], account_file) asyncio.run(app.main(), debug=False) \ No newline at end of file diff --git a/uploader/douyin_uploader/main.py b/uploader/douyin_uploader/main.py index 86fab10..28e9134 100644 --- a/uploader/douyin_uploader/main.py +++ b/uploader/douyin_uploader/main.py @@ -57,7 +57,7 @@ async def douyin_cookie_gen(account_file): class DouYinVideo(object): - def __init__(self, title, file_path, tags, publish_date: datetime, account_file, cover_path=None): + def __init__(self, title, file_path, tags, publish_date: datetime, account_file, thumbnail_path=None): self.title = title # 视频标题 self.file_path = file_path self.tags = tags @@ -65,7 +65,7 @@ class DouYinVideo(object): self.account_file = account_file self.date_format = '%Y年%m月%d日 %H:%M' self.local_executable_path = LOCAL_CHROME_PATH - self.cover_path = cover_path + self.thumbnail_path = thumbnail_path async def set_schedule_time_douyin(self, page, publish_date): # 选择包含特定文本内容的 label 元素 @@ -161,7 +161,7 @@ class DouYinVideo(object): await asyncio.sleep(2) #上传视频封面 - await self.set_cover(page, self.cover_path) + await self.set_thumbnail(page, self.thumbnail_path) # 更换可见元素 await self.set_location(page, "杭州市") @@ -200,14 +200,14 @@ class DouYinVideo(object): await context.close() await browser.close() - async def set_cover(self, page: Page, cover_path: str): - if cover_path: + async def set_thumbnail(self, page: Page, thumbnail_path: str): + if thumbnail_path: await page.click('text="选择封面"') await page.click('text="上传封面"') # 定位到上传区域并点击 - await page.locator("div[class^='semi-upload upload'] >> input.semi-upload-hidden-input").set_input_files('/mnt/NAS/MassStorage/NewsVideo/231/img_cover_v.jpg') + await page.locator("div[class^='semi-upload upload'] >> input.semi-upload-hidden-input").set_input_files(thumbnail_path) await page.wait_for_timeout(2000) # 等待2秒 - await page.locator('button:has-text("完成")').nth(1).click() + await page.locator("div[class^='uploadCrop'] button:has-text('完成')").click() async def set_location(self, page: Page, location: str = "杭州市"): await page.locator('div.semi-select span:has-text("输入地理位置")').click()