test_download.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env python
  2. import hashlib
  3. import io
  4. import os
  5. import json
  6. import unittest
  7. import sys
  8. import socket
  9. import hashlib
  10. # Allow direct execution
  11. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  12. import youtube_dl.FileDownloader
  13. import youtube_dl.InfoExtractors
  14. from youtube_dl.utils import *
  15. DEF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests.json')
  16. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  17. # General configuration (from __init__, not very elegant...)
  18. jar = compat_cookiejar.CookieJar()
  19. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  20. proxy_handler = compat_urllib_request.ProxyHandler()
  21. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  22. compat_urllib_request.install_opener(opener)
  23. socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
  24. class FileDownloader(youtube_dl.FileDownloader):
  25. def __init__(self, *args, **kwargs):
  26. self.to_stderr = self.to_screen
  27. self.processed_info_dicts = []
  28. return youtube_dl.FileDownloader.__init__(self, *args, **kwargs)
  29. def process_info(self, info_dict):
  30. self.processed_info_dicts.append(info_dict)
  31. return youtube_dl.FileDownloader.process_info(self, info_dict)
  32. def _file_md5(fn):
  33. with open(fn, 'rb') as f:
  34. return hashlib.md5(f.read()).hexdigest()
  35. with io.open(DEF_FILE, encoding='utf-8') as deff:
  36. defs = json.load(deff)
  37. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  38. parameters = json.load(pf)
  39. class TestDownload(unittest.TestCase):
  40. def setUp(self):
  41. self.parameters = parameters
  42. self.defs = defs
  43. # Clear old files
  44. self.tearDown()
  45. def tearDown(self):
  46. for fn in [ test.get('file', False) for test in self.defs ]:
  47. if fn and os.path.exists(fn):
  48. os.remove(fn)
  49. ### Dinamically generate tests
  50. def generator(test_case):
  51. def test_template(self):
  52. ie = getattr(youtube_dl.InfoExtractors, test_case['name'] + 'IE')
  53. if not ie._WORKING:
  54. print('Skipping: IE marked as not _WORKING')
  55. return
  56. if not test_case['file']:
  57. print('Skipping: No output file specified')
  58. return
  59. if 'skip' in test_case:
  60. print('Skipping: {0}'.format(test_case['skip']))
  61. return
  62. params = dict(self.parameters) # Duplicate it locally
  63. for p in test_case.get('params', {}):
  64. params[p] = test_case['params'][p]
  65. fd = FileDownloader(params)
  66. fd.add_info_extractor(ie())
  67. for ien in test_case.get('add_ie', []):
  68. fd.add_info_extractor(getattr(youtube_dl.InfoExtractors, ien + 'IE')())
  69. fd.download([test_case['url']])
  70. self.assertTrue(os.path.exists(test_case['file']))
  71. if 'md5' in test_case:
  72. md5_for_file = _file_md5(test_case['file'])
  73. self.assertEqual(md5_for_file, test_case['md5'])
  74. info_dict = fd.processed_info_dicts[0]
  75. for (info_element, value) in test_case.get('info_dict', {}).items():
  76. if value.startswith('md5:'):
  77. md5_info_value = hashlib.md5(info_dict[info_element]).hexdigest()
  78. self.assertEqual(value[3:], md5_info_value)
  79. else:
  80. self.assertEqual(value, info_dict[info_element])
  81. return test_template
  82. ### And add them to TestDownload
  83. for test_case in defs:
  84. test_method = generator(test_case)
  85. test_method.__name__ = "test_{0}".format(test_case["name"])
  86. setattr(TestDownload, test_method.__name__, test_method)
  87. del test_method
  88. if __name__ == '__main__':
  89. unittest.main()