urlcheck.py 1.6 KB

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