[networking] Refactor module
This commit is contained in:
parent
bbd928c6fa
commit
f181d644b4
7 changed files with 390 additions and 301 deletions
30
modules/networking/w3c.py
Normal file
30
modules/networking/w3c.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
import json
|
||||
import urllib
|
||||
|
||||
def validator(url):
|
||||
"""Run the w3c validator on the given URL
|
||||
|
||||
Argument:
|
||||
url -- the URL to validate
|
||||
"""
|
||||
|
||||
o = urllib.parse.urlparse(url, "http")
|
||||
if o.netloc == "":
|
||||
o = urllib.parse.urlparse("http://" + url)
|
||||
if o.netloc == "":
|
||||
raise IRCException("Indiquer une URL valide !")
|
||||
|
||||
try:
|
||||
req = urllib.request.Request("http://validator.w3.org/check?uri=%s&output=json" % (urllib.parse.quote(o.geturl())), headers={ 'User-Agent' : "nemubot v3" })
|
||||
raw = urllib.request.urlopen(req, timeout=10)
|
||||
except urllib.error.HTTPError as e:
|
||||
raise IRCException("HTTP error occurs: %s %s" % (e.code, e.reason))
|
||||
|
||||
headers = dict()
|
||||
for Hname, Hval in raw.getheaders():
|
||||
headers[Hname] = Hval
|
||||
|
||||
if "X-W3C-Validator-Status" not in headers or (headers["X-W3C-Validator-Status"] != "Valid" and headers["X-W3C-Validator-Status"] != "Invalid"):
|
||||
raise IRCException("Unexpected error on W3C servers" + (" (" + headers["X-W3C-Validator-Status"] + ")" if "X-W3C-Validator-Status" in headers else ""))
|
||||
|
||||
return headers, json.loads(raw.read().decode())
|
Loading…
Add table
Add a link
Reference in a new issue