]> Raphaël G. Git Repositories - youtubedl/blob - test/test_write_info_json.py
b5eca7649f7b601bf3eeaabfc363a62a7f97862b
[youtubedl] / test / test_write_info_json.py
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 import json
5 import os
6 import sys
7 import unittest
8
9 # Allow direct execution
10 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
12 import youtube_dl.FileDownloader
13 import youtube_dl.extractor
14 from youtube_dl.utils import *
15
16 PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
17
18 # General configuration (from __init__, not very elegant...)
19 jar = compat_cookiejar.CookieJar()
20 cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
21 proxy_handler = compat_urllib_request.ProxyHandler()
22 opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
23 compat_urllib_request.install_opener(opener)
24
25 class FileDownloader(youtube_dl.FileDownloader):
26 def __init__(self, *args, **kwargs):
27 youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
28 self.to_stderr = self.to_screen
29
30 with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
31 params = json.load(pf)
32 params['writeinfojson'] = True
33 params['skip_download'] = True
34 params['writedescription'] = True
35
36 TEST_ID = 'BaW_jenozKc'
37 INFO_JSON_FILE = TEST_ID + '.mp4.info.json'
38 DESCRIPTION_FILE = TEST_ID + '.mp4.description'
39 EXPECTED_DESCRIPTION = u'''test chars: "'/\ä↭𝕐
40
41 This is a test video for youtube-dl.
42
43 For more information, contact phihag@phihag.de .'''
44
45 class TestInfoJSON(unittest.TestCase):
46 def setUp(self):
47 # Clear old files
48 self.tearDown()
49
50 def test_info_json(self):
51 ie = youtube_dl.extractor.YoutubeIE()
52 fd = FileDownloader(params)
53 fd.add_info_extractor(ie)
54 fd.download([TEST_ID])
55 self.assertTrue(os.path.exists(INFO_JSON_FILE))
56 with io.open(INFO_JSON_FILE, 'r', encoding='utf-8') as jsonf:
57 jd = json.load(jsonf)
58 self.assertEqual(jd['upload_date'], u'20121002')
59 self.assertEqual(jd['description'], EXPECTED_DESCRIPTION)
60 self.assertEqual(jd['id'], TEST_ID)
61 self.assertEqual(jd['extractor'], 'youtube')
62 self.assertEqual(jd['title'], u'''youtube-dl test video "'/\ä↭𝕐''')
63 self.assertEqual(jd['uploader'], 'Philipp Hagemeister')
64
65 self.assertTrue(os.path.exists(DESCRIPTION_FILE))
66 with io.open(DESCRIPTION_FILE, 'r', encoding='utf-8') as descf:
67 descr = descf.read()
68 self.assertEqual(descr, EXPECTED_DESCRIPTION)
69
70 def tearDown(self):
71 if os.path.exists(INFO_JSON_FILE):
72 os.remove(INFO_JSON_FILE)
73 if os.path.exists(DESCRIPTION_FILE):
74 os.remove(DESCRIPTION_FILE)
75
76 if __name__ == '__main__':
77 unittest.main()