]> jfr.im git - yt-dlp.git/blob - devscripts/check-porn.py
[cleanup] Upgrade syntax
[yt-dlp.git] / devscripts / check-porn.py
1 #!/usr/bin/env python3
2 """
3 This script employs a VERY basic heuristic ('porn' in webpage.lower()) to check
4 if we are not 'age_limit' tagging some porn site
5
6 A second approach implemented relies on a list of porn domains, to activate it
7 pass the list filename as the only argument
8 """
9
10 # Allow direct execution
11 import os
12 import sys
13 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
14
15 from test.helper import gettestcases
16 from yt_dlp.utils import compat_urllib_parse_urlparse
17 from yt_dlp.utils import compat_urllib_request
18
19 if len(sys.argv) > 1:
20 METHOD = 'LIST'
21 LIST = open(sys.argv[1]).read().decode('utf8').strip()
22 else:
23 METHOD = 'EURISTIC'
24
25 for test in gettestcases():
26 if METHOD == 'EURISTIC':
27 try:
28 webpage = compat_urllib_request.urlopen(test['url'], timeout=10).read()
29 except Exception:
30 print('\nFail: {}'.format(test['name']))
31 continue
32
33 webpage = webpage.decode('utf8', 'replace')
34
35 RESULT = 'porn' in webpage.lower()
36
37 elif METHOD == 'LIST':
38 domain = compat_urllib_parse_urlparse(test['url']).netloc
39 if not domain:
40 print('\nFail: {}'.format(test['name']))
41 continue
42 domain = '.'.join(domain.split('.')[-2:])
43
44 RESULT = ('.' + domain + '\n' in LIST or '\n' + domain + '\n' in LIST)
45
46 if RESULT and ('info_dict' not in test or 'age_limit' not in test['info_dict']
47 or test['info_dict']['age_limit'] != 18):
48 print('\nPotential missing age_limit check: {}'.format(test['name']))
49
50 elif not RESULT and ('info_dict' in test and 'age_limit' in test['info_dict']
51 and test['info_dict']['age_limit'] == 18):
52 print('\nPotential false negative: {}'.format(test['name']))
53
54 else:
55 sys.stdout.write('.')
56 sys.stdout.flush()
57
58 print()