-
Notifications
You must be signed in to change notification settings - Fork 0
/
ytplaylist.py
executable file
·67 lines (54 loc) · 2.02 KB
/
ytplaylist.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
#!/bin/env python
# WARNING: THIS SCRIPT MAY VIOLATE YOUTUBE'S TOS AND IS AN OPTIONAL, AT YOUR OWN RISK MODULE FOR THE ORIGINAL PROGRAM. BY USING THIS CODE YOU ARE STILL SUBJECT TO THE TERMS OF THE GPL v2.0 Source Code License
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
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, StaleElementReferenceException
from urllib.request import Request, urlopen
from urllib import error
# LOCAL
#from Downloader import Downloader
import requests
import sys
import subprocess
import argparse
DRIVER_WAIT_TIME = 10
class YTPlaylist:
@staticmethod
def __getWebElem(driver, xpath, wait=DRIVER_WAIT_TIME):
try:
element = WebDriverWait(driver, DRIVER_WAIT_TIME).until(EC.presence_of_element_located((By.XPATH, xpath)))
return element
except TimeoutException:
print("ERROR: Timeout Exception")
except StaleElementReferenceException:
print("ERROR: StaleElementReferenceException")
return False
@staticmethod
def fetchPlaylist(playlistID):
link = "https://www.youtube.com/playlist?list=" + playlistID
songs = None
driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get(link)
elemExists = YTPlaylist.__getWebElem(driver, "//tr[contains(@class,'pl-video')]")
elements = driver.find_elements_by_xpath("//tr[contains(@class,'pl-video')]")
if not elemExists:
print("No pl-video on page")
driver.save_screenshot('screen.png')
if elements:
songs = dict()
songs['items'] = list()
for item in elements:
snip = dict()
snip['snippet'] = dict()
snip['snippet']['title'] = item.get_attribute('data-title')
snip['snippet']['resourceId'] = dict()
snip['snippet']['resourceId']['videoId'] = item.get_attribute('data-video-id')
songs['items'].append(snip)
else:
print("No Element")
driver.quit()
return songs