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.

79 lines
2.7 KiB

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