-
Notifications
You must be signed in to change notification settings - Fork 7
/
list_youtuble_playlist.py
executable file
·63 lines (52 loc) · 1.64 KB
/
list_youtuble_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
#! /usr/bin/python -u
"""
Just a simple script to retrieve playlists on YouTube as print as json.
Useful to gather from another program and create info to post on Twitter.
"""
import sys
import re
import curl # install python-pycurl
import bs4 # install python-bs4
import simplejson # install python-simplejson
YOUTUBE = "https://www.youtube.com/playlist?list="
VIDEO = "https://www.youtube.com/watch?v="
def help(errno=None):
print "Use: %s <playlist id>" % sys.argv[0]
if errno:
sys.exit(errno)
sys.exit(1)
def retrieve_html(url):
c = curl.Curl()
c.get(url)
return c.body()
def main(url):
if url is None:
help()
playlist_id = url
# clean from full url alike
# https://www.youtube.com/playlist?list=PLQYPYhKQVTvcNqNnkEfEKhaMxUf2tn_CP
playlist_id = re.sub(".*=", "", playlist_id)
VIDEOS = {}
body = retrieve_html("%s%s" % (YOUTUBE, playlist_id))
#print body
soup = bs4.BeautifulSoup(body, 'html.parser')
table_trs = soup.findAll('tr')
for tr in table_trs:
title = tr['data-title']
VIDEOS[title] = {}
video_url = tr['data-video-id']
VIDEOS[title]['url'] = "%s%s" % (VIDEO, video_url)
tr_tds = tr.findAll('td')
for td in tr_tds:
#print " * * ", td['class']
if td['class'][0] == "pl-video-thumbnail":
thumb = td.img['data-thumb']
VIDEOS[title]['thumb'] = thumb
return VIDEOS
if __name__ == '__main__':
try:
link = sys.argv[1]
except:
help(errno=1)
videos = main(link)
print simplejson.dumps(videos, sort_keys=True, indent=' ')