1997-10-06 15:54:25 -03:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
|
|
|
"""A variant on webchecker that creates a mirror copy of a remote site."""
|
|
|
|
|
1998-02-21 16:08:39 -04:00
|
|
|
__version__ = "$Revision$"
|
1997-10-06 15:54:25 -03:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
import urllib
|
|
|
|
import getopt
|
|
|
|
|
|
|
|
import webchecker
|
1998-02-21 16:08:39 -04:00
|
|
|
|
|
|
|
# Extract real version number if necessary
|
|
|
|
if __version__[0] == '$':
|
|
|
|
_v = string.split(__version__)
|
|
|
|
if len(_v) == 3:
|
|
|
|
__version__ = _v[1]
|
1997-10-06 15:54:25 -03:00
|
|
|
|
|
|
|
def main():
|
1998-02-21 16:08:39 -04:00
|
|
|
verbose = webchecker.VERBOSE
|
1997-10-06 15:54:25 -03:00
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "qv")
|
|
|
|
except getopt.error, msg:
|
|
|
|
print msg
|
1998-02-21 16:08:39 -04:00
|
|
|
print "usage:", sys.argv[0], "[-qv] ... [rooturl] ..."
|
1997-10-06 15:54:25 -03:00
|
|
|
return 2
|
|
|
|
for o, a in opts:
|
|
|
|
if o == "-q":
|
1998-02-21 16:08:39 -04:00
|
|
|
verbose = 0
|
1997-10-06 15:54:25 -03:00
|
|
|
if o == "-v":
|
1998-02-21 16:08:39 -04:00
|
|
|
verbose = verbose + 1
|
|
|
|
c = Sucker()
|
|
|
|
c.setflags(verbose=verbose)
|
1997-10-06 15:54:25 -03:00
|
|
|
c.urlopener.addheaders = [
|
|
|
|
('User-agent', 'websucker/%s' % __version__),
|
|
|
|
]
|
|
|
|
for arg in args:
|
|
|
|
print "Adding root", arg
|
|
|
|
c.addroot(arg)
|
|
|
|
print "Run..."
|
|
|
|
c.run()
|
|
|
|
|
|
|
|
class Sucker(webchecker.Checker):
|
|
|
|
|
1998-02-21 16:08:39 -04:00
|
|
|
checkext = 0
|
|
|
|
|
|
|
|
def readhtml(self, url):
|
|
|
|
text = None
|
1997-10-06 15:54:25 -03:00
|
|
|
path = self.savefilename(url)
|
|
|
|
try:
|
|
|
|
f = open(path, "rb")
|
|
|
|
except IOError:
|
1998-02-21 16:08:39 -04:00
|
|
|
f = self.openpage(url)
|
|
|
|
if f:
|
|
|
|
info = f.info()
|
|
|
|
nurl = f.geturl()
|
|
|
|
if nurl != url:
|
|
|
|
url = nurl
|
|
|
|
path = self.savefilename(url)
|
|
|
|
text = f.read()
|
|
|
|
f.close()
|
|
|
|
self.savefile(text, path)
|
|
|
|
if not self.checkforhtml(info, url):
|
|
|
|
text = None
|
1997-10-06 15:54:25 -03:00
|
|
|
else:
|
1998-02-21 16:08:39 -04:00
|
|
|
if self.checkforhtml({}, url):
|
|
|
|
text = f.read()
|
|
|
|
f.close()
|
|
|
|
return text, url
|
1997-10-06 15:54:25 -03:00
|
|
|
|
|
|
|
def savefile(self, text, path):
|
|
|
|
dir, base = os.path.split(path)
|
|
|
|
makedirs(dir)
|
|
|
|
f = open(path, "wb")
|
|
|
|
f.write(text)
|
|
|
|
f.close()
|
|
|
|
print "saved", path
|
|
|
|
|
|
|
|
def savefilename(self, url):
|
|
|
|
type, rest = urllib.splittype(url)
|
|
|
|
host, path = urllib.splithost(rest)
|
|
|
|
while path[:1] == "/": path = path[1:]
|
|
|
|
user, host = urllib.splituser(host)
|
|
|
|
host, port = urllib.splitnport(host)
|
|
|
|
host = string.lower(host)
|
|
|
|
path = os.path.join(host, path)
|
|
|
|
if path[-1] == "/": path = path + "index.html"
|
|
|
|
if os.sep != "/":
|
|
|
|
path = string.join(string.split(path, "/"), os.sep)
|
|
|
|
return path
|
|
|
|
|
|
|
|
def makedirs(dir):
|
|
|
|
if not dir or os.path.exists(dir):
|
|
|
|
return
|
|
|
|
head, tail = os.path.split(dir)
|
|
|
|
if not tail:
|
|
|
|
print "Huh? Don't know how to make dir", dir
|
|
|
|
return
|
|
|
|
makedirs(head)
|
|
|
|
os.mkdir(dir, 0777)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main() or 0)
|