Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

validate_links.py 1.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. import httplib2
  3. import json
  4. import socket
  5. import sys
  6. def parse_links(filename):
  7. """Returns a list of links from JSON object"""
  8. data = json.load(open(filename))
  9. links = []
  10. for entry in data['entries']:
  11. link = entry['Link']
  12. https = True if link.startswith('https') else False
  13. x = {
  14. 'link': link,
  15. 'https': https,
  16. }
  17. links.append(x)
  18. return links
  19. def validate_links(links):
  20. """Checks each entry in JSON file for live link"""
  21. print('Validating {} links...'.format(len(links)))
  22. errors = []
  23. for each in links:
  24. link = each['link']
  25. h = httplib2.Http(disable_ssl_certificate_validation=True, timeout=5)
  26. try:
  27. resp = h.request(link, 'HEAD')
  28. code = int(resp[0]['status'])
  29. # check if status code is a client or server error
  30. if code >= 404:
  31. errors.append('{}: {}'.format(code, link))
  32. except TimeoutError:
  33. errors.append("TMO: " + link)
  34. except socket.error as socketerror:
  35. errors.append("SOC: {} : {}".format(socketerror, link))
  36. return errors
  37. if __name__ == "__main__":
  38. num_args = len(sys.argv)
  39. if num_args < 2:
  40. print("No .json file passed")
  41. sys.exit(1)
  42. errors = validate_links(parse_links(sys.argv[1]))
  43. if len(errors) > 0:
  44. for err in errors:
  45. print(err)
  46. sys.exit(1)