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.

75 lines
2.1 KiB

  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. from test.helper import get_params, global_setup
  9. global_setup()
  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 + '.mp4.info.json'
  25. DESCRIPTION_FILE = TEST_ID + '.mp4.description'
  26. EXPECTED_DESCRIPTION = u'''test chars: "'/\ä↭𝕐
  27. This is a test video for youtube-dl.
  28. For more information, contact phihag@phihag.de .'''
  29. class TestInfoJSON(unittest.TestCase):
  30. def setUp(self):
  31. # Clear old files
  32. self.tearDown()
  33. def test_info_json(self):
  34. ie = youtube_dl.extractor.YoutubeIE()
  35. ydl = YoutubeDL(params)
  36. ydl.add_info_extractor(ie)
  37. ydl.download([TEST_ID])
  38. self.assertTrue(os.path.exists(INFO_JSON_FILE))
  39. with io.open(INFO_JSON_FILE, 'r', encoding='utf-8') as jsonf:
  40. jd = json.load(jsonf)
  41. self.assertEqual(jd['upload_date'], u'20121002')
  42. self.assertEqual(jd['description'], EXPECTED_DESCRIPTION)
  43. self.assertEqual(jd['id'], TEST_ID)
  44. self.assertEqual(jd['extractor'], 'youtube')
  45. self.assertEqual(jd['title'], u'''youtube-dl test video "'/\ä↭𝕐''')
  46. self.assertEqual(jd['uploader'], 'Philipp Hagemeister')
  47. self.assertTrue(os.path.exists(DESCRIPTION_FILE))
  48. with io.open(DESCRIPTION_FILE, 'r', encoding='utf-8') as descf:
  49. descr = descf.read()
  50. self.assertEqual(descr, EXPECTED_DESCRIPTION)
  51. def tearDown(self):
  52. if os.path.exists(INFO_JSON_FILE):
  53. os.remove(INFO_JSON_FILE)
  54. if os.path.exists(DESCRIPTION_FILE):
  55. os.remove(DESCRIPTION_FILE)
  56. if __name__ == '__main__':
  57. unittest.main()