-
Notifications
You must be signed in to change notification settings - Fork 2
/
reuploader.py
239 lines (183 loc) · 7.51 KB
/
reuploader.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import json
import re
import time
import os.path
from pytube import Channel
from playwright.sync_api import sync_playwright
from downloader import get_output_dir
did_upload = False
class FileNotFoundException(Exception):
"Raised when a file needed for reuploading is not found."
pass
def wait_for(seconds):
start = time.time()
while time.time() - start < seconds:
pass
def write_file(name, contents):
insert_contents = str(contents).encode('utf-8', 'ignore')
file = open(name, 'w', encoding='utf-8')
file.write(str(insert_contents))
file.close()
def read_file(name):
file = open(name, 'r', encoding='utf-8')
contents = file.read()
file.close()
return contents
def read_alternates(_dict, first, second):
try: return _dict[first]
except KeyError:
try: return _dict[second]
except: print('Invalid keys found in dictionary')
def parse_json(file):
file = open(file, 'r', encoding='utf-8')
contents = file.read()
file.close()
json_data = json.loads(contents)
link = read_alternates(json_data, 'link', 'channel')
username = read_alternates(json_data, 'username', 'user')
password = read_alternates(json_data, 'password', 'pass')
return link, username, password
def get_page_link(channel_link):
if re.match('.*(?:www.youtube.com)\\/channel\\/.+', channel_link):
return re.sub('www', 'studio', channel_link)
else: return 'https://studio.youtube.com/channel/' + channel_link
def youtube_login(page, link, username, password):
page.goto(link)
# Login with username
page.wait_for_selector('input[type="email"]')
page.type('input[type="email"]', username)
page.click('#identifierNext')
# Login with password
page.wait_for_selector('input[type="password"]')
page.type('input[type="password"]', password)
page.click('#passwordNext')
page.wait_for_selector('text=Your channel')
def file_chooser_fn(page, element, filepath):
with page.expect_file_chooser() as fc_prom:
page.wait_for_selector(element)
page.click(element)
file_chooser = fc_prom.value
file_chooser.set_files(filepath)
def close_secondary_dialog(page):
if page.locator('ytcp-button[id="close-button"]').count() == 1:
page.click('ytcp-button[id="close-button"]')
# Specific options
elif page.locator('#close-button.ytcp-uploads-still-processing-dialog').count():
page.click('#close-button.ytcp-uploads-still-processing-dialog')
elif page.locator('#close-button.ytcp-video-share-dialog').count():
page.click('#close-button.ytcp-video-share-dialog')
else:
print("WARNING: Secondary dialog box could not be closed, last upload may fail")
page.reload()
def except_upload_limit_reached(page):
error_element = page.locator('#error-message.error-details.ytcp-uploads-dialog')
if error_element.is_visible() or error_element.is_enabled():
raise Exception('Daily upload limit reached.')
def add_screenshot(page, name):
filepath = 'screenshots/' + name + '.png'
page.screenshot(path=filepath)
def check_filepath(filepath):
if not os.path.exists(filepath):
folder = os.path.dirname(filepath)
filename = os.path.split(filepath)[1]
print(f'WARNING: Could not locate "{filename}" in {folder}. Skipping.')
raise FileNotFoundException
def upload_from_folder(page, folder, time=0):
# Check if video has been uploaded
if os.path.exists(folder + '/uploaded') or os.path.exists(folder + '/noupload'): return
# Get files
video_file = folder + '/vid.mp4'
thumbnail_file = folder + '/thumb.png'
title_file = folder + '/title.txt'
# Check path validity
check_filepath(video_file)
check_filepath(thumbnail_file)
check_filepath(title_file)
title = read_file(title_file)
description = read_file(folder + '/desc.txt')
visibility = 'PUBLIC'
if not os.path.exists(video_file):
print(f'ERROR: Could not find required file for {folder}')
with open(folder + '/noupload', 'w') as f: f.close()
return
print(f'{title}')
# Open upload menu
page.wait_for_selector('ytcp-button[id="create-icon"]')
page.click('ytcp-button[id="create-icon"]')
page.wait_for_selector('tp-yt-paper-item[id="text-item-0"]')
page.click('tp-yt-paper-item[id="text-item-0"]')
# Upload video
file_chooser_fn(page, 'ytcp-button[id="select-files-button"]', video_file)
page.wait_for_selector('h1[class="style-scope ytcp-uploads-dialog"]')
add_screenshot(page, 'upload_page')
#except_upload_limit_reached(page)
# Set upload
global did_upload
did_upload = True
# Add title and description
text_inputs = page.locator('#input')
if text_inputs.first.locator('div[id="textbox"]').count():
text_inputs.first.locator('div[id="textbox"]').fill(title)
else: text_inputs.all()[1].locator('div[id="textbox"]').fill(title)
text_inputs.last.locator('div[id="textbox"]').fill(description)
# Add thumbnail
if os.path.exists(thumbnail_file):
with page.expect_file_chooser() as file_chooser_promise:
page.locator('.remove-default-style.style-scope.ytcp-thumbnails-compact-editor-uploader-old').click()
file_chooser = file_chooser_promise.value
file_chooser.set_files(thumbnail_file)
else:
print(f'WARNING: Thumbnail for {title} could not be found.')
# Mark as not for kids
page.click('tp-yt-paper-radio-button[name="VIDEO_MADE_FOR_KIDS_NOT_MFK"]')
page.wait_for_selector('#step-badge-3')
page.click('#step-badge-3')
# Set visibility
page.wait_for_selector(f'tp-yt-paper-radio-button[name="{visibility}"]')
page.click(f'tp-yt-paper-radio-button[name="{visibility}"]')
# Close page
page.click('#done-button.ytcp-uploads-dialog')
wait_for(4)
close_secondary_dialog(page)
with open(folder + '/uploaded', 'w') as f: f.close()
def get_folders():
# Get valid folders
folder_path = get_output_dir()
subfolders = [str(f.path) for f in os.scandir(folder_path) if f.is_dir()]
filtered = list(
filter(lambda _dir: not (os.path.exists(_dir + '/uploaded') or os.path.exists(_dir + '/noupload')), subfolders)
)
return filtered
def get_folder_lists():
filtered = get_folders()
# Chunk lists
chunked = []
for i in range(0, len(filtered), 3):
chunked.append(filtered[i : i + 3])
return chunked
def wait_if_uploaded():
global did_upload
if did_upload:
input('Waiting...')
def upload_to_channel(file='secrets.json'):
(link, username, password) = parse_json(file)
studio_page = get_page_link(link)
video_folders = get_folders()
c = Channel(link)
print(f'Posting to "{c.channel_name}"')
try:
with sync_playwright() as p:
browser_type = p.firefox
browser = browser_type.launch()
page = browser.new_page()
youtube_login(page, studio_page, username, password)
for folder in video_folders:
try: upload_from_folder(page, folder)
except FileNotFoundException: pass
print('Done.')
wait_if_uploaded()
page.close(run_before_unload=True)
browser.close()
except Exception as e:
print(f'ERROR: {e}')
wait_if_uploaded()