urlcheck.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from __future__ import print_function
  2. import sys
  3. import httplib2
  4. import config
  5. import urllist
  6. def validate_html5(url):
  7. http_client = httplib2.Http(None)
  8. status = "Failed"
  9. errors = -1
  10. warnings = -1
  11. urlrequest = config.W3C_VALIDATOR+url
  12. # pylint: disable=broad-except
  13. # we disable the broad-except because we want to actually catch all possible exceptions
  14. try:
  15. resp, _ = http_client.request(urlrequest, "HEAD")
  16. if resp['x-w3c-validator-status'] != "Abort":
  17. status = resp['x-w3c-validator-status']
  18. errors = int(resp['x-w3c-validator-errors'])
  19. warnings = int(resp['x-w3c-validator-warnings'])
  20. except Exception as exc:
  21. config.logger.warn("Failed validation call: %s", exc)
  22. return (status, errors, warnings)
  23. def print_validation(url):
  24. status, errors, warnings = validate_html5(url)
  25. config.logger.error("url %s is %s\terrors %s warnings %s (check at %s)", url, status, errors, warnings, config.W3C_VALIDATOR+url)
  26. def main():
  27. print("Testing %s with %s" % (config.TOASTER_BASEURL, config.W3C_VALIDATOR))
  28. if len(sys.argv) > 1:
  29. print_validation(sys.argv[1])
  30. else:
  31. for url in urllist.URLS:
  32. print_validation(config.TOASTER_BASEURL+url)
  33. if __name__ == "__main__":
  34. main()