All pastes #915304 Raw Edit

Untitled

public text v1 · immutable
#915304 ·published 2008-02-23 18:07 UTC
rendered paste body
#!usr/bin/env python2-5
import os.path, re, HTMLParser, urllib, sys


# show the invalid (http validation concerned) files of a dir
def showInvalid(dir):
	htmlFiles = lsHTMLDir(dir)
	for i in htmlFiles:
		pathName = dir + '/' + i
		isValidHRef(pathName)
	posSubdirs = os.listdir(dir)
	for j in posSubdirs:
		currentPath = dir + '/' + j
		if os.path.isdir(currentPath):
			showInvalid(currentPath)

# Return list with html files' names of a directory
def lsHTMLDir(dir): 
	if not os.path.isdir(dir):
		print n," is not a valid directory."
	return filter(isHTML,os.listdir(dir))

# Whether it is an HTML file or not
def isHTML(filename): 
	if re.match(".+\.html",filename) == None:
		return False
	else:
		return re.match(".+\.html",filename).end() == len(filename)


# examine the hrefs of the html file and print their situation
def isValidHRef(file):
	while not len(hyperlinks) == 0:
		hyperlinks.pop()
        a = contents(file)
        b = MyHTMLParser()
	b.feed(a)
	for i in hyperlinks:
		if not isValidHTTP(i):
			print file,": ",i,"is not a valid URL"
		elif not urlWorks(i):
			print file,": ",i,"does not seem to be working"
		else:
			print file,": ",i,"looks good"
# return file's contents
def contents(filename):
	f=open(filename,'r')
	return f.read()

# whether it is a valid URL or not
def isValidHTTP(url): 
	if re.match("https?://[a-zA-Z0-9\-\+]+(\.[a-zA-Z0-9\-\+]+)+(/[a-zA-Z0-9\-\+\?\.\&]*)*",url) == None:
		return False
	else:	
		return re.match("https?://[a-zA-Z0-9\-\+]+(\.[a-zA-Z0-9\-\+]+)+(/[a-zA-Z0-9\-\+\?\.\&]*)*",url).end() == len (url)

# whether or not url works
def urlWorks(url):
	try:
		urllib.urlopen(url)
		state = True
	except IOError:
		state = False
	return state 	



hyperlinks = []

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):

    def handle_starttag(self, tag, attrs):
        #print "Encountered the beginning of a %s tag" % tag
	if tag == 'a':
		hyperlinks.append(attrs.pop(0)[1])

def main():
	showInvalid(sys.argv[1])