-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlbry_batch_upload.py
414 lines (323 loc) · 13.3 KB
/
lbry_batch_upload.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import os
import time
import toml
import unidecode
import pyautogui
import pyperclip
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementClickInterceptedException
class Config:
def __init__(self, path):
self.parse_config(path)
def _parse_user_fields(self, user_config):
self.email = user_config['email']
self.password = user_config['password']
def _parse_files_fields(self, files_config):
self.folder_path = files_config['folder_path']
self.audio_format = files_config['audio_format'] # FIXME: check dot
self.thumbnail_format = files_config['thumbnail_format'] # FIXME: check dot
self.excluded = files_config['excluded']
self.album_name = os.path.split(self.folder_path)[-1]
self.thumbnail_name = f'{self.album_name}{self.thumbnail_format}'
def _parse_upload_fields(self, upload_config):
self.description = upload_config['description']
self.tags = upload_config['tags']
self.channel = upload_config['channel']
self.deposit = upload_config['deposit']
self.price = upload_config['price']
self.language = upload_config['language']
self.license_type = upload_config['license_type']
self.license_notice = upload_config['license_notice']
def parse_config(self, path):
with open(path, 'r') as f:
config = toml.load(f)
self._parse_user_fields(config['user'])
self._parse_files_fields(config['files'])
self._parse_upload_fields(config['upload'])
class BasePage:
def __init__(self, driver, config):
self.driver = driver
self.config = config
def _banner(self):
return self.driver.find_element_by_css_selector('button.nag__button')
def close_banners(self):
try:
self._banner().click()
self._banner().click()
except NoSuchElementException:
pass
def scroll_page_to_top(self):
self.driver.execute_script('window.scrollTo(0, 0)')
def scroll_page_to_bottom(self):
self.driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
class LoginPage(BasePage):
def _email_field(self):
return self.driver.find_element_by_css_selector('#username')
def _password_field(self):
return self.driver.find_element_by_css_selector('#password')
def _signin_button(self):
return self.driver.find_element_by_css_selector('[aria-label="Log In"]')
def _continue_button(self):
return self.driver.find_element_by_css_selector('[aria-label="Continue"]')
def load_page(self):
self.driver.get('https://odysee.com/$/signin')
def login(self):
self._email_field().send_keys(self.config.email)
self._signin_button().click()
time.sleep(0.5)
self._password_field().send_keys(self.config.password)
self._continue_button().click()
def close_success_popup(func):
def wrapper(self, *args, **kwargs):
try:
func(self, *args, **kwargs)
except ElementClickInterceptedException:
self.driver.find_element_by_css_selector('[aria-label="Close"]').click()
self.scroll_page_to_top()
func(self, *args, **kwargs)
return wrapper
class UploadPage(BasePage):
def _file_button(self):
return self.driver.find_element_by_xpath(
'//*[@id="app"]/div/div[1]/main/div/section[1]/div[2]/'
'fieldset-section[2]/input-submit/button'
)
def _title(self):
return self.driver.find_element_by_css_selector('#content_title')
def _description(self):
return self.driver.find_element_by_css_selector('#content_description')
def _thumbnail_button(self):
return self.driver.find_element_by_xpath(
'//*[@id="app"]/div/div[1]/main/div/div[2]/'
'section[2]/div/div/div/div[2]/div/button'
)
def _thumbnail_browse_button(self):
return self.driver.find_element_by_xpath(
'//*[@id="app"]/div/div[1]/main/div/div[2]/'
'section[2]/div/div/fieldset-section/input-submit/button'
)
def _thumbnail_upload_button(self):
return self.driver.find_element_by_xpath(
'/html/body/div[4]/div/div/div/button[1]'
)
def _tags(self):
return self.driver.find_element_by_css_selector('.tag__input')
def _channel_list(self):
return self.driver.find_element_by_xpath(
'/html/body/div/div/div[1]/main/div/div[1]/button'
)
def _channel_item(self, channel):
return self.driver.find_element_by_xpath(f'//div[@data-valuetext="{channel}"]')
def _claim_url(self):
return self.driver.find_element_by_css_selector('#content_name')
def _deposit(self):
return self.driver.find_element_by_css_selector('#content_bid')
def _price_button(self):
return self.driver.find_element_by_css_selector('label[for="content_cost"]')
def _price(self):
return self.driver.find_element_by_css_selector('#content_cost_amount_amount')
def _options_button(self):
return self.driver.find_element_by_xpath(
'//*[@id="app"]/div/div[1]/main/div/div[2]/section[6]/div/div/button'
)
def _language_list(self):
return self.driver.find_element_by_css_selector('#content_language')
def _license_list(self):
return self.driver.find_element_by_css_selector('#license')
def _copyright_notice(self):
return self.driver.find_element_by_css_selector('#copyright-notice')
def _publish_button(self):
return self.driver.find_element_by_xpath(
'//*[@id="app"]/div/div[1]/main/div/section[2]/div/button[1]'
)
def _skip_preview_checkbox(self):
return self.driver.find_element_by_xpath(
'/html/body/div[4]/div/div/form/section/div[3]/div[2]/label'
)
def _upload_button(self):
return self.driver.find_element_by_xpath(
'/html/body/div[4]/div/div/form/section/div[3]/div[1]/button[1]'
)
def _publish_next_button(self):
return self.driver.find_element_by_css_selector('a[href="/$/upload"]')
def load_page(self):
self.driver.get('https://odysee.com/$/publish')
def choose_file(self, song_name): # NOTE: designed for nautilus
self._file_button().click()
pyperclip.copy(os.path.join(self.config.folder_path, song_name))
time.sleep(1)
pyautogui.hotkey('divide')
pyautogui.press('backspace')
pyautogui.hotkey('ctrl', 'v')
time.sleep(0.5)
pyautogui.press('enter')
time.sleep(1)
def fill_title(self, upload_title):
self._title().send_keys(upload_title)
def fill_description(self):
self._description().send_keys(self.config.description)
def choose_thumbnail(self, thumbnail_name): # NOTE: designed for nautilus
path = os.path.join(self.config.folder_path, thumbnail_name)
if os.path.isfile(path):
self._thumbnail_button().click()
self._thumbnail_browse_button().click()
pyperclip.copy(path)
time.sleep(1)
pyautogui.hotkey('divide')
pyautogui.press('backspace')
pyautogui.hotkey('ctrl', 'v')
time.sleep(0.5)
pyautogui.press('enter')
time.sleep(1)
self._thumbnail_upload_button().click()
def fill_tags(self):
self._tags().send_keys(','.join(self.config.tags), Keys.ENTER)
def select_channel(self):
self._channel_list().click()
time.sleep(0.5)
self._channel_item(self.config.channel).click()
def fill_claim_name(self, claim_name):
claim = self._claim_url()
claim.clear()
time.sleep(0.5)
claim.clear()
claim.send_keys(claim_name)
def fill_deposit(self):
content_bid = self._deposit()
content_bid.clear()
content_bid.send_keys(str(self.config.deposit))
def select_price(self):
self._price_button().click()
def fill_price(self):
price = self._price()
price.clear()
price.send_keys(str(self.config.price))
def open_additional_options(self):
self._options_button().click()
def select_language(self):
language_list_element = self._language_list()
available_languages = [
element.get_attribute('value')
for element in language_list_element.find_elements_by_tag_name('option')
]
if self.config.language not in available_languages:
raise ValueError(
'Language {!r} is not a valid option -> {}'.format(
self.config.language, available_languages
)
)
Select(language_list_element).select_by_value(self.config.language)
def select_license(self):
license_list_element = self._license_list()
available_licenses = [
element.get_attribute('value')
for element in license_list_element.find_elements_by_tag_name('option')
]
if self.config.license_type not in available_licenses:
raise ValueError(
'License {!r} is not a valid option -> {}'.format(
self.config.license_type, available_licenses
)
)
Select(license_list_element).select_by_value(self.config.license_type)
def fill_license(self):
copyright_notice = self._copyright_notice()
copyright_notice.clear()
copyright_notice.send_keys(self.config.license_notice)
def publish(self):
self._publish_button().click()
def confirm_upload(self):
self._skip_preview_checkbox().click()
self._upload_button().click()
@close_success_popup
def continue_publishing(self):
self._publish_next_button().click()
@close_success_popup
def upload_song(self, song_data, first_song):
self.select_channel()
self.choose_file(song_data['song_name'])
self.fill_claim_name(song_data['claim_name'])
self.fill_title(song_data['upload_title'])
self.fill_description()
self.choose_thumbnail(self.config.thumbnail_name)
self.fill_tags()
if self.config.deposit:
self.fill_deposit()
if self.config.price:
self.select_price()
self.fill_price()
if first_song:
self.open_additional_options()
self.select_language()
self.select_license()
# FIXME: do not hardcode values
# TODO: handle `license_type == 'other'`
if self.config.license_type == 'copyright':
self.fill_license()
self.scroll_page_to_bottom()
self.publish()
if first_song:
self.confirm_upload()
time.sleep(5) # NOTE: so they finish uploading in order
def get_song_names(folder_path, audio_format, excluded): # TODO: revisit conditions
song_names = []
for file in os.listdir(folder_path):
if os.path.splitext(file)[-1] == audio_format:
if file not in excluded:
song_names.append(file)
song_names.sort()
return song_names
def get_sanitized_claim_name(song_name):
sanitized_name = ''
for char in os.path.splitext(song_name)[0]:
if char.isalpha():
sanitized_name += char
elif char == ' ':
if sanitized_name and sanitized_name[-1] != char:
sanitized_name += char
return sanitized_name.lower().split(' - ', 1)[-1]
def get_song_data(folder_path, song_name): # TODO: move to `upload_page`
path_head, album = os.path.split(folder_path)
_, artist = os.path.split(path_head)
title = os.path.splitext(song_name)[0]
# FIXME: `upload_title` can contain multiple `-`
upload_title = f'{title} - {album} - {artist}'
claim_name = unidecode.unidecode(
os.path.splitext(get_sanitized_claim_name(song_name))[0].replace(' ', '-')
)
return {
'album': album,
'artist': artist,
'song_name': song_name,
'upload_title': upload_title,
'claim_name': claim_name,
}
if __name__ == '__main__':
driver = webdriver.Chrome()
driver.implicitly_wait(5)
config = Config('config.toml')
login_page = LoginPage(driver, config)
login_page.load_page()
login_page.login()
login_page.close_banners()
upload_page = UploadPage(driver, config)
upload_page.load_page()
songs = get_song_names(config.folder_path, config.audio_format, config.excluded)
song_count = len(songs)
while len(songs) > 0:
# From last to first so they are ordered on the feed
song_name = songs.pop()
song_data = get_song_data(config.folder_path, song_name)
# Click "Additional Options" only on the first publish
first_song = False
if len(songs) + 1 == song_count:
first_song = True
upload_page.upload_song(song_data, first_song=first_song)
# Inform the user, useful for excluding songs if the script crashes
print('[PUBLISHED]', song_name)
# Keep the upload page on sight when uploading the last file
if not len(songs) == 0:
upload_page.continue_publishing()