No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

checkdeadlinks.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. # File: checkdeadlinks.py
  3. # Version: 0.1
  4. # Description: a script for checking XEPs for dead links
  5. # Last Modified: 2016-10-03
  6. # Author: Tobias Markmann (tm@ayena.de)
  7. # License: public domain
  8. # HowTo: ./checkdeadlinks.py --xep=xepnum
  9. ## LICENSE ##
  10. #
  11. # Copyright (c) 1999 - 2010 XMPP Standards Foundation
  12. #
  13. # Permission is hereby granted, free of charge, to any person obtaining a copy
  14. # of this software and associated documentation files (the "Software"), to deal
  15. # in the Software without restriction, including without limitation the rights
  16. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. # copies of the Software, and to permit persons to whom the Software is
  18. # furnished to do so, subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included in
  21. # all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. # THE SOFTWARE.
  30. #
  31. ## END LICENSE ##
  32. '''
  33. A script for checking XEPs for dead links.
  34. '''
  35. from __future__ import print_function
  36. from argparse import ArgumentParser
  37. import sys
  38. import re
  39. from xml.dom.minidom import parse
  40. try:
  41. from urllib.request import Request, urlopen
  42. except ImportError:
  43. # We are on python2
  44. from urllib2 import Request, urlopen
  45. def is_dead(url):
  46. if re.match("^(http|https)", url):
  47. if verbose:
  48. print(url + ' :', end=' ')
  49. try:
  50. request = Request(url)
  51. request.add_header('User-Agent', "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101")
  52. urlopen(request).read()
  53. except Exception as e:
  54. reason = str(e)
  55. if verbose:
  56. print("XEP-" + xepnum + " - DEAD: " + url + " [" + reason + "]")
  57. return True
  58. else:
  59. if verbose:
  60. print('OK')
  61. return False
  62. else:
  63. return False
  64. def get_deadlinks(xep, is_verbose=False):
  65. global xepnum
  66. xepnum = '%04d' % xep
  67. global verbose
  68. verbose = is_verbose
  69. xepfile = 'xep-' + xepnum + '.xml'
  70. thexep = parse(xepfile)
  71. urls = [link.getAttribute("url") for link in thexep.getElementsByTagName("link")]
  72. urls += [image.getAttribute("src") for image in thexep.getElementsByTagName("img")]
  73. if verbose:
  74. print('Checking XEP-%s (%d links):' % (xepnum, len(urls)))
  75. return [url for url in set(urls) if is_dead(url)]
  76. def main():
  77. parser = ArgumentParser(description=__doc__)
  78. parser.add_argument('-v', '--verbose', action='store_true', help='Enables more verbosity')
  79. parser.add_argument('-x', '--xep', type=int, help='Defines the number of the XEP to check')
  80. args = parser.parse_args()
  81. deadlinks = get_deadlinks(args.xep, args.verbose)
  82. if deadlinks:
  83. for url in deadlinks:
  84. print(url)
  85. sys.exit(1)
  86. if __name__ == "__main__":
  87. main()