test_age_restriction.py 1.3 KB

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