test_age_restriction.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. from test.helper import is_download_test, try_rm
  8. from hypervideo_dl import YoutubeDL
  9. from hypervideo_dl.utils import DownloadError
  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. try:
  23. ydl.download([url])
  24. except DownloadError:
  25. pass
  26. else:
  27. return os.path.exists(json_filename)
  28. finally:
  29. try_rm(json_filename)
  30. @is_download_test
  31. class TestAgeRestriction(unittest.TestCase):
  32. def _assert_restricted(self, url, filename, age, old_age=None):
  33. self.assertTrue(_download_restricted(url, filename, old_age))
  34. self.assertFalse(_download_restricted(url, filename, age))
  35. def test_youtube(self):
  36. self._assert_restricted('HtVdAasjOgU', 'HtVdAasjOgU.mp4', 10)
  37. def test_youporn(self):
  38. self._assert_restricted(
  39. 'https://www.youporn.com/watch/16715086/sex-ed-in-detention-18-asmr/',
  40. '16715086.mp4', 2, old_age=25)
  41. if __name__ == '__main__':
  42. unittest.main()