test subtitles
[youtube-dl] / test / test_youtube_lists.py
1 #!/usr/bin/env python
2
3 import sys
4 import unittest
5 import socket
6 import json
7
8 # Allow direct execution
9 import os
10 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
12 from youtube_dl.InfoExtractors import YoutubeUserIE,YoutubePlaylistIE
13 from youtube_dl.utils import *
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 socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
26
27 class FakeDownloader(object):
28     def __init__(self):
29         self.result = []
30         self.params = parameters
31     def to_screen(self, s):
32         print(s)
33     def trouble(self, s):
34         raise Exception(s)
35     def download(self, x):
36         self.result.append(x)
37
38 class TestYoutubeLists(unittest.TestCase):
39     def test_youtube_playlist(self):
40         DL = FakeDownloader()
41         IE = YoutubePlaylistIE(DL)
42         IE.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')
43         self.assertEqual(DL.result, [
44             ['http://www.youtube.com/watch?v=bV9L5Ht9LgY'],
45             ['http://www.youtube.com/watch?v=FXxLjLQi3Fg'],
46             ['http://www.youtube.com/watch?v=tU3Bgo5qJZE']
47         ])
48
49     def test_youtube_playlist_long(self):
50         DL = FakeDownloader()
51         IE = YoutubePlaylistIE(DL)
52         IE.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')
53         self.assertTrue(len(DL.result) >= 799)
54
55     def test_youtube_course(self):
56         DL = FakeDownloader()
57         IE = YoutubePlaylistIE(DL)
58         # TODO find a > 100 (paginating?) videos course
59         IE.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')
60         self.assertEqual(DL.result[0], ['http://www.youtube.com/watch?v=j9WZyLZCBzs'])
61         self.assertEqual(len(DL.result), 25)
62         self.assertEqual(DL.result[-1], ['http://www.youtube.com/watch?v=rYefUsYuEp0'])
63
64     def test_youtube_channel(self):
65         # I give up, please find a channel that does paginate and test this like test_youtube_playlist_long
66         pass # TODO
67
68     def test_youtube_user(self):
69         DL = FakeDownloader()
70         IE = YoutubeUserIE(DL)
71         IE.extract('https://www.youtube.com/user/TheLinuxFoundation')
72         self.assertTrue(len(DL.result) >= 320)
73
74 if __name__ == '__main__':
75     unittest.main()