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