]>
Commit | Line | Data |
---|---|---|
9789a05c FV |
1 | #!/usr/bin/env python |
2 | ||
3 | import sys | |
4 | import unittest | |
5e5ddcfb | 5 | import json |
9789a05c FV |
6 | |
7 | # Allow direct execution | |
8 | import os | |
9 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
10 | ||
fb6c3199 | 11 | from youtube_dl.InfoExtractors import YoutubeUserIE, YoutubePlaylistIE, YoutubeIE, YoutubeChannelIE |
9789a05c | 12 | from youtube_dl.utils import * |
597cc8a4 | 13 | from youtube_dl.FileDownloader import FileDownloader |
9789a05c | 14 | |
5e5ddcfb FV |
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 | ||
9789a05c FV |
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) | |
9789a05c | 25 | |
597cc8a4 | 26 | class FakeDownloader(FileDownloader): |
9789a05c FV |
27 | def __init__(self): |
28 | self.result = [] | |
5e5ddcfb | 29 | self.params = parameters |
9789a05c FV |
30 | def to_screen(self, s): |
31 | print(s) | |
8749b712 | 32 | def trouble(self, s, tb=None): |
9789a05c | 33 | raise Exception(s) |
597cc8a4 JMF |
34 | def extract_info(self, url): |
35 | self.result.append(url) | |
36 | return url | |
9789a05c FV |
37 | |
38 | class TestYoutubeLists(unittest.TestCase): | |
8a38a194 JMF |
39 | def assertIsPlaylist(self,info): |
40 | """Make sure the info has '_type' set to 'playlist'""" | |
41 | self.assertEqual(info['_type'], 'playlist') | |
42 | ||
9789a05c | 43 | def test_youtube_playlist(self): |
679790ee PH |
44 | dl = FakeDownloader() |
45 | ie = YoutubePlaylistIE(dl) | |
8a38a194 JMF |
46 | result = ie.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')[0] |
47 | self.assertIsPlaylist(result) | |
c7293824 | 48 | self.assertEqual(result['title'], 'ytdl test PL') |
8a38a194 | 49 | ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']] |
acb8752f | 50 | self.assertEqual(ytie_results, [ 'bV9L5Ht9LgY', 'FXxLjLQi3Fg', 'tU3Bgo5qJZE']) |
9789a05c | 51 | |
acb8752f | 52 | def test_issue_673(self): |
679790ee PH |
53 | dl = FakeDownloader() |
54 | ie = YoutubePlaylistIE(dl) | |
8a38a194 | 55 | result = ie.extract('PLBB231211A4F62143')[0] |
c7293824 | 56 | self.assertEqual(result['title'], 'Team Fortress 2') |
8a38a194 | 57 | self.assertTrue(len(result['entries']) > 40) |
89de9eb1 | 58 | |
9789a05c | 59 | def test_youtube_playlist_long(self): |
679790ee PH |
60 | dl = FakeDownloader() |
61 | ie = YoutubePlaylistIE(dl) | |
8a38a194 JMF |
62 | result = ie.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')[0] |
63 | self.assertIsPlaylist(result) | |
64 | self.assertTrue(len(result['entries']) >= 799) | |
9789a05c | 65 | |
6324fd1d | 66 | def test_youtube_playlist_with_deleted(self): |
89de9eb1 | 67 | #651 |
679790ee PH |
68 | dl = FakeDownloader() |
69 | ie = YoutubePlaylistIE(dl) | |
8a38a194 JMF |
70 | result = ie.extract('https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC')[0] |
71 | ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']] | |
acb8752f PH |
72 | self.assertFalse('pElCt5oNDuI' in ytie_results) |
73 | self.assertFalse('KdPEApIVdWM' in ytie_results) | |
aba8df23 JMF |
74 | |
75 | def test_youtube_playlist_empty(self): | |
76 | dl = FakeDownloader() | |
77 | ie = YoutubePlaylistIE(dl) | |
78 | result = ie.extract('https://www.youtube.com/playlist?list=PLtPgu7CB4gbZDA7i_euNxn75ISqxwZPYx')[0] | |
79 | self.assertIsPlaylist(result) | |
80 | self.assertEqual(len(result['entries']), 0) | |
6324fd1d | 81 | |
9789a05c | 82 | def test_youtube_course(self): |
679790ee PH |
83 | dl = FakeDownloader() |
84 | ie = YoutubePlaylistIE(dl) | |
9789a05c | 85 | # TODO find a > 100 (paginating?) videos course |
8a38a194 JMF |
86 | result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')[0] |
87 | entries = result['entries'] | |
88 | self.assertEqual(YoutubeIE()._extract_id(entries[0]['url']), 'j9WZyLZCBzs') | |
89 | self.assertEqual(len(entries), 25) | |
90 | self.assertEqual(YoutubeIE()._extract_id(entries[-1]['url']), 'rYefUsYuEp0') | |
9789a05c FV |
91 | |
92 | def test_youtube_channel(self): | |
fb6c3199 | 93 | dl = FakeDownloader() |
94 | ie = YoutubeChannelIE(dl) | |
95 | #test paginated channel | |
96 | result = ie.extract('https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w')[0] | |
97 | self.assertTrue(len(result['entries']) > 90) | |
98 | #test autogenerated channel | |
99 | result = ie.extract('https://www.youtube.com/channel/HCtnHdj3df7iM/videos')[0] | |
96655778 | 100 | self.assertTrue(len(result['entries']) >= 18) |
9789a05c FV |
101 | |
102 | def test_youtube_user(self): | |
679790ee PH |
103 | dl = FakeDownloader() |
104 | ie = YoutubeUserIE(dl) | |
8a38a194 JMF |
105 | result = ie.extract('https://www.youtube.com/user/TheLinuxFoundation')[0] |
106 | self.assertTrue(len(result['entries']) >= 320) | |
9789a05c FV |
107 | |
108 | if __name__ == '__main__': | |
109 | unittest.main() |