-
Notifications
You must be signed in to change notification settings - Fork 40
/
youtube-dl-watch-later-playlist.py
131 lines (105 loc) · 4.41 KB
/
youtube-dl-watch-later-playlist.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
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from termcolor import colored
import getpass
import subprocess
download_folder = '~/Downloads/'
def get_google_username():
return input('ᕙ(⇀‸↼‶)ᕗ Enter your google username: ')
def get_google_password():
return getpass.getpass('ᕙ(⇀‸↼‶)ᕗ Enter your google password: ')
def start_browser():
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--incognito")
return webdriver.Chrome(options=chrome_options)
def login_to_google(driver, username, password):
driver.get("https://stackoverflow.com/users/signup")
driver.find_element_by_xpath('//*[@id="openid-buttons"]/button[1]').click()
verify_username(username)
verify_password(password)
# Waits until redirect to stackoverflow.com occurs
wait = WebDriverWait(driver, 300)
wait.until(EC.url_contains("stackoverflow.com"))
print(colored('Login successful.', 'green'))
def verify_username(username):
login_field = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.ID, 'identifierId'))
)
while True:
# Enters username and clicks next button
login_field.send_keys(username)
driver.find_element_by_id('identifierNext').click()
try:
# Checks for invalid username warning
warning = WebDriverWait(driver, 3).until(
EC.visibility_of_element_located(
(By.XPATH, '//*[@id="view_container"]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div/div[2]/div[2]'))
)
if warning:
print(colored('Warning! Google Account not found.', 'red'))
username = get_google_username()
login_field.clear()
continue
except(TimeoutException):
print(colored('Username verified.', 'green'))
break
def verify_password(password):
password_field = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, '//input[@type="password"]'))
)
while True:
# Enters password and clicks next button
password_field.send_keys(password)
driver.find_element_by_id('passwordNext').click()
try:
# Checks for invalid password warning
warning = WebDriverWait(driver, 3).until(
EC.visibility_of_element_located(
(By.XPATH, '//*[@id="view_container"]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[2]'))
)
if warning:
print(colored('Warning! Wrong password.', 'red'))
password = get_google_password()
continue
except(TimeoutException):
print(colored('Password verified.', 'green'))
break
def scrape_watch_later_playlist(driver):
driver.get("https://www.youtube.com/playlist?list=WL")
videos = WebDriverWait(driver, 60).until(
EC.presence_of_all_elements_located((By.XPATH, '//*[@id="content"]/a'))
)
urls = []
for video in videos:
url = video.get_attribute('href')
url = url.split('&list=WL&index=')
url = url[0]
urls.append(url)
return urls
def run_terminal_cmd(cmd):
process = subprocess.run(cmd, shell=True)
if process.returncode != 0:
print(colored('Error running cmd!', 'red'))
return
else:
return process.stdout
if __name__ == '__main__':
print('ᕙ(⇀‸↼‶)ᕗ starting download script for youtube watch later playlist.')
username = get_google_username()
password = get_google_password()
driver = start_browser()
login_to_google(driver, username, password)
videos = scrape_watch_later_playlist(driver)
for video in videos:
prefix = "youtube-dl -i -c "
output = "-o '" + download_folder + "%(title)s.%(ext)s' "
quality = "-f 'bestvideo[height<=1080][ext=mp4]+bestaudio[ext=m4a]' "
cmd = prefix + output + quality + video
run_terminal_cmd(cmd)