forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
161 lines (129 loc) · 5.12 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# coding=utf-8
import os
import sys
from datetime import datetime
from termcolor import colored
import requests
import regex
from glob import glob
class Helpers:
min_log_level = 0
# Allows use of `environ_or_none("foo") or "default"` shorthand
# noinspection PyBroadException,PyMissingTypeHints
def environ_or_none(key):
try:
return os.environ[key]
except KeyError:
return None
def escape_format(s):
return s.replace("{", "{{").replace("}", "}}")
def expand_shorthand_link(s):
s = s.lower()
if s.endswith("so"):
s = s[:-2] + "stackoverflow.com"
elif s.endswith("se"):
s = s[:-2] + "stackexchange.com"
elif s.endswith("su"):
s = s[:-2] + "superuser.com"
elif s.endswith("sf"):
s = s[:-2] + "serverfault.com"
elif s.endswith("au"):
s = s[:-2] + "askubuntu.com"
return s
# noinspection PyMissingTypeHints
def log(log_level, *args):
levels = {
'debug': [0, 'grey'],
'info': [1, 'cyan'],
'warning': [2, 'yellow'],
'error': [3, 'red']
}
level = levels[log_level][0]
if level < Helpers.min_log_level:
return
color = (levels[log_level][1] if log_level in levels else 'white')
log_str = u"{} {}".format(colored("[{}]".format(datetime.now().isoformat()[11:-7]), color),
u" ".join([str(x) for x in args]))
print(log_str)
def only_blacklists_changed(diff):
blacklist_files = ["bad_keywords.txt", "blacklisted_usernames.txt", "blacklisted_websites.txt",
"watched_keywords.txt"]
files_changed = diff.split()
return not any([f for f in files_changed if f not in blacklist_files])
# FAIR WARNING: Sending HEAD requests to resolve a shortened link is generally okay - there aren't
# as many exploits that work on just HEAD responses. If you specify sending a GET request, you
# acknowledge that this will fetch the full, potentially unsafe response from the shortener.
def unshorten_link(url, request_type='HEAD', explicitly_ignore_security_warning=False):
requesters = {
'GET': requests.get,
'HEAD': requests.head
}
if request_type not in requesters:
raise KeyError('Unavailable request_type {}'.format(request_type))
if request_type == 'GET' and not explicitly_ignore_security_warning:
raise SecurityError('Potentially unsafe request type GET not acknowledged')
requester = requesters[request_type]
response_code = 301
headers = {'User-Agent': 'SmokeDetector/git (+https://github.com/Charcoal-SE/SmokeDetector)'}
while response_code in [301, 302, 303, 307, 308]:
res = requester(url, headers=headers)
response_code = res.status_code
if 'Location' in res.headers:
url = res.headers['Location']
return url
parser_regex = r'((?:meta\.)?(?:(?:(?:math|(?:\w{2}\.)?stack)overflow|askubuntu|superuser|serverfault)|\w+)' \
r'(?:\.meta)?)\.(?:stackexchange\.com|com|net)'
parser = regex.compile(parser_regex)
exceptions = {
'meta.superuser': 'meta.superuser',
'meta.serverfault': 'meta.serverfault',
'meta.askubuntu': 'meta.askubuntu',
'mathoverflow': 'mathoverflow.net',
'meta.mathoverflow': 'meta.mathoverflow.net',
'meta.stackexchange': 'meta'
}
def api_parameter_from_link(link):
match = parser.search(link)
if match:
if match[1] in exceptions.keys():
return exceptions[match[1]]
elif 'meta.' in match[1] and 'stackoverflow' not in match[1]:
return '.'.join(match[1].split('.')[::-1])
else:
return match[1]
else:
return None
id_parser_regex = r'(?:https?:)?//[^/]+/\w+/(\d+)'
id_parser = regex.compile(id_parser_regex)
def post_id_from_link(link):
match = id_parser.search(link)
if match:
return match[1]
else:
return None
def to_metasmoke_link(post_url, protocol=True):
return "{}//m.erwaysoftware.com/posts/uid/{}/{}".format(
"https:" if protocol else "", api_parameter_from_link(post_url), post_id_from_link(post_url))
def blacklist_integrity_check():
bl_files = glob('bad_*.txt') + glob('blacklisted_*.txt') + ['watched_keywords.txt']
seen = dict()
errors = []
for bl_file in bl_files:
with open(bl_file, 'r') as lines:
for lineno, line in enumerate(lines, 1):
if line.endswith('\r\n'):
errors.append('{0}:{1}:DOS line ending'.format(bl_file, lineno))
elif not line.endswith('\n'):
errors.append('{0}:{1}:No newline'.format(bl_file, lineno))
elif line == '\n':
errors.append('{0}:{1}:Empty line'.format(bl_file, lineno))
elif bl_file == 'watched_keywords.txt':
line = line.split('\t')[2]
if line in seen:
errors.append('{0}:{1}:Duplicate entry {2} (also {3})'.format(
bl_file, lineno, line.rstrip('\n'), seen[line]))
else:
seen[line] = '{0}:{1}'.format(bl_file, lineno)
return errors
class SecurityError(Exception):
pass