-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_validation
executable file
·219 lines (162 loc) · 8.15 KB
/
check_validation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
import urllib.request
import urllib.parse
import sys
import re
import lxml.html
# TODO: http://www.w3.org/2005/MWI/BPWG/Group/TaskForces/Checker/
# TODO: http://www.sidar.org/hera/
# TODO: YSlow
# TODO: Page Speed
# {{{ Function: retrieve_url_content ------------------------------------------
def retrieve_url_content(http_method, remote_url, params):
"""Retrieve the content of an URL that could be called in two ways (GET or
POST) sending eventually some data.
Keyword arguments:
http_method -- HTTP Method to use (GET or POST).
remote_url -- The remote URL that validates the URL.
params -- The param list, could be sent via GET or POST.
Return value:
a string, that contain the URL response."""
if (http_method == 'GET'):
data = urllib.request.urlopen(remote_url).read()
else:
data = urllib.request.urlopen(remote_url, params).read()
return data
# }}} -------------------------------------------------------------------------
# {{{ Function: validate ------------------------------------------------------
def validate(http_method, remote_url, params, page, match,
excepted_value='.*', match_reverse=False):
"""Validate an URL calling the defined web site validator,
passing some data to it.
Keyword arguments:
http_method -- HTTP Method to use (GET or POST).
remote_url -- The remote URL that validates the URL.
params -- The param list, could be sent via GET or POST.
page -- The current page that needs validation.
match -- The RegExp to match the "excepted_value" inside the HTML
response.
excepted_value -- The excepted value to consider an answer good or not.
match_reverse -- The flag that means "if the excepted value is not
matched". This behavious is useful to exclude from the
match, for example, an error string.
Return value:
a string, that contain the response of validation."""
page = urllib.request.quote(page)
params = params.replace('%25URI%25', page).replace('%URI%', page)
if (http_method == 'GET'):
remote_url = remote_url + '%s' % params
data = retrieve_url_content(http_method, remote_url, params)
tree = lxml.html.fromstring(data)
elements = tree.xpath(match)
text = ''.join(elements)
match = re.match(excepted_value, text, re.DOTALL)
status = (match != None)
if (match_reverse):
status = not status
if status:
return 'OK'
else:
return 'FAIL' + ' > Check this out to: ' + remote_url
# }}} -------------------------------------------------------------------------
# Main ------------------------------------------------------------------------
if __name__ == "__main__":
print('CHECK VALIDATION')
v = sys.argv[1]
###############################################################################
# Web Site Validation of: Homepage
###############################################################################
print('Validate W3C:')
url_param = 'uri=%URI%&charset=(detect automatically)&doctype=Inline'
url_param += '&group=0&user-agent=W3C_Validator/1.2'
url_param = urllib.parse.quote(url_param)
xpath = './/*[@id="form"]//*[text()="Result:"]/'
xpath += 'following-sibling::*[1]/text()'
res = validate('GET', 'http://validator.w3.org/check?', url_param, v,
xpath, '.*Passed.*')
print(res)
###############################################################################
# Web Site Validation of: CSS v3
###############################################################################
print('Validate CSS 3:')
url_param = 'uri=%URI%&profile=css3&usermedium=all&warning=2&vextwarning=&'
url_param += 'lang=en'
xpath = './/*[@id="results_container"]//*[local-name()="div"]/'
xpath += '*[local-name()="h3"]/text()'
res = validate('GET', 'http://jigsaw.w3.org/css-validator/validator?',
url_param, v, xpath,
'.*Congratulations! No Error Found\..*')
print(res)
###############################################################################
# Web Site Validation of: CSS v2.1
###############################################################################
print('Validate CSS 2.1:')
url_param = 'uri=%URI%&profile=css21&usermedium=all&warning=2&vextwarning='
url_param += '&lang=en'
xpath = './/*[@id="results_container"]//*[local-name()="div"]/'
xpath += '*[local-name()="h3"]/text()'
res = validate('GET', 'http://jigsaw.w3.org/css-validator/validator?',
url_param, v, xpath,
'.*Congratulations! No Error Found\..*')
print(res)
###############################################################################
# Web Site Validation of: CSS v2
###############################################################################
print('Validate CSS 2:')
url_param = 'uri=%URI%&profile=css2&usermedium=all&warning=2&vextwarning=&'
url_param += 'lang=en'
xpath = './/*[@id="results_container"]//*[local-name()="div"]/'
xpath += '*[local-name()="h3"]/text()'
res = validate('GET', 'http://jigsaw.w3.org/css-validator/validator?',
url_param, v, xpath,
'.*Congratulations! No Error Found\..*')
print(res)
###############################################################################
# Web Site Validation of: CSS vMobile
###############################################################################
print('Validate CSS Mobile:')
url_param = 'uri=%URI%&profile=mobile&usermedium=all&warning=2'
url_param += 'vextwarning=&lang=en'
xpath = './/*[@id="results_container"]//*[local-name()="div"]/'
xpath += '*[local-name()="h3"]/text()'
res = validate('GET', 'http://jigsaw.w3.org/css-validator/validator?',
url_param, v, xpath,
'.*Congratulations! No Error Found\..*')
print(res)
###############################################################################
# Web Site Validation of: Feed
###############################################################################
print('Validate Feed:')
url_param = urllib.parse.urlencode({'url': '%URI%'})
xpath = './/*[@id="main"]//*[local-name()="h2"]/text()'
res = validate('GET', 'http://validator.w3.org/feed/check.cgi?',
url_param, v, xpath, '.*Congratulations!.*')
print(res)
###############################################################################
# Web Site Validation of: HTTP Header
###############################################################################
print('Validate HTTP Headers:')
url_param = urllib.parse.urlencode({'uri': '%URI%'})
xpath = './/*[@class="bad msg"]/text()'
res = validate('GET', 'http://redbot.org/?', url_param, v, xpath, '.+',
True)
print(res)
###############################################################################
# Web Site Validation of: Semantics
###############################################################################
print('Validate Semantics:')
url_param = urllib.parse.urlencode({'url': '%URI%', 'html': ''})
xpath = './/*[@id="extracted-data-google"]/*[local-name()="div"][2]//text()'
res = validate('GET',
'http://www.google.com/webmasters/tools/richsnippets?',
url_param, v, xpath, '.*No data detected.*', True)
print(res)
###############################################################################
# Web Site Validation of: Links
###############################################################################
print('Validate Links:')
url_param = 'uri=%URI%&summary=on&hide_type=all&depth=&check=Check'
xpath = './/*[@id="main"]//text()'
res = validate('GET', 'http://validator.w3.org/checklink?', url_param,
v, xpath, '.*Valid anchors.*')
print(res)