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