test_age_restriction.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  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
  9. from youtube_dl import YoutubeDL
  10. from youtube_dl.utils import DownloadError
  11. def _download_restricted(url, filename, age):
  12. """ Returns true if the file has been downloaded """
  13. params = {
  14. 'age_limit': age,
  15. 'skip_download': True,
  16. 'writeinfojson': True,
  17. 'outtmpl': '%(id)s.%(ext)s',
  18. }
  19. ydl = YoutubeDL(params)
  20. ydl.add_default_info_extractors()
  21. json_filename = os.path.splitext(filename)[0] + '.info.json'
  22. try_rm(json_filename)
  23. try:
  24. ydl.download([url])
  25. except DownloadError:
  26. try_rm(json_filename)
  27. res = os.path.exists(json_filename)
  28. try_rm(json_filename)
  29. return res
  30. class TestAgeRestriction(unittest.TestCase):
  31. def _assert_restricted(self, url, filename, age, old_age=None):
  32. self.assertTrue(_download_restricted(url, filename, old_age))
  33. self.assertFalse(_download_restricted(url, filename, age))
  34. def test_youtube(self):
  35. self._assert_restricted('HtVdAasjOgU', 'HtVdAasjOgU.mp4', 10)
  36. def test_youporn(self):
  37. self._assert_restricted(
  38. 'https://www.youporn.com/watch/16715086/sex-ed-in-detention-18-asmr/',
  39. '16715086.mp4', 2, old_age=25)
  40. if __name__ == '__main__':
  41. unittest.main()