Merge remote-tracking branch 'dstftw/escape-non-ascii-in-urls'
[youtube-dl] / test / test_youtube_lists.py
1 #!/usr/bin/env python
2
3 # Allow direct execution
4 import os
5 import sys
6 import unittest
7 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
9 from test.helper import FakeYDL
10
11
12 from youtube_dl.extractor import (
13     YoutubeUserIE,
14     YoutubePlaylistIE,
15     YoutubeIE,
16     YoutubeChannelIE,
17     YoutubeShowIE,
18     YoutubeTopListIE,
19     YoutubeSearchURLIE,
20 )
21
22
23 class TestYoutubeLists(unittest.TestCase):
24     def assertIsPlaylist(self, info):
25         """Make sure the info has '_type' set to 'playlist'"""
26         self.assertEqual(info['_type'], 'playlist')
27
28     def test_youtube_playlist_noplaylist(self):
29         dl = FakeYDL()
30         dl.params['noplaylist'] = True
31         ie = YoutubePlaylistIE(dl)
32         result = ie.extract('https://www.youtube.com/watch?v=FXxLjLQi3Fg&list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')
33         self.assertEqual(result['_type'], 'url')
34         self.assertEqual(YoutubeIE().extract_id(result['url']), 'FXxLjLQi3Fg')
35     
36     def test_youtube_course(self):
37         dl = FakeYDL()
38         ie = YoutubePlaylistIE(dl)
39         # TODO find a > 100 (paginating?) videos course
40         result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')
41         entries = result['entries']
42         self.assertEqual(YoutubeIE().extract_id(entries[0]['url']), 'j9WZyLZCBzs')
43         self.assertEqual(len(entries), 25)
44         self.assertEqual(YoutubeIE().extract_id(entries[-1]['url']), 'rYefUsYuEp0')
45
46     def test_youtube_channel(self):
47         dl = FakeYDL()
48         ie = YoutubeChannelIE(dl)
49         #test paginated channel
50         result = ie.extract('https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w')
51         self.assertTrue(len(result['entries']) > 90)
52         #test autogenerated channel
53         result = ie.extract('https://www.youtube.com/channel/HCtnHdj3df7iM/videos')
54         self.assertTrue(len(result['entries']) >= 18)
55
56     def test_youtube_user(self):
57         dl = FakeYDL()
58         ie = YoutubeUserIE(dl)
59         result = ie.extract('https://www.youtube.com/user/TheLinuxFoundation')
60         self.assertTrue(len(result['entries']) >= 320)
61
62     def test_youtube_show(self):
63         dl = FakeYDL()
64         ie = YoutubeShowIE(dl)
65         result = ie.extract('http://www.youtube.com/show/airdisasters')
66         self.assertTrue(len(result) >= 3)
67
68     def test_youtube_mix(self):
69         dl = FakeYDL()
70         ie = YoutubePlaylistIE(dl)
71         result = ie.extract('https://www.youtube.com/watch?v=W01L70IGBgE&index=2&list=RDOQpdSVF_k_w')
72         entries = result['entries']
73         self.assertTrue(len(entries) >= 20)
74         original_video = entries[0]
75         self.assertEqual(original_video['id'], 'OQpdSVF_k_w')
76
77     def test_youtube_toptracks(self):
78         print('Skipping: The playlist page gives error 500')
79         return
80         dl = FakeYDL()
81         ie = YoutubePlaylistIE(dl)
82         result = ie.extract('https://www.youtube.com/playlist?list=MCUS')
83         entries = result['entries']
84         self.assertEqual(len(entries), 100)
85
86     def test_youtube_toplist(self):
87         dl = FakeYDL()
88         ie = YoutubeTopListIE(dl)
89         result = ie.extract('yttoplist:music:Trending')
90         entries = result['entries']
91         self.assertTrue(len(entries) >= 5)
92
93     def test_youtube_search_url(self):
94         dl = FakeYDL()
95         ie = YoutubeSearchURLIE(dl)
96         result = ie.extract('https://www.youtube.com/results?baz=bar&search_query=youtube-dl+test+video&filters=video&lclk=video')
97         entries = result['entries']
98         self.assertIsPlaylist(result)
99         self.assertEqual(result['title'], 'youtube-dl test video')
100         self.assertTrue(len(entries) >= 5)
101
102 if __name__ == '__main__':
103     unittest.main()