Allow users to specify an age limit (fixes #1545)
[youtube-dl] / test / test_age_restriction.py
1 #!/usr/bin/env python
2
3 import sys
4 import unittest
5
6 # Allow direct execution
7 import os
8 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10 from youtube_dl import YoutubeDL
11 from helper import try_rm
12
13
14 def _download_restricted(url, filename, age):
15     """ Returns true iff the file has been downloaded """
16
17     params = {
18         'age_limit': age,
19         'skip_download': True,
20         'writeinfojson': True,
21         "outtmpl": "%(id)s.%(ext)s",
22     }
23     ydl = YoutubeDL(params)
24     ydl.add_default_info_extractors()
25     json_filename = filename + '.info.json'
26     try_rm(json_filename)
27     ydl.download([url])
28     res = os.path.exists(json_filename)
29     try_rm(json_filename)
30     return res
31
32
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     def test_pornotube(self):
47         self._assert_restricted(
48             'http://pornotube.com/c/173/m/1689755/Marilyn-Monroe-Bathing',
49             '1689755.flv', 13)
50
51
52 if __name__ == '__main__':
53     unittest.main()