1
0
mirror of https://github.com/moparisthebest/xeps synced 2024-11-25 18:52:23 -05:00

checkdeadlinks: Make the script compatible both python2 and python3.

This commit is contained in:
Emmanuel Gil Peyrot 2016-10-03 23:11:15 +01:00
parent 62348c310b
commit 137e41fbba

View File

@ -36,31 +36,36 @@
A script for checking XEPs for dead links. A script for checking XEPs for dead links.
''' '''
from __future__ import print_function
from argparse import ArgumentParser from argparse import ArgumentParser
import sys import sys
import re import re
import urllib2
from xml.dom.minidom import parse from xml.dom.minidom import parse
try:
from urllib.request import Request, urlopen
except ImportError:
# We are on python2
from urllib2 import Request, urlopen
def is_dead(url): def is_dead(url):
if re.match("^(http|https)", url): if re.match("^(http|https)", url):
if verbose: if verbose:
print url + ' :', print(url + ' :', end=' ')
page = 0
try: try:
request = urllib2.Request(url) request = Request(url)
request.add_header('User-Agent', "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101") request.add_header('User-Agent', "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101")
opener = urllib2.build_opener() urlopen(request).read()
page = opener.open(request).read() except Exception as e:
except Exception, e:
reason = str(e) reason = str(e)
if verbose: if verbose:
print "XEP-" + xepnum + " - DEAD: " + url + " [" + reason + "]" print("XEP-" + xepnum + " - DEAD: " + url + " [" + reason + "]")
return True return True
else: else:
if verbose: if verbose:
print 'OK' print('OK')
return False return False
else: else:
return False return False
@ -81,7 +86,7 @@ def main():
thexep = parse(xepfile) thexep = parse(xepfile)
if verbose: if verbose:
print 'Checking XEP-' + xepnum + ':' print('Checking XEP-' + xepnum + ':')
urls = [link.getAttribute("url") for link in thexep.getElementsByTagName("link")] urls = [link.getAttribute("url") for link in thexep.getElementsByTagName("link")]
urls += [image.getAttribute("src") for image in thexep.getElementsByTagName("img")] urls += [image.getAttribute("src") for image in thexep.getElementsByTagName("img")]
@ -90,7 +95,7 @@ def main():
if deadlinks: if deadlinks:
for url in deadlinks: for url in deadlinks:
print url print(url)
sys.exit(1) sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":