]> jfr.im git - yt-dlp.git/blame - test/test_download.py
refactor YouTube subtitles code, it was ugly (my bad)
[yt-dlp.git] / test / test_download.py
CommitLineData
fd5ff020
FV
1#!/usr/bin/env python
2
efe8902f 3import hashlib
fd5ff020 4import io
efe8902f 5import os
7f60b5aa 6import json
cdab8aa3
PH
7import unittest
8import sys
fd5ff020
FV
9import socket
10
11# Allow direct execution
12sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
cdab8aa3 13
fd5ff020
FV
14import youtube_dl.FileDownloader
15import youtube_dl.InfoExtractors
16from youtube_dl.utils import *
1535ac2a 17
18DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
fd5ff020
FV
19PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
20
21# General configuration (from __init__, not very elegant...)
22jar = compat_cookiejar.CookieJar()
23cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
24proxy_handler = compat_urllib_request.ProxyHandler()
25opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
26compat_urllib_request.install_opener(opener)
27socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
28
29class FileDownloader(youtube_dl.FileDownloader):
30 def __init__(self, *args, **kwargs):
31 youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
32 self.to_stderr = self.to_screen
1535ac2a 33
fd5ff020
FV
34def _file_md5(fn):
35 with open(fn, 'rb') as f:
36 return hashlib.md5(f.read()).hexdigest()
37
38with io.open(DEF_FILE, encoding='utf-8') as deff:
39 defs = json.load(deff)
40with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
41 parameters = json.load(pf)
1535ac2a 42
43class TestDownload(unittest.TestCase):
fd5ff020
FV
44 def setUp(self):
45 self.parameters = parameters
46 self.defs = defs
47
48 # Clear old files
49 self.tearDown()
1535ac2a 50
fd5ff020 51 def tearDown(self):
5d01a647 52 for fn in [ test.get('file', False) for test in self.defs ]:
fd5ff020
FV
53 if fn and os.path.exists(fn):
54 os.remove(fn)
1535ac2a 55
1535ac2a 56
5d01a647
PH
57### Dinamically generate tests
58def generator(test_case):
59
1535ac2a 60 def test_template(self):
fd5ff020
FV
61 ie = getattr(youtube_dl.InfoExtractors, test_case['name'] + 'IE')
62 if not ie._WORKING:
63 print('Skipping: IE marked as not _WORKING')
64 return
65 if not test_case['file']:
66 print('Skipping: No output file specified')
67 return
68 if 'skip' in test_case:
69 print('Skipping: {0}'.format(test_case['skip']))
70 return
71 params = dict(self.parameters) # Duplicate it locally
72 for p in test_case.get('params', {}):
73 params[p] = test_case['params'][p]
74 fd = FileDownloader(params)
75 fd.add_info_extractor(ie())
76 for ien in test_case.get('add_ie', []):
77 fd.add_info_extractor(getattr(youtube_dl.InfoExtractors, ien + 'IE')())
78 fd.download([test_case['url']])
79 self.assertTrue(os.path.exists(test_case['file']))
80 if 'md5' in test_case:
81 md5_for_file = _file_md5(test_case['file'])
82 self.assertEqual(md5_for_file, test_case['md5'])
83
1535ac2a 84 return test_template
fd5ff020 85
5d01a647 86### And add them to TestDownload
fd5ff020 87for test_case in defs:
5d01a647 88 test_method = generator(test_case)
fd5ff020
FV
89 test_method.__name__ = "test_{0}".format(test_case["name"])
90 setattr(TestDownload, test_method.__name__, test_method)
5d01a647 91 del test_method
cdab8aa3
PH
92
93
94if __name__ == '__main__':
95 unittest.main()