[feature] support product link in douyin
This commit is contained in:
@@ -64,7 +64,7 @@ async def douyin_cookie_gen(account_file):
|
||||
|
||||
|
||||
class DouYinVideo(object):
|
||||
def __init__(self, title, file_path, tags, publish_date: datetime, account_file, thumbnail_path=None):
|
||||
def __init__(self, title, file_path, tags, publish_date: datetime, account_file, thumbnail_path=None, productLink='', productTitle=''):
|
||||
self.title = title # 视频标题
|
||||
self.file_path = file_path
|
||||
self.tags = tags
|
||||
@@ -73,6 +73,8 @@ class DouYinVideo(object):
|
||||
self.date_format = '%Y年%m月%d日 %H:%M'
|
||||
self.local_executable_path = LOCAL_CHROME_PATH
|
||||
self.thumbnail_path = thumbnail_path
|
||||
self.productLink = productLink
|
||||
self.productTitle = productTitle
|
||||
|
||||
async def set_schedule_time_douyin(self, page, publish_date):
|
||||
# 选择包含特定文本内容的 label 元素
|
||||
@@ -157,6 +159,10 @@ class DouYinVideo(object):
|
||||
await page.press(css_selector, "Space")
|
||||
douyin_logger.info(f'总共添加{len(self.tags)}个话题')
|
||||
|
||||
if self.productLink and self.productTitle:
|
||||
await asyncio.sleep(1)
|
||||
await self.set_product_link(page, self.productLink, self.productTitle)
|
||||
|
||||
while True:
|
||||
# 判断重新上传按钮是否存在,如果不存在,代表视频正在上传,则等待
|
||||
try:
|
||||
@@ -242,6 +248,86 @@ class DouYinVideo(object):
|
||||
await page.wait_for_selector('div[role="listbox"] [role="option"]', timeout=5000)
|
||||
await page.locator('div[role="listbox"] [role="option"]').first.click()
|
||||
|
||||
async def handle_product_dialog(self, page: Page, product_title: str):
|
||||
"""处理商品编辑弹窗"""
|
||||
|
||||
await page.wait_for_timeout(2000)
|
||||
short_title_input = page.locator('input[placeholder="请输入商品短标题"]')
|
||||
if not await short_title_input.count():
|
||||
douyin_logger.error("[-] 未找到商品短标题输入框")
|
||||
return False
|
||||
product_title = product_title[:10]
|
||||
await short_title_input.fill(product_title)
|
||||
# 等待一下让界面响应
|
||||
await page.wait_for_timeout(1000)
|
||||
|
||||
finish_button = page.locator('button:has-text("完成编辑")')
|
||||
if 'disabled' not in await finish_button.get_attribute('class'):
|
||||
await finish_button.click()
|
||||
douyin_logger.debug("[+] 成功点击'完成编辑'按钮")
|
||||
|
||||
# 等待对话框关闭
|
||||
await page.wait_for_selector('.semi-modal-content', state='hidden', timeout=5000)
|
||||
return True
|
||||
else:
|
||||
douyin_logger.error("[-] '完成编辑'按钮处于禁用状态,尝试直接关闭对话框")
|
||||
# 如果按钮禁用,尝试点击取消或关闭按钮
|
||||
cancel_button = page.locator('button:has-text("取消")')
|
||||
if await cancel_button.count():
|
||||
await cancel_button.click()
|
||||
else:
|
||||
# 点击右上角的关闭按钮
|
||||
close_button = page.locator('.semi-modal-close')
|
||||
await close_button.click()
|
||||
|
||||
await page.wait_for_selector('.semi-modal-content', state='hidden', timeout=5000)
|
||||
return False
|
||||
|
||||
async def set_product_link(self, page: Page, product_link: str, product_title: str):
|
||||
"""设置商品链接功能"""
|
||||
try:
|
||||
# 定位"添加标签"文本,然后向上导航到容器,再找到下拉框
|
||||
dropdown = page.get_by_text('添加标签').locator("..").locator("..").locator("..").locator(".semi-select").first
|
||||
if not await dropdown.count():
|
||||
douyin_logger.error("[-] 未找到标签下拉框")
|
||||
return False
|
||||
douyin_logger.debug("[-] 找到标签下拉框,准备选择'购物车'")
|
||||
await dropdown.click()
|
||||
## 等待下拉选项出现
|
||||
await page.wait_for_selector('[role="listbox"]', timeout=5000)
|
||||
## 选择"购物车"选项
|
||||
await page.locator('[role="option"]:has-text("购物车")').click()
|
||||
douyin_logger.debug("[+] 成功选择'购物车'")
|
||||
|
||||
# 输入商品链接
|
||||
## 等待商品链接输入框出现
|
||||
await page.wait_for_selector('input[placeholder="粘贴商品链接"]', timeout=5000)
|
||||
# 输入
|
||||
input_field = page.locator('input[placeholder="粘贴商品链接"]')
|
||||
await input_field.fill(product_link)
|
||||
douyin_logger.debug(f"[+] 已输入商品链接: {product_link}")
|
||||
|
||||
# 点击"添加链接"按钮
|
||||
add_button = page.locator('span:has-text("添加链接")')
|
||||
## 检查按钮是否可用(没有disable类)
|
||||
button_class = await add_button.get_attribute('class')
|
||||
if 'disable' in button_class:
|
||||
douyin_logger.error("[-] '添加链接'按钮不可用")
|
||||
return False
|
||||
await add_button.click()
|
||||
douyin_logger.debug("[+] 成功点击'添加链接'按钮")
|
||||
|
||||
# 填写商品短标题
|
||||
if not await self.handle_product_dialog(page, product_title):
|
||||
return False
|
||||
|
||||
# 等待链接添加完成
|
||||
douyin_logger.debug("[+] 成功设置商品链接")
|
||||
return True
|
||||
except Exception as e:
|
||||
douyin_logger.error(f"[-] 设置商品链接时出错: {str(e)}")
|
||||
return False
|
||||
|
||||
async def main(self):
|
||||
async with async_playwright() as playwright:
|
||||
await self.upload(playwright)
|
||||
|
||||
Reference in New Issue
Block a user