]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
54007a45 | 2 | |
44a5f171 PH |
3 | # Allow direct execution |
4 | import os | |
8dbe9899 PH |
5 | import sys |
6 | import unittest | |
f8271158 | 7 | |
44a5f171 PH |
8 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
9 | ||
8dbe9899 | 10 | |
54007a45 | 11 | from test.helper import is_download_test, try_rm |
7a5c1cfe | 12 | from yt_dlp import YoutubeDL |
45b2ee6f | 13 | from yt_dlp.utils import DownloadError |
8dbe9899 PH |
14 | |
15 | ||
16 | def _download_restricted(url, filename, age): | |
8ae98080 | 17 | """ Returns true if the file has been downloaded """ |
8dbe9899 PH |
18 | |
19 | params = { | |
20 | 'age_limit': age, | |
21 | 'skip_download': True, | |
22 | 'writeinfojson': True, | |
a0f59cdc | 23 | 'outtmpl': '%(id)s.%(ext)s', |
8dbe9899 PH |
24 | } |
25 | ydl = YoutubeDL(params) | |
26 | ydl.add_default_info_extractors() | |
f99e0f1e | 27 | json_filename = os.path.splitext(filename)[0] + '.info.json' |
8dbe9899 | 28 | try_rm(json_filename) |
45b2ee6f | 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) | |
8dbe9899 PH |
37 | |
38 | ||
060ac762 | 39 | @is_download_test |
8dbe9899 PH |
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): | |
45b2ee6f | 46 | self._assert_restricted('HtVdAasjOgU', 'HtVdAasjOgU.mp4', 10) |
8dbe9899 PH |
47 | |
48 | def test_youporn(self): | |
49 | self._assert_restricted( | |
45b2ee6f | 50 | 'https://www.youporn.com/watch/16715086/sex-ed-in-detention-18-asmr/', |
51 | '16715086.mp4', 2, old_age=25) | |
8dbe9899 | 52 | |
8dbe9899 PH |
53 | |
54 | if __name__ == '__main__': | |
55 | unittest.main() |