mirror of
https://github.com/l1ving/youtube-dl
synced 2025-02-03 04:02:51 +08:00
Changed implementation of YoutubeUserIE to download all video ids using YouTube Data API.
This commit is contained in:
parent
8cc4434116
commit
549f25758b
40
youtube-dl
40
youtube-dl
@ -1980,7 +1980,9 @@ class YoutubeUserIE(InfoExtractor):
|
||||
|
||||
_VALID_URL = r'(?:http://)?(?:\w+\.)?youtube.com/user/(.*)'
|
||||
_TEMPLATE_URL = 'http://gdata.youtube.com/feeds/api/users/%s'
|
||||
_VIDEO_INDICATOR = r'http://gdata.youtube.com/feeds/api/videos/(.*)' # XXX Fix this.
|
||||
_GDATA_PAGE_SIZE = 50
|
||||
_GDATA_URL = 'http://gdata.youtube.com/feeds/api/users/%s/uploads?max-results=%d&start-index=%d'
|
||||
_VIDEO_INDICATOR = r'/watch\?v=(.+?)&'
|
||||
_youtube_ie = None
|
||||
|
||||
def __init__(self, youtube_ie, downloader=None):
|
||||
@ -1991,9 +1993,9 @@ class YoutubeUserIE(InfoExtractor):
|
||||
def suitable(url):
|
||||
return (re.match(YoutubeUserIE._VALID_URL, url) is not None)
|
||||
|
||||
def report_download_page(self, username):
|
||||
def report_download_page(self, username, start_index):
|
||||
"""Report attempt to download user page."""
|
||||
self._downloader.to_screen(u'[youtube] user %s: Downloading page ' % (username))
|
||||
self._downloader.to_screen(u'[youtube] user %s: Downloading video ids from %d to %d' % (username, start_index, start_index + self._GDATA_PAGE_SIZE))
|
||||
|
||||
def _real_initialize(self):
|
||||
self._youtube_ie.initialize()
|
||||
@ -2005,13 +2007,20 @@ class YoutubeUserIE(InfoExtractor):
|
||||
self._downloader.trouble(u'ERROR: invalid url: %s' % url)
|
||||
return
|
||||
|
||||
# Download user page
|
||||
username = mobj.group(1)
|
||||
video_ids = []
|
||||
pagenum = 1
|
||||
|
||||
self.report_download_page(username)
|
||||
request = urllib2.Request(self._TEMPLATE_URL % (username), None, std_headers)
|
||||
# Download video ids using YouTube Data API. Result size per query is limited (currently to 50 videos) so
|
||||
# we need to query page by page until there are no video ids - it means we got all of them.
|
||||
|
||||
video_ids = []
|
||||
pagenum = 0
|
||||
|
||||
while True:
|
||||
start_index = pagenum * self._GDATA_PAGE_SIZE + 1
|
||||
self.report_download_page(username, start_index)
|
||||
|
||||
request = urllib2.Request(self._GDATA_URL % (username, self._GDATA_PAGE_SIZE, start_index), None, std_headers)
|
||||
|
||||
try:
|
||||
page = urllib2.urlopen(request).read()
|
||||
except (urllib2.URLError, httplib.HTTPException, socket.error), err:
|
||||
@ -2024,15 +2033,18 @@ class YoutubeUserIE(InfoExtractor):
|
||||
for mobj in re.finditer(self._VIDEO_INDICATOR, page):
|
||||
if mobj.group(1) not in ids_in_page:
|
||||
ids_in_page.append(mobj.group(1))
|
||||
|
||||
if len(ids_in_page) == 0:
|
||||
# Got them all, now download!
|
||||
break
|
||||
|
||||
pagenum += 1
|
||||
video_ids.extend(ids_in_page)
|
||||
|
||||
playliststart = self._downloader.params.get('playliststart', 1) - 1
|
||||
playlistend = self._downloader.params.get('playlistend', -1)
|
||||
video_ids = video_ids[playliststart:playlistend]
|
||||
self._downloader.to_screen("[youtube] user %s: Collected %d video ids" % (username, len(video_ids)))
|
||||
|
||||
for id in video_ids:
|
||||
self._youtube_ie.extract('http://www.youtube.com/watch?v=%s' % id)
|
||||
return
|
||||
for video_id in video_ids:
|
||||
self._youtube_ie.extract('http://www.youtube.com/watch?v=%s' % video_id)
|
||||
|
||||
class PostProcessor(object):
|
||||
"""Post Processor class.
|
||||
|
Loading…
Reference in New Issue
Block a user