pookz@stme commited on
Commit
95edc20
·
1 Parent(s): 345cc19

upload for tiktok

Browse files
README.MD CHANGED
@@ -1,33 +1,40 @@
1
  # social-auto-upload
2
  social-auto-upload 该项目旨在自动化发布视频到各个社交媒体平台
3
 
 
 
 
4
 
5
  ## 💡Feature
6
  - 中国主流社交媒体平台:
7
- - 抖音
8
- - 视频号
9
- - bilibili
10
- - 小红书
11
- - 快手(todo)
12
 
13
  - 部分国外社交媒体:
14
- - tiktok(todo)
15
- - youtube(todo)
16
- - 自动化上传(schedule)(todo)
17
- - 定时上传(cron)
18
- - cookie 管理(todo)
19
- - 国外平台proxy 设置(todo)
20
- - 多线程上传(todo)
21
- - slack 推送(todo)
22
 
23
 
24
  # 💾Installation
25
  ```
26
- pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
27
- playwright install chromium
28
  ```
29
 
30
  # 🐇 About
 
 
 
 
31
  该项目为我自用项目抽离出来,我的发布策略是定时发布(提前一天发布),故发布部分采用的事件均为第二天的时间
32
 
33
  如果你有需求立即发布,可自行研究源码或者向我提问
@@ -35,17 +42,19 @@ playwright install chromium
35
 
36
  # 核心模块解释
37
 
38
- ### 1. 视频文件准备
39
- filepath 本地视频目录,目录包含
40
- - 视频文件
41
- - 视频meta信息txt文件
 
 
 
42
 
43
- 举例:
44
  file:2023-08-24_16-29-52 - 这位勇敢的男子为了心爱之人每天坚守 .mp4
45
 
46
  meta_file:2023-08-24_16-29-52 - 这位勇敢的男子为了心爱之人每天坚守 .txt
47
 
48
- meta_file 内容:
49
  ```angular2html
50
  这位勇敢的男子为了心爱之人每天坚守 🥺❤️‍🩹
51
  #坚持不懈 #爱情执着 #奋斗使者 #短视频
@@ -166,12 +175,42 @@ bilibili cookie 长期有效(至少我运行2年以来是这样的)
166
 
167
  ---
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  ### 其余部分(todo)
170
  整理后上传
171
 
172
  ---
173
 
174
  ## 🐾Communicate
 
 
175
  探讨自动化上传、自动制作视频
176
  |![Nas](media/mp.jpg)|![赞赏](media/QR.png)|
177
  |:-:|:-:|
 
1
  # social-auto-upload
2
  social-auto-upload 该项目旨在自动化发布视频到各个社交媒体平台
3
 
4
+ social-auto-upload This project aims to automate the posting of videos to various social media platforms.
5
+
6
+ <img src="media/show/tkupload.gif" alt="tiktok show" width="800"/>
7
 
8
  ## 💡Feature
9
  - 中国主流社交媒体平台:
10
+ - [x] 抖音
11
+ - [x] 视频号
12
+ - [x] bilibili
13
+ - [x] 小红书
14
+ - [ ] 快手
15
 
16
  - 部分国外社交媒体:
17
+ - [x] tiktok
18
+ - [ ] youtube
19
+ - [ ] 自动化上传(schedule)
20
+ - [x] 定时上传(cron)
21
+ - [ ] cookie 管理
22
+ - [ ] 国外平台proxy 设置
23
+ - [ ] 多线程上传
24
+ - [ ] slack 推送
25
 
26
 
27
  # 💾Installation
28
  ```
29
+ pip install -r requirements.txt
30
+ playwright install chromium firefox
31
  ```
32
 
33
  # 🐇 About
34
+ The project for my own project extracted, my release strategy is timed release (released a day in advance), so the release part of the event are used for the next day time!
35
+
36
+ If you need to release it immediately, you can study the source code or ask me questions.
37
+
38
  该项目为我自用项目抽离出来,我的发布策略是定时发布(提前一天发布),故发布部分采用的事件均为第二天的时间
39
 
40
  如果你有需求立即发布,可自行研究源码或者向我提问
 
42
 
43
  # 核心模块解释
44
 
45
+ ### 1. 视频文件准备(video prepare)
46
+ filepath 本地视频目录,目录包含(filepath Local video directory containing)
47
+
48
+ - 视频文件(video files)
49
+ - 视频meta信息txt文件(video meta information txt file)
50
+
51
+ 举例(for example):
52
 
 
53
  file:2023-08-24_16-29-52 - 这位勇敢的男子为了心爱之人每天坚守 .mp4
54
 
55
  meta_file:2023-08-24_16-29-52 - 这位勇敢的男子为了心爱之人每天坚守 .txt
56
 
57
+ meta_file 内容(content)
58
  ```angular2html
59
  这位勇敢的男子为了心爱之人每天坚守 🥺❤️‍🩹
60
  #坚持不懈 #爱情执着 #奋斗使者 #短视频
 
175
 
176
  ---
177
 
178
+ ### 6. tiktok
179
+ 使用playwright模拟浏览器行为(Simulating Browser Behavior with playwright)
180
+ 1. 准备视频目录结构(Prepare the video directory structure)
181
+ 2. cookie获取(generate your cookie):get_tk_cookie.py
182
+ ![get tiktok cookie](media/tk_login.png)
183
+ 3. 上传视频(upload video):upload_video_to_tiktok.py
184
+
185
+
186
+
187
+ 其他部分解释:
188
+ ```
189
+ 参考上面douyin_setup 配置
190
+ ```
191
+
192
+ other part explain(for eng friends):
193
+ ```
194
+ tiktok_setup handle parameter is True to get cookie manually False to check cookie validity
195
+
196
+ generate_schedule_time_next_day defaults to start on the next day (this is to avoid accidental time selection errors)
197
+ Parameter explanation:
198
+ - total_videos Number of videos uploaded this time
199
+ - videos_per_day Number of videos uploaded per day
200
+ - daily_times The video posting times are 6, 11, 14, 16, 22 by default.
201
+ - start_days Starts on the nth day.
202
+ ```
203
+
204
+ ---
205
+
206
  ### 其余部分(todo)
207
  整理后上传
208
 
209
  ---
210
 
211
  ## 🐾Communicate
212
+ [donate as u like](https://www.buymeacoffee.com/hysn2001m)
213
+
214
  探讨自动化上传、自动制作视频
215
  |![Nas](media/mp.jpg)|![赞赏](media/QR.png)|
216
  |:-:|:-:|
examples/get_tk_cookie.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from pathlib import Path
3
+
4
+ from conf import BASE_DIR
5
+ from tk_uploader.main import tiktok_setup
6
+
7
+ if __name__ == '__main__':
8
+ account_file = Path(BASE_DIR / "tk_uploader" / "account.json")
9
+ cookie_setup = asyncio.run(tiktok_setup(str(account_file), handle=True))
examples/upload_video_to_tiktok.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+
9
+ if __name__ == '__main__':
10
+ filepath = Path(BASE_DIR) / "videos"
11
+ account_file = Path(BASE_DIR / "tk_uploader" / "account.json")
12
+ folder_path = Path(filepath)
13
+ # get video files from folder
14
+ files = list(folder_path.glob("*.mp4"))
15
+ file_num = len(files)
16
+ publish_datetimes = generate_schedule_time_next_day(file_num, 1, daily_times=[16])
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
+ app = TiktokVideo(title, file, tags, publish_datetimes[index], account_file)
24
+ asyncio.run(app.main(), debug=False)
tk_uploader/main.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ import re
3
+ from datetime import datetime
4
+
5
+ from playwright.async_api import Playwright, async_playwright
6
+ import time
7
+ import os
8
+ import asyncio
9
+ from tk_uploader.tk_config import Tk_Locator
10
+ from utils.files_times import get_absolute_path
11
+
12
+
13
+ async def cookie_auth(account_file):
14
+ async with async_playwright() as playwright:
15
+ browser = await playwright.firefox.launch(headless=True)
16
+ context = await browser.new_context(storage_state=account_file)
17
+ # 创建一个新的页面
18
+ page = await context.new_page()
19
+ # 访问指定的 URL
20
+ await page.goto("https://www.tiktok.com/creator-center/content")
21
+ await page.wait_for_load_state('networkidle')
22
+ try:
23
+ # 选择所有的 select 元素
24
+ select_elements = await page.query_selector_all('select')
25
+ for element in select_elements:
26
+ class_name = await element.get_attribute('class')
27
+ # 使用正则表达式匹配特定模式的 class 名称
28
+ if re.match(r'tiktok-.*-SelectFormContainer.*', class_name):
29
+ print("[+] cookie expired")
30
+ return False
31
+ print("[+] cookie valid")
32
+ return True
33
+ except:
34
+ print("[+] cookie valid")
35
+ return True
36
+
37
+
38
+ async def tiktok_setup(account_file, handle=False):
39
+ account_file = get_absolute_path(account_file, "tk_uploader")
40
+ if not os.path.exists(account_file) or not await cookie_auth(account_file):
41
+ if not handle:
42
+ return False
43
+ print('[+] 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')
44
+ await get_tiktok_cookie(account_file)
45
+ return True
46
+
47
+
48
+ async def get_tiktok_cookie(account_file):
49
+ async with async_playwright() as playwright:
50
+ options = {
51
+ 'args': [
52
+ '--lang en-GB',
53
+ ],
54
+ 'headless': False, # Set headless option here
55
+ }
56
+ # Make sure to run headed.
57
+ browser = await playwright.firefox.launch(**options)
58
+ # Setup context however you like.
59
+ context = await browser.new_context() # Pass any options
60
+ # Pause the page, and start recording manually.
61
+ page = await context.new_page()
62
+ await page.goto("https://www.tiktok.com/creator-center/content")
63
+ await page.pause()
64
+ # 点击调试器的继续,保存cookie
65
+ await context.storage_state(path=account_file)
66
+
67
+
68
+ class TiktokVideo(object):
69
+ def __init__(self, title, file_path, tags, publish_date: datetime, account_file):
70
+ self.title = title
71
+ self.file_path = file_path
72
+ self.tags = tags
73
+ self.publish_date = publish_date
74
+ self.account_file = account_file
75
+
76
+ async def set_schedule_time(self, page, publish_date):
77
+ print("click schedule")
78
+
79
+ await page.frame_locator(Tk_Locator.tk_iframe).locator('div.scheduled-container input').click()
80
+ scheduled_picker = page.frame_locator(Tk_Locator.tk_iframe).locator('div.scheduled-picker')
81
+ await scheduled_picker.locator('div.date-picker-input').click()
82
+
83
+ calendar_month = await page.frame_locator(Tk_Locator.tk_iframe).locator('div.calendar-wrapper span.month-title').inner_text()
84
+
85
+ n_calendar_month = datetime.strptime(calendar_month, '%B').month
86
+
87
+ schedule_month = publish_date.month
88
+
89
+ if n_calendar_month != schedule_month:
90
+ if n_calendar_month < schedule_month:
91
+ arrow = page.frame_locator(Tk_Locator.tk_iframe).locator('div.calendar-wrapper span.arrow').nth(-1)
92
+ else:
93
+ arrow = page.frame_locator(Tk_Locator.tk_iframe).locator('div.calendar-wrapper span.arrow').nth(0)
94
+ await arrow.click()
95
+
96
+ # day set
97
+ valid_days_locator = page.frame_locator(Tk_Locator.tk_iframe).locator(
98
+ 'div.calendar-wrapper span.day.valid')
99
+ valid_days = await valid_days_locator.count()
100
+ for i in range(valid_days):
101
+ day_element = valid_days_locator.nth(i)
102
+ text = await day_element.inner_text()
103
+ if text.strip() == str(publish_date.day):
104
+ await day_element.click()
105
+ break
106
+ # time set
107
+ await page.frame_locator(Tk_Locator.tk_iframe).locator("div.time-picker-input").click()
108
+
109
+ hour_str = publish_date.strftime("%H")
110
+ correct_minute = int(publish_date.minute / 5)
111
+ minute_str = f"{correct_minute:02d}"
112
+
113
+ hour_selector = f"span.tiktok-timepicker-left:has-text('{hour_str}')"
114
+ minute_selector = f"span.tiktok-timepicker-right:has-text('{minute_str}')"
115
+
116
+ # pick hour first
117
+ await page.frame_locator(Tk_Locator.tk_iframe).locator(hour_selector).click()
118
+ # pick minutes after
119
+ await page.frame_locator(Tk_Locator.tk_iframe).locator(minute_selector).click()
120
+
121
+ # click title to remove the focus.
122
+ await page.frame_locator(Tk_Locator.tk_iframe).locator("h1:has-text('Upload video')").click()
123
+
124
+ async def handle_upload_error(self, page):
125
+ print("video upload error retrying.")
126
+ select_file_button = page.frame_locator(Tk_Locator.tk_iframe).locator('button[aria-label="Select file"]')
127
+ async with page.expect_file_chooser() as fc_info:
128
+ await select_file_button.click()
129
+ file_chooser = await fc_info.value
130
+ await file_chooser.set_files(self.file_path)
131
+
132
+ async def upload(self, playwright: Playwright) -> None:
133
+ browser = await playwright.firefox.launch(headless=False)
134
+ context = await browser.new_context(storage_state=f"{self.account_file}")
135
+
136
+ page = await context.new_page()
137
+
138
+ await page.goto("https://www.tiktok.com/creator-center/upload")
139
+ print('[+]Uploading-------{}.mp4'.format(self.title))
140
+
141
+ await page.wait_for_url("https://www.tiktok.com/creator-center/upload")
142
+ await page.wait_for_selector('iframe[data-tt="Upload_index_iframe"]')
143
+ upload_button = page.frame_locator(Tk_Locator.tk_iframe).locator(
144
+ 'button:has-text("Select file"):visible')
145
+
146
+ async with page.expect_file_chooser() as fc_info:
147
+ await upload_button.click()
148
+ file_chooser = await fc_info.value
149
+ await file_chooser.set_files(self.file_path)
150
+
151
+ await self.add_title_tags(page)
152
+ # detact upload status
153
+ await self.detact_upload_status(page)
154
+ if self.publish_date != 0:
155
+ await self.set_schedule_time(page, self.publish_date)
156
+
157
+ await self.click_publish(page)
158
+
159
+ await context.storage_state(path=f"{self.account_file}") # save cookie
160
+ print(' [-] update cookie!')
161
+ await asyncio.sleep(2) # close delay for look the video status
162
+ # close all
163
+ await context.close()
164
+ await browser.close()
165
+
166
+ async def add_title_tags(self, page):
167
+
168
+ await page.frame_locator(Tk_Locator.tk_iframe).locator(
169
+ 'div.public-DraftEditor-content').click()
170
+ time.sleep(2)
171
+ await page.keyboard.press("Control+KeyA")
172
+ time.sleep(2)
173
+ await page.keyboard.press("Delete")
174
+
175
+ # title part
176
+ await page.keyboard.type(self.title)
177
+ await page.keyboard.press("Enter")
178
+
179
+ # tag part
180
+ for index, tag in enumerate(self.tags, start=1):
181
+ print("Setting the %s tag" % index)
182
+ await page.keyboard.type("#" + tag, delay=20)
183
+ await asyncio.sleep(1)
184
+ await page.keyboard.press("Space")
185
+ # if await page.frame_locator(Tk_Locator.tk_iframe).locator('div.mentionSuggestions').count():
186
+ # await page.frame_locator(Tk_Locator.tk_iframe).locator('div.mentionSuggestions- > div').nth(0).click()
187
+
188
+ print(f"success add hashtag: {len(self.tags)}")
189
+
190
+ async def click_publish(self, page):
191
+ while True:
192
+ try:
193
+ publish_button = page.frame_locator(Tk_Locator.tk_iframe).locator('div.btn-post')
194
+ if await publish_button.count():
195
+ await publish_button.click()
196
+
197
+ await page.frame_locator(Tk_Locator.tk_iframe).locator("div.uploaded-modal:visible").wait_for(state="visible", timeout=1500)
198
+ print(" [-] video published success")
199
+ break
200
+ except Exception as e:
201
+ if await page.frame_locator(Tk_Locator.tk_iframe).locator("div.uploaded-modal:visible").count():
202
+ print(" [-]video published success")
203
+ break
204
+ else:
205
+ print(f" [-] Exception: {e}")
206
+ print(" [-] video publishing")
207
+ await page.screenshot(full_page=True)
208
+ await asyncio.sleep(0.5)
209
+
210
+ async def detact_upload_status(self, page):
211
+ while True:
212
+ try:
213
+ if await page.frame_locator(Tk_Locator.tk_iframe).locator('div.btn-post > button').get_attribute("disabled") is None:
214
+ print(" [-]video uploaded.")
215
+ break
216
+ else:
217
+ print(" [-] video uploading...")
218
+ await asyncio.sleep(2)
219
+ if await page.frame_locator(Tk_Locator.tk_iframe).locator('button[aria-label="Select file"]').count():
220
+ print(" [-] found some error while uploading now retry...")
221
+ await self.handle_upload_error(page)
222
+ except:
223
+ print(" [-] video uploading...")
224
+ await asyncio.sleep(2)
225
+
226
+ async def main(self):
227
+ async with async_playwright() as playwright:
228
+ await self.upload(playwright)
229
+
tk_uploader/tk_config.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+
2
+ class Tk_Locator(object):
3
+ tk_iframe = '[data-tt="Upload_index_iframe"]'