/usr/lib/python3/dist-packages/pafy/playlist.py is in python3-pafy 0.5.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import sys
import re
import json
if sys.version_info[:2] >= (3, 0):
# pylint: disable=E0611,F0401,I0011
from urllib.parse import parse_qs, urlparse
else:
from urlparse import parse_qs, urlparse
from . import g
from .pafy import new, get_categoryname, call_gdata, fetch_decode
def extract_playlist_id(playlist_url):
# Normal playlists start with PL, Mixes start with RD + first video ID
idregx = re.compile(r'((?:RD|PL|LL)[-_0-9a-zA-Z]+)$')
playlist_id = None
if idregx.match(playlist_url):
playlist_id = playlist_url # ID of video
if '://' not in playlist_url:
playlist_url = '//' + playlist_url
parsedurl = urlparse(playlist_url)
if parsedurl.netloc in ('youtube.com', 'www.youtube.com'):
query = parse_qs(parsedurl.query)
if 'list' in query and idregx.match(query['list'][0]):
playlist_id = query['list'][0]
return playlist_id
def get_playlist(playlist_url, basic=False, gdata=False,
size=False, callback=lambda x: None):
""" Return a dict containing Pafy objects from a YouTube Playlist.
The returned Pafy objects are initialised using the arguments to
get_playlist() in the manner documented for pafy.new()
"""
playlist_id = extract_playlist_id(playlist_url)
if not playlist_id:
err = "Unrecognized playlist url: %s"
raise ValueError(err % playlist_url)
url = g.urls["playlist"] % playlist_id
allinfo = fetch_decode(url) # unicode
allinfo = json.loads(allinfo)
# playlist specific metadata
playlist = dict(
playlist_id=playlist_id,
likes=allinfo.get('likes'),
title=allinfo.get('title'),
author=allinfo.get('author'),
dislikes=allinfo.get('dislikes'),
description=allinfo.get('description'),
items=[]
)
# playlist items specific metadata
for v in allinfo['video']:
vid_data = dict(
added=v.get('added'),
is_cc=v.get('is_cc'),
is_hd=v.get('is_hd'),
likes=v.get('likes'),
title=v.get('title'),
views=v.get('views'),
rating=v.get('rating'),
author=v.get('author'),
user_id=v.get('user_id'),
privacy=v.get('privacy'),
start=v.get('start', 0.0),
dislikes=v.get('dislikes'),
duration=v.get('duration'),
comments=v.get('comments'),
keywords=v.get('keywords'),
thumbnail=v.get('thumbnail'),
cc_license=v.get('cc_license'),
category_id=v.get('category_id'),
description=v.get('description'),
encrypted_id=v.get('encrypted_id'),
time_created=v.get('time_created'),
time_updated=v.get('time_updated'),
length_seconds=v.get('length_seconds'),
end=v.get('end', v.get('length_seconds'))
)
try:
pafy_obj = new(vid_data['encrypted_id'],
basic=basic,
gdata=gdata,
size=size,
callback=callback)
except IOError as e:
callback("%s: %s" % (v['title'], e.message))
continue
pafy_obj.populate_from_playlist(vid_data)
playlist['items'].append(dict(pafy=pafy_obj,
playlist_meta=vid_data))
callback("Added video: %s" % v['title'])
return playlist
def parseISO8591(duration):
""" Parse ISO 8591 formated duration """
regex = re.compile(r'PT((\d{1,3})H)?((\d{1,3})M)?((\d{1,2})S)?')
if duration:
duration = regex.findall(duration)
if len(duration) > 0:
_, hours, _, minutes, _, seconds = duration[0]
duration = [seconds, minutes, hours]
duration = [int(v) if len(v) > 0 else 0 for v in duration]
duration = sum([60**p*v for p, v in enumerate(duration)])
else:
duration = 30
else:
duration = 30
return duration
class Playlist(object):
_items = None
def __init__(self, playlist_url, basic, gdata, size, callback):
playlist_id = extract_playlist_id(playlist_url)
if not playlist_id:
err = "Unrecognized playlist url: %s"
raise ValueError(err % playlist_url)
query = {'part': 'snippet, contentDetails',
'id': playlist_id}
allinfo = call_gdata('playlists', query)
pl = allinfo['items'][0]
self.plid = playlist_id
self.title = pl['snippet']['title']
self.author = pl['snippet']['channelTitle']
self.description = pl['snippet']['description']
self ._len = pl['contentDetails']['itemCount']
self._basic = basic
self._gdata = gdata
self._size = size
self._callback = callback
def __len__(self):
return self._len
def __iter__(self):
if self._items is not None:
for i in self._items:
yield i
return
items = []
# playlist items specific metadata
query = {'part': 'snippet',
'maxResults': 50,
'playlistId': self.plid}
while True:
playlistitems = call_gdata('playlistItems', query)
query2 = {'part':'contentDetails,snippet,statistics',
'maxResults': 50,
'id': ','.join(i['snippet']['resourceId']['videoId']
for i in playlistitems['items'])}
wdata = call_gdata('videos', query2)
for v, vextra in zip(playlistitems['items'], wdata['items']):
vid_data = dict(
title=v['snippet']['title'],
author=v['snippet']['channelTitle'],
thumbnail=v['snippet'].get('thumbnails', {}
).get('default', {}).get('url'),
description=v['snippet']['description'],
length_seconds=parseISO8591(
vextra['contentDetails']['duration']),
category=get_categoryname(vextra['snippet']['categoryId']),
views=vextra['statistics'].get('viewCount',0),
likes=vextra['statistics'].get('likeCount',0),
dislikes=vextra['statistics'].get('dislikeCount',0),
comments=vextra['statistics'].get('commentCount',0),
)
try:
pafy_obj = new(v['snippet']['resourceId']['videoId'],
basic=self._basic, gdata=self._gdata,
size=self._size, callback=self._callback)
except IOError as e:
self.callback("%s: %s" % (v['title'], e.message))
continue
pafy_obj.populate_from_playlist(vid_data)
items.append(pafy_obj)
self._callback("Added video: %s" % vid_data['title'])
yield pafy_obj
if not playlistitems.get('nextPageToken'):
break
query['pageToken'] = playlistitems['nextPageToken']
self._items = items
def get_playlist2(playlist_url, basic=False, gdata=False,
size=False, callback=lambda x: None):
""" Return a Playlist object from a YouTube Playlist.
The returned Pafy objects are initialised using the arguments to
get_playlist() in the manner documented for pafy.new()
"""
return Playlist(playlist_url, basic, gdata, size, callback)
|