Add auxiliary methods to InfoExtractor to set the '_type' key and use them for some playlist IEs

This commit is contained in:
Jaime Marquínez Ferrándiz 2013-03-05 20:55:48 +01:00
parent 6ac7f082c4
commit 8a38a194fb
2 changed files with 47 additions and 18 deletions

View File

@ -36,31 +36,37 @@ class FakeDownloader(FileDownloader):
return url return url
class TestYoutubeLists(unittest.TestCase): class TestYoutubeLists(unittest.TestCase):
def assertIsPlaylist(self,info):
"""Make sure the info has '_type' set to 'playlist'"""
self.assertEqual(info['_type'], 'playlist')
def test_youtube_playlist(self): def test_youtube_playlist(self):
dl = FakeDownloader() dl = FakeDownloader()
ie = YoutubePlaylistIE(dl) ie = YoutubePlaylistIE(dl)
ie.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re') result = ie.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')[0]
ytie_results = [YoutubeIE()._extract_id(url) for url in dl.result] self.assertIsPlaylist(result)
ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
self.assertEqual(ytie_results, [ 'bV9L5Ht9LgY', 'FXxLjLQi3Fg', 'tU3Bgo5qJZE']) self.assertEqual(ytie_results, [ 'bV9L5Ht9LgY', 'FXxLjLQi3Fg', 'tU3Bgo5qJZE'])
def test_issue_673(self): def test_issue_673(self):
dl = FakeDownloader() dl = FakeDownloader()
ie = YoutubePlaylistIE(dl) ie = YoutubePlaylistIE(dl)
ie.extract('PLBB231211A4F62143') result = ie.extract('PLBB231211A4F62143')[0]
self.assertTrue(len(dl.result) > 40) self.assertTrue(len(result['entries']) > 40)
def test_youtube_playlist_long(self): def test_youtube_playlist_long(self):
dl = FakeDownloader() dl = FakeDownloader()
ie = YoutubePlaylistIE(dl) ie = YoutubePlaylistIE(dl)
ie.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q') result = ie.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')[0]
self.assertTrue(len(dl.result) >= 799) self.assertIsPlaylist(result)
self.assertTrue(len(result['entries']) >= 799)
def test_youtube_playlist_with_deleted(self): def test_youtube_playlist_with_deleted(self):
#651 #651
dl = FakeDownloader() dl = FakeDownloader()
ie = YoutubePlaylistIE(dl) ie = YoutubePlaylistIE(dl)
ie.extract('https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC') result = ie.extract('https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC')[0]
ytie_results = [YoutubeIE()._extract_id(url) for url in dl.result] ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
self.assertFalse('pElCt5oNDuI' in ytie_results) self.assertFalse('pElCt5oNDuI' in ytie_results)
self.assertFalse('KdPEApIVdWM' in ytie_results) self.assertFalse('KdPEApIVdWM' in ytie_results)
@ -68,10 +74,11 @@ class TestYoutubeLists(unittest.TestCase):
dl = FakeDownloader() dl = FakeDownloader()
ie = YoutubePlaylistIE(dl) ie = YoutubePlaylistIE(dl)
# TODO find a > 100 (paginating?) videos course # TODO find a > 100 (paginating?) videos course
ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8') result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')[0]
self.assertEqual(YoutubeIE()._extract_id(dl.result[0]), 'j9WZyLZCBzs') entries = result['entries']
self.assertEqual(len(dl.result), 25) self.assertEqual(YoutubeIE()._extract_id(entries[0]['url']), 'j9WZyLZCBzs')
self.assertEqual(YoutubeIE()._extract_id(dl.result[-1]), 'rYefUsYuEp0') self.assertEqual(len(entries), 25)
self.assertEqual(YoutubeIE()._extract_id(entries[-1]['url']), 'rYefUsYuEp0')
def test_youtube_channel(self): def test_youtube_channel(self):
# I give up, please find a channel that does paginate and test this like test_youtube_playlist_long # I give up, please find a channel that does paginate and test this like test_youtube_playlist_long
@ -80,8 +87,8 @@ class TestYoutubeLists(unittest.TestCase):
def test_youtube_user(self): def test_youtube_user(self):
dl = FakeDownloader() dl = FakeDownloader()
ie = YoutubeUserIE(dl) ie = YoutubeUserIE(dl)
ie.extract('https://www.youtube.com/user/TheLinuxFoundation') result = ie.extract('https://www.youtube.com/user/TheLinuxFoundation')[0]
self.assertTrue(len(dl.result) >= 320) self.assertTrue(len(result['entries']) >= 320)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -128,6 +128,24 @@ class InfoExtractor(object):
urlh = self._request_webpage(url_or_request, video_id, note, errnote) urlh = self._request_webpage(url_or_request, video_id, note, errnote)
webpage_bytes = urlh.read() webpage_bytes = urlh.read()
return webpage_bytes.decode('utf-8', 'replace') return webpage_bytes.decode('utf-8', 'replace')
#Methods for following #608
#They set the correct value of the '_type' key
def video_result(self, video_info):
"""Returns a video"""
video_info['_type'] = 'video'
return video_info
def url_result(self, url, ie=None):
"""Returns a url that points to a page that should be processed"""
#TODO: ie should be the class used for getting the info
video_info = {'_type': 'url',
'url': url}
return video_info
def playlist_result(self, entries):
"""Returns a playlist"""
video_info = {'_type': 'playlist',
'entries': entries}
return video_info
class YoutubeIE(InfoExtractor): class YoutubeIE(InfoExtractor):
@ -1756,7 +1774,8 @@ class YoutubePlaylistIE(InfoExtractor):
else: else:
self._downloader.to_screen(u'[youtube] PL %s: Found %i videos, downloading %i' % (playlist_id, total, len(videos))) self._downloader.to_screen(u'[youtube] PL %s: Found %i videos, downloading %i' % (playlist_id, total, len(videos)))
return self._downloader.extract_info_iterable(videos) url_results = [self.url_result(url) for url in videos]
return [self.playlist_result(url_results)]
class YoutubeChannelIE(InfoExtractor): class YoutubeChannelIE(InfoExtractor):
@ -1807,7 +1826,8 @@ class YoutubeChannelIE(InfoExtractor):
self._downloader.to_screen(u'[youtube] Channel %s: Found %i videos' % (channel_id, len(video_ids))) self._downloader.to_screen(u'[youtube] Channel %s: Found %i videos' % (channel_id, len(video_ids)))
urls = ['http://www.youtube.com/watch?v=%s' % id for id in video_ids] urls = ['http://www.youtube.com/watch?v=%s' % id for id in video_ids]
return self._downloader.extract_info_iterable(urls) url_entries = [self.url_result(url) for url in urls]
return [self.playlist_result(url_entries)]
class YoutubeUserIE(InfoExtractor): class YoutubeUserIE(InfoExtractor):
@ -1890,7 +1910,8 @@ class YoutubeUserIE(InfoExtractor):
(username, all_ids_count, len(video_ids))) (username, all_ids_count, len(video_ids)))
urls = ['http://www.youtube.com/watch?v=%s' % video_id for video_id in video_ids] urls = ['http://www.youtube.com/watch?v=%s' % video_id for video_id in video_ids]
return self._downloader.extract_info_iterable(urls) url_results = [self.url_result(url) for url in urls]
return [self.playlist_result(url_results)]
class BlipTVUserIE(InfoExtractor): class BlipTVUserIE(InfoExtractor):
@ -1981,7 +2002,8 @@ class BlipTVUserIE(InfoExtractor):
(self.IE_NAME, username, all_ids_count, len(video_ids))) (self.IE_NAME, username, all_ids_count, len(video_ids)))
urls = [u'http://blip.tv/%s' % video_id for video_id in video_ids] urls = [u'http://blip.tv/%s' % video_id for video_id in video_ids]
return self._downloader.extract_info_iterable(urls) url_entries = [self.url_result(url) for url in urls]
return [self.playlist_result(url_entries)]
class DepositFilesIE(InfoExtractor): class DepositFilesIE(InfoExtractor):