1
0
Fork 0
nemubot/modules/networking/w3c.py

33 lines
1.2 KiB
Python
Raw Normal View History

2014-12-01 17:13:58 +00:00
import json
import urllib
from nemubot import __version__
from nemubot.exception import IMException
2015-10-13 22:17:02 +00:00
from nemubot.tools.web import getNormalizedURL
2014-12-01 17:13:58 +00:00
def validator(url):
"""Run the w3c validator on the given URL
Argument:
url -- the URL to validate
"""
2015-10-13 22:17:02 +00:00
o = urllib.parse.urlparse(getNormalizedURL(url), "http")
2014-12-01 17:13:58 +00:00
if o.netloc == "":
raise IMException("Indicate a valid URL!")
2014-12-01 17:13:58 +00:00
try:
req = urllib.request.Request("https://validator.w3.org/check?uri=%s&output=json" % (urllib.parse.quote(o.geturl())), headers={ 'User-Agent' : "Nemubot v%s" % __version__})
2014-12-01 17:13:58 +00:00
raw = urllib.request.urlopen(req, timeout=10)
except urllib.error.HTTPError as e:
raise IMException("HTTP error occurs: %s %s" % (e.code, e.reason))
2014-12-01 17:13:58 +00:00
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 IMException("Unexpected error on W3C servers" + (" (" + headers["X-W3C-Validator-Status"] + ")" if "X-W3C-Validator-Status" in headers else ""))
2014-12-01 17:13:58 +00:00
return headers, json.loads(raw.read().decode())