-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuploadVideo.py
181 lines (164 loc) · 8.39 KB
/
uploadVideo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import traceback
from autoselenium import AutoSelenium
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# //*[@data-text="true"]
# //div[@class="tiktok-timepicker-option-item"]//span[@class="tiktok-timepicker-option-text tiktok-timepicker-left"]
#
# //span[contains(@class, 'arrow')] #tháng
class UploadVideoTiktok(AutoSelenium):
def __init__(self, driver):
super().__init__()
self.setDriver(driver)
def __checkVideoUpload(self):
while True:
sleep(0.25)
try:
if self.driver.find_element('xpath', '//*[text()="Chỉnh sửa video"]').is_displayed():
return True
except: continue
def __upload(self):
for i in range(10):
self.ClickJsWebElement('xpath', '//*[text()="Đăng" or text()="Lịch biểu"]')
sleep(0.5)
if "Đã thử tải lên quá nhiều lần. Vui lòng thử lại sau." in self.driver.page_source:
return False
elif "Việc bạn rời trang không gây gián đoạn cho quá trình đăng video" in self.driver.page_source or "Video đã được lên lịch!" in self.driver.page_source:
return True
return False
def __checkUploadSuccess(self):
while True:
sleep(0.25)
self.driver.implicitly_wait(0.1)
try:
# self.driver.find_element('xpath', '//*[text()="Tách thành nhiều phần để tăng khả năng hiển thị"]')
self.driver.find_element('xpath', '//*[text()="Để sau"]').click()
except: pass
try:
self.driver.find_elements('xpath', '//*[text()="Tải video khác lên"]')[-1].click()
return True
except: continue
# Tiếp tục đăng video dài hơn 10 phút?
def scheduleUpload(self, args):
self.driver.implicitly_wait(30)
day, hour, minute = args
self.driver.find_element('xpath', '//input[@id="tux-3" and @type="checkbox"]').click()
try:
self.driver.implicitly_wait(1)
self.driver.find_element('xpath', '//*[text()="Cho phép"]').click()
except: pass
self.driver.implicitly_wait(30)
sleep(1)
self.driver.find_element('xpath', '//div[contains(@class, "time-picker-input picker-input")]').click()
# minuteValid = ["00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55"]
# hourValid = ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"]
if len(str(hour)) == 1: hour = "0"+str(hour)
if len(str(minute)) == 1: minute = "0"+str(minute)
print(hour, minute)
el = self.driver.find_element('xpath', '//*[text()="%s" and contains(@class, "left")]'%hour)
self.driver.execute_script('''arguments[0].scrollIntoView({ behavior: 'smooth' });''', el)
self.driver.execute_script('''arguments[0].click();''', el)
el = self.driver.find_element('xpath', '//*[text()="%s" and contains(@class, "right")]'%minute)
self.driver.execute_script('''arguments[0].scrollIntoView({ behavior: 'smooth' });''', el)
self.driver.execute_script('''arguments[0].click();''', el)
sleep(1)
self.driver.find_element('xpath', '//div[contains(@class, "date-picker-input picker-input")]').click()
sleep(1)
self.driver.find_elements('xpath', '//div[contains(@class, "day-span-container")]//span[text()="%s"]'%day)[-1].click()
sleep(1)
if 'Lên lịch trước ít nhất 15 phút' in self.driver.page_source: return False
# sleep(100)
return True
def tagUsername(self, username):
while True:
try:
self.driver.implicitly_wait(0.1)
self.driver.find_element('xpath', '//*[text()="Để sau"]').click()
except: pass
try:
self.driver.implicitly_wait(30)
self.driver.find_element('xpath', '//button[contains(@aria-label, "@")]').click()
break
except: continue
# self.driver.find_element('xpath', '//input[contains(@class, "search-friends")]').send_keys(username.replace("@", ""))
WebDriverWait(self.driver, 30).until(EC.visibility_of_element_located(('xpath', '//input[contains(@class, "search-friends")]'))).send_keys(username.replace("@", ""))
# sleep(5)
# inp.send_keys(Keys.ENTER)
try:
self.driver.implicitly_wait(5)
self.driver.find_element('xpath', '//*[text()="%s"]'%username).click()
except:
self.driver.find_element('xpath', '//input[contains(@class, "search-friends")]').send_keys(Keys.ENTER)
# sleep(1)
def hashTag(self, tag):
while True:
try:
self.driver.implicitly_wait(0.1)
self.driver.find_element('xpath', '//*[text()="Để sau"]').click()
except: pass
try:
self.driver.implicitly_wait(30)
self.driver.find_element('xpath', '//button[@aria-label="Hashtag"]').click()
break
except: continue
# self.driver.find_element('xpath', '//*[@data-text="true"]').send_keys(tag.replace("#", ""))
WebDriverWait(self.driver, 30).until(EC.visibility_of_element_located(('xpath', '//*[@data-text="true"]'))).send_keys(tag.replace("#", ""))
try:
self.driver.implicitly_wait(5)
self.driver.find_element('xpath', '//*[@role="listbox"]')
sleep(1)
except: pass
self.driver.find_element('xpath', '//*[@data-text="true"]').send_keys(Keys.ENTER)
# sleep(1)
def content(self, nd):
sleep(1)
while True:
try:
self.driver.implicitly_wait(0.1)
self.driver.find_element('xpath', '//*[text()="Để sau"]').click()
except: pass
try:
# print(self.driver.find_element('xpath', '//*[@data-contents="true"]').text)
self.driver.find_element('xpath', '//div[@aria-autocomplete="list"]').send_keys(Keys.CONTROL+"a")
self.driver.find_element('xpath', '//div[@aria-autocomplete="list"]').send_keys(Keys.DELETE)
if self.driver.find_element('xpath', '//*[@data-contents="true"]').text == "": break
except: pass
# ActionChains(self.driver).click(self.driver.find_element('xpath', '//*[@data-contents="true"]')).send_keys(Keys.DELETE).perform()
sleep(3)
for n in nd.split(" "):
try:
self.driver.implicitly_wait(0.1)
self.driver.find_element('xpath', '//*[text()="Để sau"]').click()
except: pass
if "#" in n:
self.hashTag(n)
elif "@" in n:
self.tagUsername(n)
else:
WebDriverWait(self.driver, 30).until(EC.visibility_of_element_located(('xpath', '//*[@data-text="true"]'))).send_keys(n+" ")
def uploadVideo(self, pathVideo, content, schedule):
try:
self.driver.get("https://www.tiktok.com/creator-center/upload")
self.driver.implicitly_wait(30)
self.driver.switch_to.frame(self.driver.find_element('xpath', '//iframe[@data-tt="Upload_index_iframe"]'))
# self.driver.implicitly_wait(30)
self.driver.find_element('xpath', '//input[@accept="video/*"]').send_keys(pathVideo)
print(self.__checkVideoUpload())
if content != "":
self.content(content)
sleep(1)
print(schedule)
if schedule:
if not self.scheduleUpload(schedule):return False
sleep(1)
if self.__upload():
return self.__checkUploadSuccess()
else: return False
except:
with open("error.txt", "a+", encoding="utf-8") as file:
file.write(traceback.format_exc()+"\n")
return False
# UploadVideoTiktok(None).content("ngon vl @chi2911ks #xuhuong #dexuat #gai #sexy")