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.

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