[jukebox] call YoutubeIE if necessary
[youtube-dl] / test / test_youtube_lists.py
1 #!/usr/bin/env python
2
3 import sys
4 import unittest
5 import json
6
7 # Allow direct execution
8 import os
9 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
11 from youtube_dl.extractor import YoutubeUserIE, YoutubePlaylistIE, YoutubeIE, YoutubeChannelIE
12 from youtube_dl.utils import *
13 from youtube_dl.FileDownloader import FileDownloader
14
15 PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
16 with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
17     parameters = json.load(pf)
18
19 # General configuration (from __init__, not very elegant...)
20 jar = compat_cookiejar.CookieJar()
21 cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
22 proxy_handler = compat_urllib_request.ProxyHandler()
23 opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
24 compat_urllib_request.install_opener(opener)
25
26 class FakeDownloader(FileDownloader):
27     def __init__(self):
28         self.result = []
29         self.params = parameters
30     def to_screen(self, s):
31         print(s)
32     def trouble(self, s, tb=None):
33         raise Exception(s)
34     def extract_info(self, url):
35         self.result.append(url)
36         return url
37
38 class TestYoutubeLists(unittest.TestCase):
39     def assertIsPlaylist(self,info):
40         """Make sure the info has '_type' set to 'playlist'"""
41         self.assertEqual(info['_type'], 'playlist')
42
43     def test_youtube_playlist(self):
44         dl = FakeDownloader()
45         ie = YoutubePlaylistIE(dl)
46         result = ie.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')[0]
47         self.assertIsPlaylist(result)
48         self.assertEqual(result['title'], 'ytdl test PL')
49         ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
50         self.assertEqual(ytie_results, [ 'bV9L5Ht9LgY', 'FXxLjLQi3Fg', 'tU3Bgo5qJZE'])
51
52     def test_issue_673(self):
53         dl = FakeDownloader()
54         ie = YoutubePlaylistIE(dl)
55         result = ie.extract('PLBB231211A4F62143')[0]
56         self.assertTrue(len(result['entries']) > 25)
57
58     def test_youtube_playlist_long(self):
59         dl = FakeDownloader()
60         ie = YoutubePlaylistIE(dl)
61         result = ie.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')[0]
62         self.assertIsPlaylist(result)
63         self.assertTrue(len(result['entries']) >= 799)
64
65     def test_youtube_playlist_with_deleted(self):
66         #651
67         dl = FakeDownloader()
68         ie = YoutubePlaylistIE(dl)
69         result = ie.extract('https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC')[0]
70         ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
71         self.assertFalse('pElCt5oNDuI' in ytie_results)
72         self.assertFalse('KdPEApIVdWM' in ytie_results)
73         
74     def test_youtube_playlist_empty(self):
75         dl = FakeDownloader()
76         ie = YoutubePlaylistIE(dl)
77         result = ie.extract('https://www.youtube.com/playlist?list=PLtPgu7CB4gbZDA7i_euNxn75ISqxwZPYx')[0]
78         self.assertIsPlaylist(result)
79         self.assertEqual(len(result['entries']), 0)
80
81     def test_youtube_course(self):
82         dl = FakeDownloader()
83         ie = YoutubePlaylistIE(dl)
84         # TODO find a > 100 (paginating?) videos course
85         result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')[0]
86         entries = result['entries']
87         self.assertEqual(YoutubeIE()._extract_id(entries[0]['url']), 'j9WZyLZCBzs')
88         self.assertEqual(len(entries), 25)
89         self.assertEqual(YoutubeIE()._extract_id(entries[-1]['url']), 'rYefUsYuEp0')
90
91     def test_youtube_channel(self):
92         dl = FakeDownloader()
93         ie = YoutubeChannelIE(dl)
94         #test paginated channel
95         result = ie.extract('https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w')[0]
96         self.assertTrue(len(result['entries']) > 90)
97         #test autogenerated channel
98         result = ie.extract('https://www.youtube.com/channel/HCtnHdj3df7iM/videos')[0]
99         self.assertTrue(len(result['entries']) >= 18)
100
101     def test_youtube_user(self):
102         dl = FakeDownloader()
103         ie = YoutubeUserIE(dl)
104         result = ie.extract('https://www.youtube.com/user/TheLinuxFoundation')[0]
105         self.assertTrue(len(result['entries']) >= 320)
106
107     def test_youtube_safe_search(self):
108         dl = FakeDownloader()
109         ie = YoutubePlaylistIE(dl)
110         result = ie.extract('PLtPgu7CB4gbY9oDN3drwC3cMbJggS7dKl')[0]
111         self.assertEqual(len(result['entries']), 2)
112
113 if __name__ == '__main__':
114     unittest.main()