You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.2 KiB

  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  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. from test.helper import get_params
  10. import io
  11. import json
  12. import youtube_dl.YoutubeDL
  13. import youtube_dl.extractor
  14. class YoutubeDL(youtube_dl.YoutubeDL):
  15. def __init__(self, *args, **kwargs):
  16. super(YoutubeDL, self).__init__(*args, **kwargs)
  17. self.to_stderr = self.to_screen
  18. params = get_params({
  19. 'writeinfojson': True,
  20. 'skip_download': True,
  21. 'writedescription': True,
  22. })
  23. TEST_ID = 'BaW_jenozKc'
  24. INFO_JSON_FILE = TEST_ID + '.info.json'
  25. DESCRIPTION_FILE = TEST_ID + '.mp4.description'
  26. EXPECTED_DESCRIPTION = '''test chars: "'/\ä↭𝕐
  27. test URL: https://github.com/rg3/youtube-dl/issues/1892
  28. This is a test video for youtube-dl.
  29. For more information, contact phihag@phihag.de .'''
  30. class TestInfoJSON(unittest.TestCase):
  31. def setUp(self):
  32. # Clear old files
  33. self.tearDown()
  34. def test_info_json(self):
  35. ie = youtube_dl.extractor.YoutubeIE()
  36. ydl = YoutubeDL(params)
  37. ydl.add_info_extractor(ie)
  38. ydl.download([TEST_ID])
  39. self.assertTrue(os.path.exists(INFO_JSON_FILE))
  40. with io.open(INFO_JSON_FILE, 'r', encoding='utf-8') as jsonf:
  41. jd = json.load(jsonf)
  42. self.assertEqual(jd['upload_date'], '20121002')
  43. self.assertEqual(jd['description'], EXPECTED_DESCRIPTION)
  44. self.assertEqual(jd['id'], TEST_ID)
  45. self.assertEqual(jd['extractor'], 'youtube')
  46. self.assertEqual(jd['title'], '''youtube-dl test video "'/\ä↭𝕐''')
  47. self.assertEqual(jd['uploader'], 'Philipp Hagemeister')
  48. self.assertTrue(os.path.exists(DESCRIPTION_FILE))
  49. with io.open(DESCRIPTION_FILE, 'r', encoding='utf-8') as descf:
  50. descr = descf.read()
  51. self.assertEqual(descr, EXPECTED_DESCRIPTION)
  52. def tearDown(self):
  53. if os.path.exists(INFO_JSON_FILE):
  54. os.remove(INFO_JSON_FILE)
  55. if os.path.exists(DESCRIPTION_FILE):
  56. os.remove(DESCRIPTION_FILE)
  57. if __name__ == '__main__':
  58. unittest.main()