]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python3 | |
2 | ||
3 | # Allow direct execution | |
4 | import os | |
5 | import sys | |
6 | import unittest | |
7 | ||
8 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
9 | ||
10 | ||
11 | from test.helper import is_download_test, try_rm | |
12 | from yt_dlp import YoutubeDL | |
13 | from yt_dlp.utils import DownloadError | |
14 | ||
15 | ||
16 | def _download_restricted(url, filename, age): | |
17 | """ Returns true if the file has been downloaded """ | |
18 | ||
19 | params = { | |
20 | 'age_limit': age, | |
21 | 'skip_download': True, | |
22 | 'writeinfojson': True, | |
23 | 'outtmpl': '%(id)s.%(ext)s', | |
24 | } | |
25 | ydl = YoutubeDL(params) | |
26 | ydl.add_default_info_extractors() | |
27 | json_filename = os.path.splitext(filename)[0] + '.info.json' | |
28 | try_rm(json_filename) | |
29 | try: | |
30 | ydl.download([url]) | |
31 | except DownloadError: | |
32 | pass | |
33 | else: | |
34 | return os.path.exists(json_filename) | |
35 | finally: | |
36 | try_rm(json_filename) | |
37 | ||
38 | ||
39 | @is_download_test | |
40 | class TestAgeRestriction(unittest.TestCase): | |
41 | def _assert_restricted(self, url, filename, age, old_age=None): | |
42 | self.assertTrue(_download_restricted(url, filename, old_age)) | |
43 | self.assertFalse(_download_restricted(url, filename, age)) | |
44 | ||
45 | def test_youtube(self): | |
46 | self._assert_restricted('HtVdAasjOgU', 'HtVdAasjOgU.mp4', 10) | |
47 | ||
48 | def test_youporn(self): | |
49 | self._assert_restricted( | |
50 | 'https://www.youporn.com/watch/16715086/sex-ed-in-detention-18-asmr/', | |
51 | '16715086.mp4', 2, old_age=25) | |
52 | ||
53 | ||
54 | if __name__ == '__main__': | |
55 | unittest.main() |