test_age_restriction.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 yt_dlp import YoutubeDL
  9. from yt_dlp.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()