pookz@stme commited on
Commit ·
13f2fb9
1
Parent(s): f46f91d
1. tk(chrome support)
Browse files2. tk add thumbnail support
- README.MD +11 -6
- cli_main.py +1 -1
- examples/get_tk_cookie.py +1 -1
- examples/upload_video_to_tiktok.py +8 -2
- tk_uploader/main_chrome.py +286 -0
README.MD
CHANGED
|
@@ -67,7 +67,7 @@ meta_file 内容(content):
|
|
| 67 |
```
|
| 68 |
|
| 69 |
### Usage
|
| 70 |
-
1. 设置conf 文件中的 `LOCAL_CHROME_PATH`(在douyin
|
| 71 |
2. 这里分割出来3条路
|
| 72 |
- 可自行研究源码,免费、任意 穿插在自己的项目中
|
| 73 |
- 可参考下面的各个平台的使用指南,`examples`文件夹中有各种示例代码
|
|
@@ -141,7 +141,7 @@ generate_schedule_time_next_day 默认从第二天开始(此举为避免选择
|
|
| 141 |
> 解决方案:
|
| 142 |
> 1. 下载chrome
|
| 143 |
> 2. 找到chrome的安装目录
|
| 144 |
-
> 3. 将
|
| 145 |
|
| 146 |
|
| 147 |
参考项目:
|
|
@@ -168,7 +168,7 @@ generate_schedule_time_next_day 默认从第二天开始(此举为避免选择
|
|
| 168 |
> 解决方案:
|
| 169 |
> 1. 下载chrome
|
| 170 |
> 2. 找到chrome的安装目录
|
| 171 |
-
> 3. 将
|
| 172 |
|
| 173 |
---
|
| 174 |
|
|
@@ -234,11 +234,16 @@ bilibili cookie 长期有效(至少我运行2年以来是这样的)
|
|
| 234 |
### 5. tiktok
|
| 235 |
使用playwright模拟浏览器行为(Simulating Browser Behavior with playwright)
|
| 236 |
1. 准备视频目录结构(Prepare the video directory structure)
|
| 237 |
-
2.
|
|
|
|
| 238 |

|
| 239 |
-
|
| 240 |
-
|
| 241 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
其他部分解释:
|
| 244 |
```
|
|
|
|
| 67 |
```
|
| 68 |
|
| 69 |
### Usage
|
| 70 |
+
1. 设置conf 文件中的 `LOCAL_CHROME_PATH`(在douyin、视频号 tiktok可能出现chromium 不兼容的各种问题,建议设置本地的chrome)
|
| 71 |
2. 这里分割出来3条路
|
| 72 |
- 可自行研究源码,免费、任意 穿插在自己的项目中
|
| 73 |
- 可参考下面的各个平台的使用指南,`examples`文件夹中有各种示例代码
|
|
|
|
| 141 |
> 解决方案:
|
| 142 |
> 1. 下载chrome
|
| 143 |
> 2. 找到chrome的安装目录
|
| 144 |
+
> 3. 将本地chrome 路径conf文件中 LOCAL_CHROME_PATH = "xxx/xxx/chrome.exe"
|
| 145 |
|
| 146 |
|
| 147 |
参考项目:
|
|
|
|
| 168 |
> 解决方案:
|
| 169 |
> 1. 下载chrome
|
| 170 |
> 2. 找到chrome的安装目录
|
| 171 |
+
> 3. 将本地chrome 路径conf文件中 LOCAL_CHROME_PATH = "xxx/xxx/chrome.exe"
|
| 172 |
|
| 173 |
---
|
| 174 |
|
|
|
|
| 234 |
### 5. tiktok
|
| 235 |
使用playwright模拟浏览器行为(Simulating Browser Behavior with playwright)
|
| 236 |
1. 准备视频目录结构(Prepare the video directory structure)
|
| 237 |
+
2. 将本地chrome路径配置到conf文件中 LOCAL_CHROME_PATH = "xxx/xxx/chrome.exe"(Configure the local chrome path to the conf file)
|
| 238 |
+
3. cookie获取(generate your cookie):get_tk_cookie.py
|
| 239 |

|
| 240 |
+
4. 上传视频(upload video):upload_video_to_tiktok.py
|
|
|
|
| 241 |
|
| 242 |
+
24.7.3 update local chrome support:
|
| 243 |
+
- you can upload your custom thumbnail(place `png` file at the path of `videos`)
|
| 244 |
+
- if not, the system will take the first frame of the video as the thumbnail.
|
| 245 |
+
- chrome drive can't login by gmail oauth. the google don't support it. (if you want login by google oauth you can use the `tk_uploader/main.py` old firefox.)
|
| 246 |
+
- before the upload process change the language to eng.
|
| 247 |
|
| 248 |
其他部分解释:
|
| 249 |
```
|
cli_main.py
CHANGED
|
@@ -7,7 +7,7 @@ from pathlib import Path
|
|
| 7 |
from conf import BASE_DIR
|
| 8 |
from douyin_uploader.main import douyin_setup, DouYinVideo
|
| 9 |
from tencent_uploader.main import weixin_setup, TencentVideo
|
| 10 |
-
from tk_uploader.
|
| 11 |
from utils.base_social_media import get_supported_social_media, get_cli_action, SOCIAL_MEDIA_DOUYIN, \
|
| 12 |
SOCIAL_MEDIA_TENCENT, SOCIAL_MEDIA_TIKTOK
|
| 13 |
from utils.constant import TencentZoneTypes
|
|
|
|
| 7 |
from conf import BASE_DIR
|
| 8 |
from douyin_uploader.main import douyin_setup, DouYinVideo
|
| 9 |
from tencent_uploader.main import weixin_setup, TencentVideo
|
| 10 |
+
from tk_uploader.main_chrome import tiktok_setup, TiktokVideo
|
| 11 |
from utils.base_social_media import get_supported_social_media, get_cli_action, SOCIAL_MEDIA_DOUYIN, \
|
| 12 |
SOCIAL_MEDIA_TENCENT, SOCIAL_MEDIA_TIKTOK
|
| 13 |
from utils.constant import TencentZoneTypes
|
examples/get_tk_cookie.py
CHANGED
|
@@ -2,7 +2,7 @@ import asyncio
|
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
from conf import BASE_DIR
|
| 5 |
-
from tk_uploader.
|
| 6 |
|
| 7 |
if __name__ == '__main__':
|
| 8 |
account_file = Path(BASE_DIR / "tk_uploader" / "account.json")
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
from conf import BASE_DIR
|
| 5 |
+
from tk_uploader.main_chrome import tiktok_setup
|
| 6 |
|
| 7 |
if __name__ == '__main__':
|
| 8 |
account_file = Path(BASE_DIR / "tk_uploader" / "account.json")
|
examples/upload_video_to_tiktok.py
CHANGED
|
@@ -2,7 +2,8 @@ import asyncio
|
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
from conf import BASE_DIR
|
| 5 |
-
from tk_uploader.main import tiktok_setup, TiktokVideo
|
|
|
|
| 6 |
from utils.files_times import generate_schedule_time_next_day, get_title_and_hashtags
|
| 7 |
|
| 8 |
|
|
@@ -17,8 +18,13 @@ if __name__ == '__main__':
|
|
| 17 |
cookie_setup = asyncio.run(tiktok_setup(account_file, handle=True))
|
| 18 |
for index, file in enumerate(files):
|
| 19 |
title, tags = get_title_and_hashtags(str(file))
|
|
|
|
| 20 |
print(f"video_file_name:{file}")
|
| 21 |
print(f"video_title:{title}")
|
| 22 |
print(f"video_hashtag:{tags}")
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
asyncio.run(app.main(), debug=False)
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
|
| 4 |
from conf import BASE_DIR
|
| 5 |
+
# from tk_uploader.main import tiktok_setup, TiktokVideo
|
| 6 |
+
from tk_uploader.main_chrome import tiktok_setup, TiktokVideo
|
| 7 |
from utils.files_times import generate_schedule_time_next_day, get_title_and_hashtags
|
| 8 |
|
| 9 |
|
|
|
|
| 18 |
cookie_setup = asyncio.run(tiktok_setup(account_file, handle=True))
|
| 19 |
for index, file in enumerate(files):
|
| 20 |
title, tags = get_title_and_hashtags(str(file))
|
| 21 |
+
thumbnail_path = file.with_suffix('.png')
|
| 22 |
print(f"video_file_name:{file}")
|
| 23 |
print(f"video_title:{title}")
|
| 24 |
print(f"video_hashtag:{tags}")
|
| 25 |
+
if thumbnail_path.exists():
|
| 26 |
+
print(f"thumbnail_file_name:{thumbnail_path}")
|
| 27 |
+
app = TiktokVideo(title, file, tags, publish_datetimes[index], account_file, thumbnail_path)
|
| 28 |
+
else:
|
| 29 |
+
app = TiktokVideo(title, file, tags, publish_datetimes[index], account_file)
|
| 30 |
asyncio.run(app.main(), debug=False)
|
tk_uploader/main_chrome.py
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
import re
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
from playwright.async_api import Playwright, async_playwright
|
| 6 |
+
import os
|
| 7 |
+
import asyncio
|
| 8 |
+
|
| 9 |
+
from conf import LOCAL_CHROME_PATH
|
| 10 |
+
from tk_uploader.tk_config import Tk_Locator
|
| 11 |
+
from utils.base_social_media import set_init_script
|
| 12 |
+
from utils.files_times import get_absolute_path
|
| 13 |
+
from utils.log import tiktok_logger
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
async def cookie_auth(account_file):
|
| 17 |
+
async with async_playwright() as playwright:
|
| 18 |
+
browser = await playwright.chromium.launch(headless=True)
|
| 19 |
+
context = await browser.new_context(storage_state=account_file)
|
| 20 |
+
context = await set_init_script(context)
|
| 21 |
+
# 创建一个新的页面
|
| 22 |
+
page = await context.new_page()
|
| 23 |
+
# 访问指定的 URL
|
| 24 |
+
await page.goto("https://www.tiktok.com/tiktokstudio/upload?lang=en")
|
| 25 |
+
await page.wait_for_load_state('networkidle')
|
| 26 |
+
try:
|
| 27 |
+
# 选择所有的 select 元素
|
| 28 |
+
select_elements = await page.query_selector_all('select')
|
| 29 |
+
for element in select_elements:
|
| 30 |
+
class_name = await element.get_attribute('class')
|
| 31 |
+
# 使用正则表达式匹配特定模式的 class 名称
|
| 32 |
+
if re.match(r'tiktok-.*-SelectFormContainer.*', class_name):
|
| 33 |
+
tiktok_logger.error("[+] cookie expired")
|
| 34 |
+
return False
|
| 35 |
+
tiktok_logger.success("[+] cookie valid")
|
| 36 |
+
return True
|
| 37 |
+
except:
|
| 38 |
+
tiktok_logger.success("[+] cookie valid")
|
| 39 |
+
return True
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
async def tiktok_setup(account_file, handle=False):
|
| 43 |
+
account_file = get_absolute_path(account_file, "tk_uploader")
|
| 44 |
+
if not os.path.exists(account_file) or not await cookie_auth(account_file):
|
| 45 |
+
if not handle:
|
| 46 |
+
return False
|
| 47 |
+
tiktok_logger.info('[+] cookie file is not existed or expired. Now open the browser auto. Please login with your way(gmail phone, whatever, the cookie file will generated after login')
|
| 48 |
+
await get_tiktok_cookie(account_file)
|
| 49 |
+
return True
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
async def get_tiktok_cookie(account_file):
|
| 53 |
+
async with async_playwright() as playwright:
|
| 54 |
+
options = {
|
| 55 |
+
'args': [
|
| 56 |
+
'--lang en-GB',
|
| 57 |
+
],
|
| 58 |
+
'headless': False, # Set headless option here
|
| 59 |
+
}
|
| 60 |
+
# Make sure to run headed.
|
| 61 |
+
browser = await playwright.chromium.launch(**options)
|
| 62 |
+
# Setup context however you like.
|
| 63 |
+
context = await browser.new_context() # Pass any options
|
| 64 |
+
context = await set_init_script(context)
|
| 65 |
+
# Pause the page, and start recording manually.
|
| 66 |
+
page = await context.new_page()
|
| 67 |
+
await page.goto("https://www.tiktok.com/login?lang=en")
|
| 68 |
+
await page.pause()
|
| 69 |
+
# 点击调试器的继续,保存cookie
|
| 70 |
+
await context.storage_state(path=account_file)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class TiktokVideo(object):
|
| 74 |
+
def __init__(self, title, file_path, tags, publish_date: datetime, account_file, thumbnail_path=None):
|
| 75 |
+
self.title = title
|
| 76 |
+
self.file_path = file_path
|
| 77 |
+
self.tags = tags
|
| 78 |
+
self.publish_date = publish_date
|
| 79 |
+
self.thumbnail_path = thumbnail_path
|
| 80 |
+
self.account_file = account_file
|
| 81 |
+
self.local_executable_path = LOCAL_CHROME_PATH
|
| 82 |
+
|
| 83 |
+
async def set_schedule_time(self, page, publish_date):
|
| 84 |
+
schedule_input_element = page.frame_locator(Tk_Locator.tk_iframe).get_by_label('Schedule')
|
| 85 |
+
await schedule_input_element.wait_for(state='visible') # 确保按钮可见
|
| 86 |
+
|
| 87 |
+
await schedule_input_element.click()
|
| 88 |
+
if await page.frame_locator(Tk_Locator.tk_iframe).locator('div.TUXButton-content >> text=Allow').count():
|
| 89 |
+
await page.frame_locator(Tk_Locator.tk_iframe).locator('div.TUXButton-content >> text=Allow').click()
|
| 90 |
+
|
| 91 |
+
scheduled_picker = page.frame_locator(Tk_Locator.tk_iframe).locator('div.scheduled-picker')
|
| 92 |
+
await scheduled_picker.locator('div.TUXInputBox').nth(1).click()
|
| 93 |
+
|
| 94 |
+
calendar_month = await page.frame_locator(Tk_Locator.tk_iframe).locator(
|
| 95 |
+
'div.calendar-wrapper span.month-title').inner_text()
|
| 96 |
+
|
| 97 |
+
n_calendar_month = datetime.strptime(calendar_month, '%B').month
|
| 98 |
+
|
| 99 |
+
schedule_month = publish_date.month
|
| 100 |
+
|
| 101 |
+
if n_calendar_month != schedule_month:
|
| 102 |
+
if n_calendar_month < schedule_month:
|
| 103 |
+
arrow = page.frame_locator(Tk_Locator.tk_iframe).locator('div.calendar-wrapper span.arrow').nth(-1)
|
| 104 |
+
else:
|
| 105 |
+
arrow = page.frame_locator(Tk_Locator.tk_iframe).locator('div.calendar-wrapper span.arrow').nth(0)
|
| 106 |
+
await arrow.click()
|
| 107 |
+
|
| 108 |
+
# day set
|
| 109 |
+
valid_days_locator = page.frame_locator(Tk_Locator.tk_iframe).locator(
|
| 110 |
+
'div.calendar-wrapper span.day.valid')
|
| 111 |
+
valid_days = await valid_days_locator.count()
|
| 112 |
+
for i in range(valid_days):
|
| 113 |
+
day_element = valid_days_locator.nth(i)
|
| 114 |
+
text = await day_element.inner_text()
|
| 115 |
+
if text.strip() == str(publish_date.day):
|
| 116 |
+
await day_element.click()
|
| 117 |
+
break
|
| 118 |
+
# time set
|
| 119 |
+
await scheduled_picker.locator('div.TUXInputBox').nth(0).click()
|
| 120 |
+
|
| 121 |
+
hour_str = publish_date.strftime("%H")
|
| 122 |
+
correct_minute = int(publish_date.minute / 5)
|
| 123 |
+
minute_str = f"{correct_minute:02d}"
|
| 124 |
+
|
| 125 |
+
hour_selector = f"span.tiktok-timepicker-left:has-text('{hour_str}')"
|
| 126 |
+
minute_selector = f"span.tiktok-timepicker-right:has-text('{minute_str}')"
|
| 127 |
+
|
| 128 |
+
# pick hour first
|
| 129 |
+
await page.wait_for_timeout(500) # 等待500毫秒
|
| 130 |
+
await page.frame_locator(Tk_Locator.tk_iframe).locator(hour_selector).click()
|
| 131 |
+
# click time button again
|
| 132 |
+
await page.wait_for_timeout(500) # 等待500毫秒
|
| 133 |
+
await scheduled_picker.locator('div.TUXInputBox').nth(0).click()
|
| 134 |
+
await page.wait_for_timeout(500) # 等待500毫秒
|
| 135 |
+
# pick minutes after
|
| 136 |
+
await page.frame_locator(Tk_Locator.tk_iframe).locator(minute_selector).click()
|
| 137 |
+
|
| 138 |
+
# click title to remove the focus.
|
| 139 |
+
# await page.frame_locator(Tk_Locator.tk_iframe).locator("h1:has-text('Upload video')").click()
|
| 140 |
+
|
| 141 |
+
async def handle_upload_error(self, page):
|
| 142 |
+
tiktok_logger.info("video upload error retrying.")
|
| 143 |
+
select_file_button = page.frame_locator(Tk_Locator.tk_iframe).locator('button[aria-label="Select file"]')
|
| 144 |
+
async with page.expect_file_chooser() as fc_info:
|
| 145 |
+
await select_file_button.click()
|
| 146 |
+
file_chooser = await fc_info.value
|
| 147 |
+
await file_chooser.set_files(self.file_path)
|
| 148 |
+
|
| 149 |
+
async def upload(self, playwright: Playwright) -> None:
|
| 150 |
+
browser = await playwright.chromium.launch(headless=False, executable_path=self.local_executable_path)
|
| 151 |
+
context = await browser.new_context(storage_state=f"{self.account_file}")
|
| 152 |
+
context = await set_init_script(context)
|
| 153 |
+
page = await context.new_page()
|
| 154 |
+
|
| 155 |
+
# change language to eng first
|
| 156 |
+
await self.change_language(page)
|
| 157 |
+
await page.goto("https://www.tiktok.com/tiktokstudio/upload")
|
| 158 |
+
tiktok_logger.info(f'[+]Uploading-------{self.title}.mp4')
|
| 159 |
+
|
| 160 |
+
await page.wait_for_url("https://www.tiktok.com/tiktokstudio/upload", timeout=10000)
|
| 161 |
+
await page.wait_for_selector('iframe[data-tt="Upload_index_iframe"]')
|
| 162 |
+
upload_button = page.frame_locator(Tk_Locator.tk_iframe).locator(
|
| 163 |
+
'button:has-text("Select video"):visible')
|
| 164 |
+
await upload_button.wait_for(state='visible') # 确保按钮可见
|
| 165 |
+
|
| 166 |
+
async with page.expect_file_chooser() as fc_info:
|
| 167 |
+
await upload_button.click()
|
| 168 |
+
file_chooser = await fc_info.value
|
| 169 |
+
await file_chooser.set_files(self.file_path)
|
| 170 |
+
|
| 171 |
+
await self.add_title_tags(page)
|
| 172 |
+
# detact upload status
|
| 173 |
+
await self.detact_upload_status(page)
|
| 174 |
+
if self.thumbnail_path:
|
| 175 |
+
tiktok_logger.info(f'[+] Uploading thumbnail file {self.title}.png')
|
| 176 |
+
await self.upload_thumbnails(page)
|
| 177 |
+
|
| 178 |
+
if self.publish_date != 0:
|
| 179 |
+
await self.set_schedule_time(page, self.publish_date)
|
| 180 |
+
|
| 181 |
+
await self.click_publish(page)
|
| 182 |
+
|
| 183 |
+
await context.storage_state(path=f"{self.account_file}") # save cookie
|
| 184 |
+
tiktok_logger.info(' [-] update cookie!')
|
| 185 |
+
await asyncio.sleep(2) # close delay for look the video status
|
| 186 |
+
# close all
|
| 187 |
+
await context.close()
|
| 188 |
+
await browser.close()
|
| 189 |
+
|
| 190 |
+
async def add_title_tags(self, page):
|
| 191 |
+
|
| 192 |
+
editor_locator = page.frame_locator(Tk_Locator.tk_iframe).locator('div.public-DraftEditor-content')
|
| 193 |
+
await editor_locator.click()
|
| 194 |
+
|
| 195 |
+
await page.keyboard.press("End")
|
| 196 |
+
|
| 197 |
+
await page.keyboard.press("Control+A")
|
| 198 |
+
|
| 199 |
+
await page.keyboard.press("Delete")
|
| 200 |
+
|
| 201 |
+
await page.keyboard.press("End")
|
| 202 |
+
|
| 203 |
+
await page.wait_for_timeout(1000) # 等待1秒
|
| 204 |
+
|
| 205 |
+
await page.keyboard.insert_text(self.title)
|
| 206 |
+
await page.wait_for_timeout(1000) # 等待1秒
|
| 207 |
+
await page.keyboard.press("End")
|
| 208 |
+
|
| 209 |
+
await page.keyboard.press("Enter")
|
| 210 |
+
|
| 211 |
+
# tag part
|
| 212 |
+
for index, tag in enumerate(self.tags, start=1):
|
| 213 |
+
tiktok_logger.info("Setting the %s tag" % index)
|
| 214 |
+
await page.keyboard.press("End")
|
| 215 |
+
await page.wait_for_timeout(1000) # 等待1秒
|
| 216 |
+
await page.keyboard.insert_text("#" + tag + " ")
|
| 217 |
+
await page.keyboard.press("Space")
|
| 218 |
+
await page.wait_for_timeout(1000) # 等待1秒
|
| 219 |
+
|
| 220 |
+
await page.keyboard.press("Backspace")
|
| 221 |
+
await page.keyboard.press("End")
|
| 222 |
+
|
| 223 |
+
async def upload_thumbnails(self, page):
|
| 224 |
+
await page.frame_locator(Tk_Locator.tk_iframe).locator(".cover-selector-image-container").click()
|
| 225 |
+
await page.frame_locator(Tk_Locator.tk_iframe).locator(".cover-edit-container >> text=Upload cover").click()
|
| 226 |
+
async with page.expect_file_chooser() as fc_info:
|
| 227 |
+
await page.frame_locator(Tk_Locator.tk_iframe).locator(".upload-image-upload-area").click()
|
| 228 |
+
file_chooser = await fc_info.value
|
| 229 |
+
await file_chooser.set_files(self.thumbnail_path)
|
| 230 |
+
await page.frame_locator(Tk_Locator.tk_iframe).locator('div.cover-edit-panel:not(.hide-panel)').get_by_role(
|
| 231 |
+
"button", name="Confirm").click()
|
| 232 |
+
await page.wait_for_timeout(3000) # wait 3s, fix it later
|
| 233 |
+
|
| 234 |
+
async def change_language(self, page):
|
| 235 |
+
# set the language to english
|
| 236 |
+
await page.goto("https://www.tiktok.com")
|
| 237 |
+
await page.wait_for_url("https://www.tiktok.com/", timeout=100000)
|
| 238 |
+
await page.wait_for_selector('#header-more-menu-icon')
|
| 239 |
+
|
| 240 |
+
await page.locator('#header-more-menu-icon').hover()
|
| 241 |
+
await page.locator('[data-e2e="language-select"]').click()
|
| 242 |
+
await page.locator('#lang-setting-popup-list >> text=English').click()
|
| 243 |
+
|
| 244 |
+
async def click_publish(self, page):
|
| 245 |
+
success_flag_div = 'div.common-modal-confirm-modal'
|
| 246 |
+
while True:
|
| 247 |
+
try:
|
| 248 |
+
publish_button = page.frame_locator(Tk_Locator.tk_iframe).locator('div.button-group button').nth(0)
|
| 249 |
+
if await publish_button.count():
|
| 250 |
+
await publish_button.click()
|
| 251 |
+
|
| 252 |
+
await page.frame_locator(Tk_Locator.tk_iframe).locator(success_flag_div).wait_for(state="visible", timeout=1500)
|
| 253 |
+
tiktok_logger.success(" [-] video published success")
|
| 254 |
+
break
|
| 255 |
+
except Exception as e:
|
| 256 |
+
if await page.frame_locator(Tk_Locator.tk_iframe).locator(success_flag_div).count():
|
| 257 |
+
tiktok_logger.success(" [-]video published success")
|
| 258 |
+
break
|
| 259 |
+
else:
|
| 260 |
+
tiktok_logger.exception(f" [-] Exception: {e}")
|
| 261 |
+
tiktok_logger.info(" [-] video publishing")
|
| 262 |
+
await page.screenshot(full_page=True)
|
| 263 |
+
await asyncio.sleep(0.5)
|
| 264 |
+
|
| 265 |
+
async def detact_upload_status(self, page):
|
| 266 |
+
while True:
|
| 267 |
+
try:
|
| 268 |
+
# if await page.frame_locator(Tk_Locator.tk_iframe).locator('div.btn-post > button').get_attribute("disabled") is None:
|
| 269 |
+
if await page.frame_locator(Tk_Locator.tk_iframe).locator(
|
| 270 |
+
'div.button-group > button >> text=Post').get_attribute("disabled") is None:
|
| 271 |
+
tiktok_logger.info(" [-]video uploaded.")
|
| 272 |
+
break
|
| 273 |
+
else:
|
| 274 |
+
tiktok_logger.info(" [-] video uploading...")
|
| 275 |
+
await asyncio.sleep(2)
|
| 276 |
+
if await page.frame_locator(Tk_Locator.tk_iframe).locator(
|
| 277 |
+
'button[aria-label="Select file"]').count():
|
| 278 |
+
tiktok_logger.info(" [-] found some error while uploading now retry...")
|
| 279 |
+
await self.handle_upload_error(page)
|
| 280 |
+
except:
|
| 281 |
+
tiktok_logger.info(" [-] video uploading...")
|
| 282 |
+
await asyncio.sleep(2)
|
| 283 |
+
|
| 284 |
+
async def main(self):
|
| 285 |
+
async with async_playwright() as playwright:
|
| 286 |
+
await self.upload(playwright)
|