08ae38b6ce 2010-06-25 c.kworr@b84a3: #!/usr/bin/env python3.1
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: import datetime, http.cookiejar, optparse, os, sys, shelve, re, urllib.request
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser = optparse.OptionParser()
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser.add_option('-v', '--verbose', action = 'store_true', dest = 'verbose', help = 'turns on verbose status notifications', metavar = 'bool', default = False)
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser.add_option('-d', '--dir', action = 'store', dest = 'dir', help = 'specify directory where the files should be stored', metavar = 'string', default = None)
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser.add_option('-r', '--root', action = 'store', dest = 'root', help = 'specify a site from which data should be mirrored', metavar = 'string', default = None)
08ae38b6ce 2010-06-25 c.kworr@b84a3: parser.add_option('-l', '--log', action = 'store', dest = 'log', help = 'specify a log file to process', metavar = 'string', default = None)
08ae38b6ce 2010-06-25 c.kworr@b84a3: (options, args) = parser.parse_args()
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: if not options.dir:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Directory not specified')
08ae38b6ce 2010-06-25 c.kworr@b84a3: exit(1)
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: if not options.root:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Server not specified')
08ae38b6ce 2010-06-25 c.kworr@b84a3: exit(1)
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: if not options.log:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Log file not specified')
08ae38b6ce 2010-06-25 c.kworr@b84a3: exit(1)
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: if not os.access(options.log, os.R_OK):
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Log file unreadable')
08ae38b6ce 2010-06-25 c.kworr@b84a3: exit(1)
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # this is file index - everything is stored in this file
08ae38b6ce 2010-06-25 c.kworr@b84a3: index = shelve.open(options.dir + '/.index')
08ae38b6ce 2010-06-25 c.kworr@b84a3: desc_fields = ('Content-Length', 'ETag', 'Pragma', 'Last-Modified')
08ae38b6ce 2010-06-25 c.kworr@b84a3: ignore_fields = ('Accept-Ranges', 'Age', 'Cache-Control', 'Connection', 'Content-Type', 'Date', 'Expires', 'Server', 'Via', 'X-Cache', 'X-Cache-Lookup')
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: while True:
08ae38b6ce 2010-06-25 c.kworr@b84a3: unchecked_files = set()
08ae38b6ce 2010-06-25 c.kworr@b84a3: checked_files = 0
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # reading log and storing found urls for processing
08ae38b6ce 2010-06-25 c.kworr@b84a3: # check file mtime XXX
08ae38b6ce 2010-06-25 c.kworr@b84a3: with open(options.log, 'r') as log_file:
08ae38b6ce 2010-06-25 c.kworr@b84a3: log_line = re.compile('^[^ ]+ - - \[.*] "GET (.*?)(\?.*)? HTTP/1.1" (\d+) \d+ "(.*)" "(.*)"$')
08ae38b6ce 2010-06-25 c.kworr@b84a3: for line in log_file:
08ae38b6ce 2010-06-25 c.kworr@b84a3: this_line = log_line.match(line.strip())
08ae38b6ce 2010-06-25 c.kworr@b84a3: if this_line:
08ae38b6ce 2010-06-25 c.kworr@b84a3: unchecked_files.add(this_line.group(1))
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: for url in unchecked_files:
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # creating empty placeholder in index
08ae38b6ce 2010-06-25 c.kworr@b84a3: if not url in index:
08ae38b6ce 2010-06-25 c.kworr@b84a3: index[url] = {}
08ae38b6ce 2010-06-25 c.kworr@b84a3: reload = False
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # creating file name from url
08ae38b6ce 2010-06-25 c.kworr@b84a3: file_name = options.dir + re.compile('%20').sub(' ', url)
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # forcibly checking file if no file present
08ae38b6ce 2010-06-25 c.kworr@b84a3: if not os.access(file_name, os.R_OK):
08ae38b6ce 2010-06-25 c.kworr@b84a3: reload = True
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # forcibly checking file if file size doesn't match with index data
08ae38b6ce 2010-06-25 c.kworr@b84a3: elif 'Content-Length' in index[url] and os.stat(file_name).st_size != int(index[url]['Content-Length']):
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('File size is', os.stat(file_name).st_size, 'and stored file size is', index[url]['Content-Length'])
08ae38b6ce 2010-06-25 c.kworr@b84a3: reload = True
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # forcibly checking file if index hods Pragma header
08ae38b6ce 2010-06-25 c.kworr@b84a3: if 'Pragma' in index[url] and index[url]['Pragma'] == 'no-cache':
08ae38b6ce 2010-06-25 c.kworr@b84a3: reload = True
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # skipping file processing if there's no need to recheck it and we have checked it at least 4 hours ago
08ae38b6ce 2010-06-25 c.kworr@b84a3: if not reload and '__time__' in index[url] and (datetime.datetime.now() - datetime.timedelta(hours = 4) - index[url]['__time__']).days < 0:
08ae38b6ce 2010-06-25 c.kworr@b84a3: continue
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Checking file:', url)
08ae38b6ce 2010-06-25 c.kworr@b84a3: try:
08ae38b6ce 2010-06-25 c.kworr@b84a3: with urllib.request.urlopen(options.root + url) as source:
08ae38b6ce 2010-06-25 c.kworr@b84a3: new_headers = {}
08ae38b6ce 2010-06-25 c.kworr@b84a3: headers = source.info()
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # stripping unneeded headers (XXX make this inplace?)
08ae38b6ce 2010-06-25 c.kworr@b84a3: for header in headers:
08ae38b6ce 2010-06-25 c.kworr@b84a3: if header in desc_fields:
08ae38b6ce 2010-06-25 c.kworr@b84a3: if header == 'Pragma' and headers[header] != 'no-cache':
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Pragma:', headers[header])
08ae38b6ce 2010-06-25 c.kworr@b84a3: new_headers[header] = headers[header]
08ae38b6ce 2010-06-25 c.kworr@b84a3: elif not header in ignore_fields:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Undefined header', header, ':', headers[header])
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # comparing headers with data found in index
08ae38b6ce 2010-06-25 c.kworr@b84a3: # if any header has changed (except Pragma) file is fully downloaded
08ae38b6ce 2010-06-25 c.kworr@b84a3: # same if we get more or less headers
08ae38b6ce 2010-06-25 c.kworr@b84a3: old_keys = set(index[url].keys())
08ae38b6ce 2010-06-25 c.kworr@b84a3: old_keys.discard('__time__')
08ae38b6ce 2010-06-25 c.kworr@b84a3: old_keys.discard('Pragma')
08ae38b6ce 2010-06-25 c.kworr@b84a3: more_keys = set(new_headers.keys()) - old_keys
08ae38b6ce 2010-06-25 c.kworr@b84a3: more_keys.discard('Pragma')
08ae38b6ce 2010-06-25 c.kworr@b84a3: less_keys = old_keys - set(new_headers.keys())
08ae38b6ce 2010-06-25 c.kworr@b84a3: if len(more_keys) > 0:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('More headers appear:', more_keys)
08ae38b6ce 2010-06-25 c.kworr@b84a3: reload = True
08ae38b6ce 2010-06-25 c.kworr@b84a3: elif len(less_keys) > 0:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Less headers appear:', less_keys)
08ae38b6ce 2010-06-25 c.kworr@b84a3: reload = True
08ae38b6ce 2010-06-25 c.kworr@b84a3: else:
08ae38b6ce 2010-06-25 c.kworr@b84a3: for key in index[url].keys():
08ae38b6ce 2010-06-25 c.kworr@b84a3: if key not in ('__time__', 'Pragma') and not index[url][key] == new_headers[key]:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Header', key, 'changed from', index[url][key], 'to', new_headers[key])
08ae38b6ce 2010-06-25 c.kworr@b84a3: reload = True
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # downloading file
08ae38b6ce 2010-06-25 c.kworr@b84a3: if reload:
08ae38b6ce 2010-06-25 c.kworr@b84a3: if 'Content-Length' in headers:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Downloading', headers['Content-Length'], 'bytes [', end='')
08ae38b6ce 2010-06-25 c.kworr@b84a3: else:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('Downloading [', end='')
08ae38b6ce 2010-06-25 c.kworr@b84a3: sys.stdout.flush()
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # file is created at temporary location and moved in place only when download completes
08ae38b6ce 2010-06-25 c.kworr@b84a3: temp_file = open(options.dir + '/.tmp', 'wb')
08ae38b6ce 2010-06-25 c.kworr@b84a3: buffer = source.read(4096)
08ae38b6ce 2010-06-25 c.kworr@b84a3: while len(buffer) > 0:
08ae38b6ce 2010-06-25 c.kworr@b84a3: temp_file.write(buffer)
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('.', end='')
08ae38b6ce 2010-06-25 c.kworr@b84a3: sys.stdout.flush()
08ae38b6ce 2010-06-25 c.kworr@b84a3: buffer = source.read(4096)
08ae38b6ce 2010-06-25 c.kworr@b84a3: temp_file.close()
08ae38b6ce 2010-06-25 c.kworr@b84a3: print(']')
08ae38b6ce 2010-06-25 c.kworr@b84a3: os.renames(options.dir + '/.tmp', file_name)
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: checked_files += 1
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # storing new time mark and storing new headers
08ae38b6ce 2010-06-25 c.kworr@b84a3: new_headers['__time__'] = datetime.datetime.now()
08ae38b6ce 2010-06-25 c.kworr@b84a3: index[url] = new_headers
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: except urllib.error.HTTPError as error:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # in case of error we don't need to do anything actually,
08ae38b6ce 2010-06-25 c.kworr@b84a3: # if file download stalls or fails the file would not be moved to it's location
08ae38b6ce 2010-06-25 c.kworr@b84a3: print(error)
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: print('[', len(unchecked_files), '/', checked_files, ']')
08ae38b6ce 2010-06-25 c.kworr@b84a3:
08ae38b6ce 2010-06-25 c.kworr@b84a3: # checking if there were any files downloaded, if yes - restarting sequence
08ae38b6ce 2010-06-25 c.kworr@b84a3: if checked_files == 0:
08ae38b6ce 2010-06-25 c.kworr@b84a3: break