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.

77 lines
2.6 KiB

  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. import json
  4. import os
  5. import sys
  6. import unittest
  7. # Allow direct execution
  8. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. import youtube_dl.YoutubeDL
  10. import youtube_dl.extractor
  11. from youtube_dl.utils import *
  12. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  13. # General configuration (from __init__, not very elegant...)
  14. jar = compat_cookiejar.CookieJar()
  15. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  16. proxy_handler = compat_urllib_request.ProxyHandler()
  17. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  18. compat_urllib_request.install_opener(opener)
  19. class YoutubeDL(youtube_dl.YoutubeDL):
  20. def __init__(self, *args, **kwargs):
  21. super(YoutubeDL, self).__init__(*args, **kwargs)
  22. self.to_stderr = self.to_screen
  23. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  24. params = json.load(pf)
  25. params['writeinfojson'] = True
  26. params['skip_download'] = True
  27. params['writedescription'] = True
  28. TEST_ID = 'BaW_jenozKc'
  29. INFO_JSON_FILE = TEST_ID + '.mp4.info.json'
  30. DESCRIPTION_FILE = TEST_ID + '.mp4.description'
  31. EXPECTED_DESCRIPTION = u'''test chars: "'/\ä↭𝕐
  32. This is a test video for youtube-dl.
  33. For more information, contact phihag@phihag.de .'''
  34. class TestInfoJSON(unittest.TestCase):
  35. def setUp(self):
  36. # Clear old files
  37. self.tearDown()
  38. def test_info_json(self):
  39. ie = youtube_dl.extractor.YoutubeIE()
  40. ydl = YoutubeDL(params)
  41. ydl.add_info_extractor(ie)
  42. ydl.download([TEST_ID])
  43. self.assertTrue(os.path.exists(INFO_JSON_FILE))
  44. with io.open(INFO_JSON_FILE, 'r', encoding='utf-8') as jsonf:
  45. jd = json.load(jsonf)
  46. self.assertEqual(jd['upload_date'], u'20121002')
  47. self.assertEqual(jd['description'], EXPECTED_DESCRIPTION)
  48. self.assertEqual(jd['id'], TEST_ID)
  49. self.assertEqual(jd['extractor'], 'youtube')
  50. self.assertEqual(jd['title'], u'''youtube-dl test video "'/\ä↭𝕐''')
  51. self.assertEqual(jd['uploader'], 'Philipp Hagemeister')
  52. self.assertTrue(os.path.exists(DESCRIPTION_FILE))
  53. with io.open(DESCRIPTION_FILE, 'r', encoding='utf-8') as descf:
  54. descr = descf.read()
  55. self.assertEqual(descr, EXPECTED_DESCRIPTION)
  56. def tearDown(self):
  57. if os.path.exists(INFO_JSON_FILE):
  58. os.remove(INFO_JSON_FILE)
  59. if os.path.exists(DESCRIPTION_FILE):
  60. os.remove(DESCRIPTION_FILE)
  61. if __name__ == '__main__':
  62. unittest.main()