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