test_age_restriction.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. from __future__ import unicode_literals
  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 try_rm, is_download_test
  9. from hypervideo_dl import YoutubeDL
  10. def _download_restricted(url, filename, age):
  11. """ Returns true if the file has been downloaded """
  12. params = {
  13. 'age_limit': age,
  14. 'skip_download': True,
  15. 'writeinfojson': True,
  16. 'outtmpl': '%(id)s.%(ext)s',
  17. }
  18. ydl = YoutubeDL(params)
  19. ydl.add_default_info_extractors()
  20. json_filename = os.path.splitext(filename)[0] + '.info.json'
  21. try_rm(json_filename)
  22. ydl.download([url])
  23. res = os.path.exists(json_filename)
  24. try_rm(json_filename)
  25. return res
  26. @is_download_test
  27. class TestAgeRestriction(unittest.TestCase):
  28. def _assert_restricted(self, url, filename, age, old_age=None):
  29. self.assertTrue(_download_restricted(url, filename, old_age))
  30. self.assertFalse(_download_restricted(url, filename, age))
  31. def test_youtube(self):
  32. self._assert_restricted('07FYdnEawAQ', '07FYdnEawAQ.mp4', 10)
  33. def test_youporn(self):
  34. self._assert_restricted(
  35. 'http://www.youporn.com/watch/505835/sex-ed-is-it-safe-to-masturbate-daily/',
  36. '505835.mp4', 2, old_age=25)
  37. if __name__ == '__main__':
  38. unittest.main()