10
10
11
11
12
12
# DEFAULTS
13
- regex_allowed_chars = r"[^\u00E5\u00E4\u00F6\u00C5\u00C4\u00D6a-zA-Zå-öÅ-Ö 0-9\-:\/]+"
13
+ REGEX_ALLOWED_CHARS = r"[^\u00E5\u00E4\u00F6\u00C5\u00C4\u00D6a-zA-Zå-öÅ-Ö 0-9\-:\/]+"
14
14
REQUEST_TIMEOUT = get_config_or_default ('http_request_timeout' )
15
15
USERAGENT = get_config_or_default ('useragent' )
16
- time_sleep = get_config_or_default ('WEBBKOLL_SLEEP' )
17
- if time_sleep < 5 :
18
- time_sleep = 5
16
+ TIME_SLEEP = max (get_config_or_default ('WEBBKOLL_SLEEP' ), 5 )
19
17
REVIEW_SHOW_IMPROVEMENTS_ONLY = get_config_or_default ('review_show_improvements_only' )
20
18
21
19
def run_test (global_translation , lang_code , url ):
@@ -32,8 +30,7 @@ def run_test(global_translation, lang_code, url):
32
30
datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )))
33
31
34
32
orginal_url = url
35
- url = 'https://webbkoll.dataskydd.net/{1}/check?url={0}' .format (
36
- urllib .parse .quote (url ), lang_code )
33
+ url = f'https://webbkoll.dataskydd.net/{ urllib .parse .quote (url )} /check?url={ lang_code } '
37
34
headers = {
38
35
'user-agent' : 'Mozilla/5.0 (compatible; Webperf; +https://webperf.se)' }
39
36
@@ -45,7 +42,7 @@ def run_test(global_translation, lang_code, url):
45
42
request = session .get (url , allow_redirects = True ,
46
43
headers = headers , timeout = REQUEST_TIMEOUT )
47
44
48
- if 'type="search" value="{0 }">' . format ( orginal_url ) in request .text :
45
+ if f 'type="search" value="{ orginal_url } ">' in request .text :
49
46
# headers[''] = ''
50
47
regex = r"_csrf_token[^>]*value=\"(?P<csrf>[^\"]+)\""
51
48
matches = re .finditer (regex , request .text , re .MULTILINE )
@@ -57,20 +54,19 @@ def run_test(global_translation, lang_code, url):
57
54
'_csrf_token' : csrf_value ,
58
55
'url' : orginal_url ,
59
56
'submit' : '' }
60
- service_url = 'https://webbkoll.dataskydd.net/{0}/check' .format (
61
- lang_code )
57
+ service_url = f'https://webbkoll.dataskydd.net/{ lang_code } /check'
62
58
request = session .post (service_url , allow_redirects = True ,
63
59
headers = headers , timeout = REQUEST_TIMEOUT , data = data )
64
60
65
61
if '<meta http-equiv="refresh"' in request .text :
66
62
has_refresh_statement = True
67
63
had_refresh_statement = True
68
64
print (local_translation ('TEXT_RESULT_NOT_READY' ).format (
69
- time_sleep ))
70
- time .sleep (time_sleep )
65
+ TIME_SLEEP ))
66
+ time .sleep (TIME_SLEEP )
71
67
72
68
if not had_refresh_statement :
73
- time .sleep (time_sleep )
69
+ time .sleep (TIME_SLEEP )
74
70
75
71
# hämta det faktiska resultatet
76
72
soup2 = BeautifulSoup (request .text , 'html.parser' )
@@ -86,92 +82,7 @@ def run_test(global_translation, lang_code, url):
86
82
#review_messages = ''
87
83
88
84
for result in results :
89
- review_messages = ''
90
- points_to_remove_for_current_result = 0.0
91
-
92
- header = result .find ("h3" )
93
- header_id = header .get ('id' )
94
- if header_id == 'what' or header_id == 'raw-headers' or header_id == 'server-location' or header_id == 'localstorage' or header_id == 'requests' :
95
- continue
96
-
97
- heading_rating = Rating (global_translation , REVIEW_SHOW_IMPROVEMENTS_ONLY )
98
-
99
- number_of_success = len (header .find_all ("span" , class_ = "success" ))
100
-
101
- # - alert
102
- number_of_alerts = len (header .find_all ("span" , class_ = "alert" ))
103
- points_to_remove_for_current_result += (number_of_alerts * 5.0 )
104
-
105
- # - warning
106
- number_of_warnings = len (header .find_all ("span" , class_ = "warning" ))
107
- points_to_remove_for_current_result += (number_of_warnings * 2.5 )
108
-
109
- number_of_sub_alerts = 0
110
- number_of_sub_warnings = 0
111
- divs = result .find_all ("div" )
112
-
113
- more_info = ''
114
- if len (divs ) > 0 :
115
- div = divs [0 ]
116
- # -- alert
117
- number_of_sub_alerts = len (div .find_all ("span" , class_ = "alert" ))
118
- points_to_remove_for_current_result += (
119
- number_of_sub_alerts * 0.5 )
120
- # -- warning
121
- number_of_sub_warnings = len (
122
- div .find_all ("span" , class_ = "warning" ))
123
- points_to_remove_for_current_result += (
124
- number_of_sub_warnings * 0.25 )
125
-
126
- paragraphs = result .find_all ("p" )
127
- if len (paragraphs ) > 0 :
128
- for paragraph_text in paragraphs [0 ].strings :
129
- more_info += re .sub (regex_allowed_chars , '' ,
130
- paragraph_text , 0 , re .MULTILINE ) + " "
131
- else :
132
- more_info = "!" + re .sub (regex_allowed_chars , '' ,
133
- result .text , 0 , re .MULTILINE )
134
- more_info = more_info .replace (" " , " " ).strip ()
135
-
136
- points_for_current_result = 5.0
137
-
138
- # only try to remove points if we have more then one
139
- if points_to_remove_for_current_result > 0.0 :
140
- points_for_current_result -= points_to_remove_for_current_result
141
- points -= points_to_remove_for_current_result
142
-
143
- if points_for_current_result < 1.0 :
144
- points_for_current_result = 1.0
145
-
146
- # add review info
147
- review_messages += '- ' + re .sub (regex_allowed_chars , '' ,
148
- header .text , 0 , re .MULTILINE ).strip ()
149
-
150
- if number_of_success > 0 and number_of_sub_alerts == 0 and number_of_sub_warnings == 0 :
151
- review_messages += local_translation ('TEXT_REVIEW_CATEGORY_VERY_GOOD' )
152
- elif number_of_alerts > 0 :
153
- review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_VERY_BAD' ).format (
154
- 0 )
155
- elif number_of_warnings > 0 :
156
- review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_BAD' ).format (
157
- 0 )
158
- elif number_of_sub_alerts > 0 and number_of_sub_warnings > 0 :
159
- review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_OK' ).format (
160
- number_of_sub_alerts , number_of_sub_warnings , 0 )
161
- elif number_of_sub_alerts > 0 :
162
- review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_OK' ).format (
163
- number_of_sub_alerts , number_of_sub_warnings , 0 )
164
- elif number_of_sub_warnings > 0 :
165
- review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_GOOD' ).format (
166
- number_of_sub_warnings , 0 )
167
- elif header_id == 'headers' or header_id == 'cookies' :
168
- review_messages += local_translation ('TEXT_REVIEW_CATEGORY_VERY_GOOD' )
169
- else :
170
- review_messages += ": " + more_info
171
-
172
- heading_rating .set_integrity_and_security (
173
- points_for_current_result , review_messages )
174
- rating += heading_rating
85
+ rating += rate_result (result , global_translation , local_translation )
175
86
176
87
points = rating .get_integrity_and_security ()
177
88
if points >= 5 :
@@ -192,7 +103,7 @@ def run_test(global_translation, lang_code, url):
192
103
result_title_beta = result_title .find_all ('div' , class_ = "beta" )
193
104
if len (result_title_beta ) > 0 :
194
105
for header_info in result_title_beta [0 ].strings :
195
- info = re .sub (regex_allowed_chars , '' ,
106
+ info = re .sub (REGEX_ALLOWED_CHARS , '' ,
196
107
header_info , 0 , re .MULTILINE ).strip ()
197
108
if info .startswith ('20' ):
198
109
review += local_translation ('TEXT_REVIEW_GENERATED' ).format (info )
@@ -206,3 +117,92 @@ def run_test(global_translation, lang_code, url):
206
117
datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )))
207
118
208
119
return (rating , return_dict )
120
+
121
+ def rate_result (result , global_translation , local_translation ):
122
+ heading_rating = Rating (global_translation , REVIEW_SHOW_IMPROVEMENTS_ONLY )
123
+
124
+ points = 5.0
125
+ review_messages = ''
126
+ points_to_remove_for_current_result = 0.0
127
+
128
+ header = result .find ("h3" )
129
+ header_id = header .get ('id' )
130
+
131
+ if header_id in ('what' , 'raw-headers' , 'server-location' , 'localstorage' , 'requests' ):
132
+ return heading_rating
133
+
134
+ number_of_success = len (header .find_all ("span" , class_ = "success" ))
135
+
136
+ # - alert
137
+ number_of_alerts = len (header .find_all ("span" , class_ = "alert" ))
138
+ points_to_remove_for_current_result += (number_of_alerts * 5.0 )
139
+
140
+ # - warning
141
+ number_of_warnings = len (header .find_all ("span" , class_ = "warning" ))
142
+ points_to_remove_for_current_result += (number_of_warnings * 2.5 )
143
+
144
+ number_of_sub_alerts = 0
145
+ number_of_sub_warnings = 0
146
+ divs = result .find_all ("div" )
147
+
148
+ more_info = ''
149
+ if len (divs ) > 0 :
150
+ div = divs [0 ]
151
+ # -- alert
152
+ number_of_sub_alerts = len (div .find_all ("span" , class_ = "alert" ))
153
+ points_to_remove_for_current_result += (
154
+ number_of_sub_alerts * 0.5 )
155
+ # -- warning
156
+ number_of_sub_warnings = len (
157
+ div .find_all ("span" , class_ = "warning" ))
158
+ points_to_remove_for_current_result += (
159
+ number_of_sub_warnings * 0.25 )
160
+
161
+ paragraphs = result .find_all ("p" )
162
+ if len (paragraphs ) > 0 :
163
+ for paragraph_text in paragraphs [0 ].strings :
164
+ more_info += re .sub (REGEX_ALLOWED_CHARS , '' ,
165
+ paragraph_text , 0 , re .MULTILINE ) + " "
166
+ else :
167
+ more_info = "!" + re .sub (REGEX_ALLOWED_CHARS , '' ,
168
+ result .text , 0 , re .MULTILINE )
169
+ more_info = more_info .replace (" " , " " ).strip ()
170
+
171
+ points_for_current_result = 5.0
172
+
173
+ # only try to remove points if we have more then one
174
+ if points_to_remove_for_current_result > 0.0 :
175
+ points_for_current_result -= points_to_remove_for_current_result
176
+ points -= points_to_remove_for_current_result
177
+
178
+ points_for_current_result = max (points_for_current_result , 1.0 )
179
+
180
+ # add review info
181
+ review_messages += '- ' + re .sub (REGEX_ALLOWED_CHARS , '' ,
182
+ header .text , 0 , re .MULTILINE ).strip ()
183
+
184
+ if number_of_success > 0 and number_of_sub_alerts == 0 and number_of_sub_warnings == 0 :
185
+ review_messages += local_translation ('TEXT_REVIEW_CATEGORY_VERY_GOOD' )
186
+ elif number_of_alerts > 0 :
187
+ review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_VERY_BAD' ).format (
188
+ 0 )
189
+ elif number_of_warnings > 0 :
190
+ review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_BAD' ).format (
191
+ 0 )
192
+ elif number_of_sub_alerts > 0 and number_of_sub_warnings > 0 :
193
+ review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_OK' ).format (
194
+ number_of_sub_alerts , number_of_sub_warnings , 0 )
195
+ elif number_of_sub_alerts > 0 :
196
+ review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_OK' ).format (
197
+ number_of_sub_alerts , number_of_sub_warnings , 0 )
198
+ elif number_of_sub_warnings > 0 :
199
+ review_messages += local_translation ('TEXT_REVIEW_CATEGORY_IS_GOOD' ).format (
200
+ number_of_sub_warnings , 0 )
201
+ elif header_id in ('headers' , 'cookies' ):
202
+ review_messages += local_translation ('TEXT_REVIEW_CATEGORY_VERY_GOOD' )
203
+ else :
204
+ review_messages += ": " + more_info
205
+
206
+ heading_rating .set_integrity_and_security (
207
+ points_for_current_result , review_messages )
208
+ return heading_rating
0 commit comments