test_age_restriction.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. 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. 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()