0000: 23 21 2f 75 73 72 2f 62 69 6e 2f 65 6e 76 20 70 #!/usr/bin/env p
0010: 79 74 68 6f 6e 33 2e 31 0a 0a 69 6d 70 6f 72 74 ython3.1..import
0020: 20 64 61 74 65 74 69 6d 65 2c 20 68 74 74 70 2e datetime, http.
0030: 63 6f 6f 6b 69 65 6a 61 72 2c 20 6f 70 74 70 61 cookiejar, optpa
0040: 72 73 65 2c 20 6f 73 2c 20 73 79 73 2c 20 73 68 rse, os, sys, sh
0050: 65 6c 76 65 2c 20 72 65 2c 20 75 72 6c 6c 69 62 elve, re, urllib
0060: 2e 72 65 71 75 65 73 74 0a 0a 70 61 72 73 65 72 .request..parser
0070: 20 3d 20 6f 70 74 70 61 72 73 65 2e 4f 70 74 69 = optparse.Opti
0080: 6f 6e 50 61 72 73 65 72 28 29 0a 70 61 72 73 65 onParser().parse
0090: 72 2e 61 64 64 5f 6f 70 74 69 6f 6e 28 27 2d 76 r.add_option('-v
00a0: 27 2c 20 27 2d 2d 76 65 72 62 6f 73 65 27 2c 20 ', '--verbose',
00b0: 61 63 74 69 6f 6e 20 3d 20 27 73 74 6f 72 65 5f action = 'store_
00c0: 74 72 75 65 27 2c 20 64 65 73 74 20 3d 20 27 76 true', dest = 'v
00d0: 65 72 62 6f 73 65 27 2c 20 68 65 6c 70 20 3d 20 erbose', help =
00e0: 27 74 75 72 6e 73 20 6f 6e 20 76 65 72 62 6f 73 'turns on verbos
00f0: 65 20 73 74 61 74 75 73 20 6e 6f 74 69 66 69 63 e status notific
0100: 61 74 69 6f 6e 73 27 2c 20 6d 65 74 61 76 61 72 ations', metavar
0110: 20 3d 20 27 62 6f 6f 6c 27 2c 20 64 65 66 61 75 = 'bool', defau
0120: 6c 74 20 3d 20 46 61 6c 73 65 29 0a 70 61 72 73 lt = False).pars
0130: 65 72 2e 61 64 64 5f 6f 70 74 69 6f 6e 28 27 2d er.add_option('-
0140: 64 27 2c 20 27 2d 2d 64 69 72 27 2c 20 61 63 74 d', '--dir', act
0150: 69 6f 6e 20 3d 20 27 73 74 6f 72 65 27 2c 20 64 ion = 'store', d
0160: 65 73 74 20 3d 20 27 64 69 72 27 2c 20 68 65 6c est = 'dir', hel
0170: 70 20 3d 20 27 73 70 65 63 69 66 79 20 64 69 72 p = 'specify dir
0180: 65 63 74 6f 72 79 20 77 68 65 72 65 20 74 68 65 ectory where the
0190: 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 62 65 files should be
01a0: 20 73 74 6f 72 65 64 27 2c 20 6d 65 74 61 76 61 stored', metava
01b0: 72 20 3d 20 27 73 74 72 69 6e 67 27 2c 20 64 65 r = 'string', de
01c0: 66 61 75 6c 74 20 3d 20 4e 6f 6e 65 29 0a 70 61 fault = None).pa
01d0: 72 73 65 72 2e 61 64 64 5f 6f 70 74 69 6f 6e 28 rser.add_option(
01e0: 27 2d 72 27 2c 20 27 2d 2d 72 6f 6f 74 27 2c 20 '-r', '--root',
01f0: 61 63 74 69 6f 6e 20 3d 20 27 73 74 6f 72 65 27 action = 'store'
0200: 2c 20 64 65 73 74 20 3d 20 27 72 6f 6f 74 27 2c , dest = 'root',
0210: 20 68 65 6c 70 20 3d 20 27 73 70 65 63 69 66 79 help = 'specify
0220: 20 61 20 73 69 74 65 20 66 72 6f 6d 20 77 68 69 a site from whi
0230: 63 68 20 64 61 74 61 20 73 68 6f 75 6c 64 20 62 ch data should b
0240: 65 20 6d 69 72 72 6f 72 65 64 27 2c 20 6d 65 74 e mirrored', met
0250: 61 76 61 72 20 3d 20 27 73 74 72 69 6e 67 27 2c avar = 'string',
0260: 20 64 65 66 61 75 6c 74 20 3d 20 4e 6f 6e 65 29 default = None)
0270: 0a 70 61 72 73 65 72 2e 61 64 64 5f 6f 70 74 69 .parser.add_opti
0280: 6f 6e 28 27 2d 6c 27 2c 20 27 2d 2d 6c 6f 67 27 on('-l', '--log'
0290: 2c 20 61 63 74 69 6f 6e 20 3d 20 27 73 74 6f 72 , action = 'stor
02a0: 65 27 2c 20 64 65 73 74 20 3d 20 27 6c 6f 67 27 e', dest = 'log'
02b0: 2c 20 68 65 6c 70 20 3d 20 27 73 70 65 63 69 66 , help = 'specif
02c0: 79 20 61 20 6c 6f 67 20 66 69 6c 65 20 74 6f 20 y a log file to
02d0: 70 72 6f 63 65 73 73 27 2c 20 6d 65 74 61 76 61 process', metava
02e0: 72 20 3d 20 27 73 74 72 69 6e 67 27 2c 20 64 65 r = 'string', de
02f0: 66 61 75 6c 74 20 3d 20 4e 6f 6e 65 29 0a 28 6f fault = None).(o
0300: 70 74 69 6f 6e 73 2c 20 61 72 67 73 29 20 3d 20 ptions, args) =
0310: 70 61 72 73 65 72 2e 70 61 72 73 65 5f 61 72 67 parser.parse_arg
0320: 73 28 29 0a 0a 69 66 20 6e 6f 74 20 6f 70 74 69 s()..if not opti
0330: 6f 6e 73 2e 64 69 72 3a 0a 09 70 72 69 6e 74 28 ons.dir:..print(
0340: 27 44 69 72 65 63 74 6f 72 79 20 6e 6f 74 20 73 'Directory not s
0350: 70 65 63 69 66 69 65 64 27 29 0a 09 65 78 69 74 pecified')..exit
0360: 28 31 29 0a 0a 69 66 20 6e 6f 74 20 6f 70 74 69 (1)..if not opti
0370: 6f 6e 73 2e 72 6f 6f 74 3a 0a 09 70 72 69 6e 74 ons.root:..print
0380: 28 27 53 65 72 76 65 72 20 6e 6f 74 20 73 70 65 ('Server not spe
0390: 63 69 66 69 65 64 27 29 0a 09 65 78 69 74 28 31 cified')..exit(1
03a0: 29 0a 0a 69 66 20 6e 6f 74 20 6f 70 74 69 6f 6e )..if not option
03b0: 73 2e 6c 6f 67 3a 0a 09 70 72 69 6e 74 28 27 4c s.log:..print('L
03c0: 6f 67 20 66 69 6c 65 20 6e 6f 74 20 73 70 65 63 og file not spec
03d0: 69 66 69 65 64 27 29 0a 09 65 78 69 74 28 31 29 ified')..exit(1)
03e0: 0a 0a 69 66 20 6e 6f 74 20 6f 73 2e 61 63 63 65 ..if not os.acce
03f0: 73 73 28 6f 70 74 69 6f 6e 73 2e 6c 6f 67 2c 20 ss(options.log,
0400: 6f 73 2e 52 5f 4f 4b 29 3a 0a 09 70 72 69 6e 74 os.R_OK):..print
0410: 28 27 4c 6f 67 20 66 69 6c 65 20 75 6e 72 65 61 ('Log file unrea
0420: 64 61 62 6c 65 27 29 0a 09 65 78 69 74 28 31 29 dable')..exit(1)
0430: 0a 0a 23 20 74 68 69 73 20 69 73 20 66 69 6c 65 ..# this is file
0440: 20 69 6e 64 65 78 20 2d 20 65 76 65 72 79 74 68 index - everyth
0450: 69 6e 67 20 69 73 20 73 74 6f 72 65 64 20 69 6e ing is stored in
0460: 20 74 68 69 73 20 66 69 6c 65 0a 69 6e 64 65 78 this file.index
0470: 20 3d 20 73 68 65 6c 76 65 2e 6f 70 65 6e 28 6f = shelve.open(o
0480: 70 74 69 6f 6e 73 2e 64 69 72 20 2b 20 27 2f 2e ptions.dir + '/.
0490: 69 6e 64 65 78 27 29 0a 64 65 73 63 5f 66 69 65 index').desc_fie
04a0: 6c 64 73 20 3d 20 28 27 43 6f 6e 74 65 6e 74 2d lds = ('Content-
04b0: 4c 65 6e 67 74 68 27 2c 20 27 45 54 61 67 27 2c Length', 'ETag',
04c0: 20 27 50 72 61 67 6d 61 27 2c 20 27 4c 61 73 74 'Pragma', 'Last
04d0: 2d 4d 6f 64 69 66 69 65 64 27 29 0a 69 67 6e 6f -Modified').igno
04e0: 72 65 5f 66 69 65 6c 64 73 20 3d 20 28 27 41 63 re_fields = ('Ac
04f0: 63 65 70 74 2d 52 61 6e 67 65 73 27 2c 20 27 41 cept-Ranges', 'A
0500: 67 65 27 2c 20 27 43 61 63 68 65 2d 43 6f 6e 74 ge', 'Cache-Cont
0510: 72 6f 6c 27 2c 20 27 43 6f 6e 6e 65 63 74 69 6f rol', 'Connectio
0520: 6e 27 2c 20 27 43 6f 6e 74 65 6e 74 2d 54 79 70 n', 'Content-Typ
0530: 65 27 2c 20 27 44 61 74 65 27 2c 20 27 45 78 70 e', 'Date', 'Exp
0540: 69 72 65 73 27 2c 20 27 53 65 72 76 65 72 27 2c ires', 'Server',
0550: 20 27 56 69 61 27 2c 20 27 58 2d 43 61 63 68 65 'Via', 'X-Cache
0560: 27 2c 20 27 58 2d 43 61 63 68 65 2d 4c 6f 6f 6b ', 'X-Cache-Look
0570: 75 70 27 29 0a 0a 62 6c 6f 63 6b 5f 73 69 7a 65 up')..block_size
0580: 20 3d 20 33 32 37 36 38 0a 0a 77 68 69 6c 65 20 = 32768..while
0590: 54 72 75 65 3a 0a 09 75 6e 63 68 65 63 6b 65 64 True:..unchecked
05a0: 5f 66 69 6c 65 73 20 3d 20 73 65 74 28 29 0a 09 _files = set()..
05b0: 63 68 65 63 6b 65 64 5f 66 69 6c 65 73 20 3d 20 checked_files =
05c0: 30 0a 0a 09 23 20 72 65 61 64 69 6e 67 20 6c 6f 0...# reading lo
05d0: 67 20 61 6e 64 20 73 74 6f 72 69 6e 67 20 66 6f g and storing fo
05e0: 75 6e 64 20 75 72 6c 73 20 66 6f 72 20 70 72 6f und urls for pro
05f0: 63 65 73 73 69 6e 67 0a 09 23 20 63 68 65 63 6b cessing..# check
0600: 20 66 69 6c 65 20 6d 74 69 6d 65 20 58 58 58 0a file mtime XXX.
0610: 09 77 69 74 68 20 6f 70 65 6e 28 6f 70 74 69 6f .with open(optio
0620: 6e 73 2e 6c 6f 67 2c 20 27 72 27 29 20 61 73 20 ns.log, 'r') as
0630: 6c 6f 67 5f 66 69 6c 65 3a 0a 09 09 6c 6f 67 5f log_file:...log_
0640: 6c 69 6e 65 20 3d 20 72 65 2e 63 6f 6d 70 69 6c line = re.compil
0650: 65 28 27 5e 5b 5e 20 5d 2b 20 2d 20 2d 20 5c 5b e('^[^ ]+ - - \[
0660: 2e 2a 5d 20 22 28 47 45 54 7c 48 45 41 44 29 20 .*] "(GET|HEAD)
0670: 28 2e 2a 3f 29 28 5c 3f 2e 2a 29 3f 20 48 54 54 (.*?)(\?.*)? HTT
0680: 50 2f 31 2e 31 22 20 28 5c 64 2b 29 20 5c 64 2b P/1.1" (\d+) \d+
0690: 20 22 28 2e 2a 29 22 20 22 28 2e 2a 29 22 24 27 "(.*)" "(.*)"$'
06a0: 29 0a 09 09 66 6f 72 20 6c 69 6e 65 20 69 6e 20 )...for line in
06b0: 6c 6f 67 5f 66 69 6c 65 3a 0a 09 09 09 74 68 69 log_file:....thi
06c0: 73 5f 6c 69 6e 65 20 3d 20 6c 6f 67 5f 6c 69 6e s_line = log_lin
06d0: 65 2e 6d 61 74 63 68 28 6c 69 6e 65 2e 73 74 72 e.match(line.str
06e0: 69 70 28 29 29 0a 09 09 09 69 66 20 74 68 69 73 ip())....if this
06f0: 5f 6c 69 6e 65 3a 0a 09 09 09 09 75 6e 63 68 65 _line:.....unche
0700: 63 6b 65 64 5f 66 69 6c 65 73 2e 61 64 64 28 74 cked_files.add(t
0710: 68 69 73 5f 6c 69 6e 65 2e 67 72 6f 75 70 28 32 his_line.group(2
0720: 29 29 0a 0a 09 66 6f 72 20 75 72 6c 20 69 6e 20 ))...for url in
0730: 75 6e 63 68 65 63 6b 65 64 5f 66 69 6c 65 73 3a unchecked_files:
0740: 0a 0a 09 09 23 20 63 72 65 61 74 69 6e 67 20 65 ....# creating e
0750: 6d 70 74 79 20 70 6c 61 63 65 68 6f 6c 64 65 72 mpty placeholder
0760: 20 69 6e 20 69 6e 64 65 78 0a 09 09 69 66 20 6e in index...if n
0770: 6f 74 20 75 72 6c 20 69 6e 20 69 6e 64 65 78 3a ot url in index:
0780: 0a 09 09 09 69 6e 64 65 78 5b 75 72 6c 5d 20 3d ....index[url] =
0790: 20 7b 7d 0a 09 09 72 65 6c 6f 61 64 20 3d 20 46 {}...reload = F
07a0: 61 6c 73 65 0a 0a 09 09 23 20 63 72 65 61 74 69 alse....# creati
07b0: 6e 67 20 66 69 6c 65 20 6e 61 6d 65 20 66 72 6f ng file name fro
07c0: 6d 20 75 72 6c 0a 09 09 66 69 6c 65 5f 6e 61 6d m url...file_nam
07d0: 65 20 3d 20 6f 70 74 69 6f 6e 73 2e 64 69 72 20 e = options.dir
07e0: 2b 20 72 65 2e 63 6f 6d 70 69 6c 65 28 27 25 32 + re.compile('%2
07f0: 30 27 29 2e 73 75 62 28 27 20 27 2c 20 75 72 6c 0').sub(' ', url
0800: 29 0a 09 09 70 72 69 6e 74 28 27 43 68 65 63 6b )...print('Check
0810: 69 6e 67 20 66 69 6c 65 3a 27 2c 20 75 72 6c 29 ing file:', url)
0820: 0a 0a 09 09 23 20 66 6f 72 63 69 62 6c 79 20 63 ....# forcibly c
0830: 68 65 63 6b 69 6e 67 20 66 69 6c 65 20 69 66 20 hecking file if
0840: 6e 6f 20 66 69 6c 65 20 70 72 65 73 65 6e 74 0a no file present.
0850: 09 09 69 66 20 6e 6f 74 20 6f 73 2e 61 63 63 65 ..if not os.acce
0860: 73 73 28 66 69 6c 65 5f 6e 61 6d 65 2c 20 6f 73 ss(file_name, os
0870: 2e 52 5f 4f 4b 29 3a 0a 09 09 09 70 72 69 6e 74 .R_OK):....print
0880: 28 27 46 69 6c 65 20 6e 6f 74 20 66 6f 75 6e 64 ('File not found
0890: 20 6f 72 20 69 6e 61 63 63 65 73 73 69 62 6c 65 or inaccessible
08a0: 2e 27 29 0a 09 09 09 72 65 6c 6f 61 64 20 3d 20 .')....reload =
08b0: 54 72 75 65 0a 0a 09 09 23 20 66 6f 72 63 69 62 True....# forcib
08c0: 6c 79 20 63 68 65 63 6b 69 6e 67 20 66 69 6c 65 ly checking file
08d0: 20 69 66 20 66 69 6c 65 20 73 69 7a 65 20 64 6f if file size do
08e0: 65 73 6e 27 74 20 6d 61 74 63 68 20 77 69 74 68 esn't match with
08f0: 20 69 6e 64 65 78 20 64 61 74 61 0a 09 09 65 6c index data...el
0900: 69 66 20 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 if 'Content-Leng
0910: 74 68 27 20 69 6e 20 69 6e 64 65 78 5b 75 72 6c th' in index[url
0920: 5d 20 61 6e 64 20 6f 73 2e 73 74 61 74 28 66 69 ] and os.stat(fi
0930: 6c 65 5f 6e 61 6d 65 29 2e 73 74 5f 73 69 7a 65 le_name).st_size
0940: 20 21 3d 20 69 6e 74 28 69 6e 64 65 78 5b 75 72 != int(index[ur
0950: 6c 5d 5b 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 l]['Content-Leng
0960: 74 68 27 5d 29 3a 0a 09 09 09 70 72 69 6e 74 28 th']):....print(
0970: 27 46 69 6c 65 20 73 69 7a 65 20 69 73 27 2c 20 'File size is',
0980: 6f 73 2e 73 74 61 74 28 66 69 6c 65 5f 6e 61 6d os.stat(file_nam
0990: 65 29 2e 73 74 5f 73 69 7a 65 2c 20 27 61 6e 64 e).st_size, 'and
09a0: 20 73 74 6f 72 65 64 20 66 69 6c 65 20 73 69 7a stored file siz
09b0: 65 20 69 73 27 2c 20 69 6e 64 65 78 5b 75 72 6c e is', index[url
09c0: 5d 5b 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 ]['Content-Lengt
09d0: 68 27 5d 29 0a 09 09 09 72 65 6c 6f 61 64 20 3d h'])....reload =
09e0: 20 54 72 75 65 0a 0a 09 09 23 20 66 6f 72 63 69 True....# forci
09f0: 62 6c 79 20 63 68 65 63 6b 69 6e 67 20 66 69 6c bly checking fil
0a00: 65 20 69 66 20 69 6e 64 65 78 20 68 6f 64 73 20 e if index hods
0a10: 50 72 61 67 6d 61 20 68 65 61 64 65 72 0a 09 09 Pragma header...
0a20: 69 66 20 27 50 72 61 67 6d 61 27 20 69 6e 20 69 if 'Pragma' in i
0a30: 6e 64 65 78 5b 75 72 6c 5d 20 61 6e 64 20 69 6e ndex[url] and in
0a40: 64 65 78 5b 75 72 6c 5d 5b 27 50 72 61 67 6d 61 dex[url]['Pragma
0a50: 27 5d 20 3d 3d 20 27 6e 6f 2d 63 61 63 68 65 27 '] == 'no-cache'
0a60: 3a 0a 09 09 09 70 72 69 6e 74 28 27 50 72 61 67 :....print('Prag
0a70: 6d 61 20 6f 6e 3a 20 72 65 63 68 65 63 6b 20 69 ma on: recheck i
0a80: 6d 69 6e 65 6e 74 2e 27 29 0a 09 09 09 72 65 6c minent.')....rel
0a90: 6f 61 64 20 3d 20 54 72 75 65 0a 0a 09 09 23 20 oad = True....#
0aa0: 73 6b 69 70 70 69 6e 67 20 66 69 6c 65 20 70 72 skipping file pr
0ab0: 6f 63 65 73 73 69 6e 67 20 69 66 20 74 68 65 72 ocessing if ther
0ac0: 65 27 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 e's no need to r
0ad0: 65 63 68 65 63 6b 20 69 74 20 61 6e 64 20 77 65 echeck it and we
0ae0: 20 68 61 76 65 20 63 68 65 63 6b 65 64 20 69 74 have checked it
0af0: 20 61 74 20 6c 65 61 73 74 20 34 20 68 6f 75 72 at least 4 hour
0b00: 73 20 61 67 6f 0a 09 09 69 66 20 6e 6f 74 20 72 s ago...if not r
0b10: 65 6c 6f 61 64 20 61 6e 64 20 27 5f 5f 74 69 6d eload and '__tim
0b20: 65 5f 5f 27 20 69 6e 20 69 6e 64 65 78 5b 75 72 e__' in index[ur
0b30: 6c 5d 20 61 6e 64 20 28 64 61 74 65 74 69 6d 65 l] and (datetime
0b40: 2e 64 61 74 65 74 69 6d 65 2e 6e 6f 77 28 29 20 .datetime.now()
0b50: 2d 20 64 61 74 65 74 69 6d 65 2e 74 69 6d 65 64 - datetime.timed
0b60: 65 6c 74 61 28 68 6f 75 72 73 20 3d 20 34 29 20 elta(hours = 4)
0b70: 2d 20 69 6e 64 65 78 5b 75 72 6c 5d 5b 27 5f 5f - index[url]['__
0b80: 74 69 6d 65 5f 5f 27 5d 29 2e 64 61 79 73 20 3c time__']).days <
0b90: 20 30 3a 0a 09 09 09 63 6f 6e 74 69 6e 75 65 0a 0:....continue.
0ba0: 09 09 74 72 79 3a 0a 09 09 09 70 72 69 6e 74 28 ..try:....print(
0bb0: 27 43 6f 6e 74 61 63 74 69 6e 67 20 77 65 62 73 'Contacting webs
0bc0: 69 74 65 2e 27 29 0a 09 09 09 77 69 74 68 20 75 ite.')....with u
0bd0: 72 6c 6c 69 62 2e 72 65 71 75 65 73 74 2e 75 72 rllib.request.ur
0be0: 6c 6f 70 65 6e 28 6f 70 74 69 6f 6e 73 2e 72 6f lopen(options.ro
0bf0: 6f 74 20 2b 20 75 72 6c 29 20 61 73 20 73 6f 75 ot + url) as sou
0c00: 72 63 65 3a 0a 09 09 09 09 6e 65 77 5f 68 65 61 rce:.....new_hea
0c10: 64 65 72 73 20 3d 20 7b 7d 0a 09 09 09 09 68 65 ders = {}.....he
0c20: 61 64 65 72 73 20 3d 20 73 6f 75 72 63 65 2e 69 aders = source.i
0c30: 6e 66 6f 28 29 0a 0a 09 09 09 09 23 20 73 74 72 nfo()......# str
0c40: 69 70 70 69 6e 67 20 75 6e 6e 65 65 64 65 64 20 ipping unneeded
0c50: 68 65 61 64 65 72 73 20 28 58 58 58 20 6d 61 6b headers (XXX mak
0c60: 65 20 74 68 69 73 20 69 6e 70 6c 61 63 65 3f 29 e this inplace?)
0c70: 0a 09 09 09 09 66 6f 72 20 68 65 61 64 65 72 20 .....for header
0c80: 69 6e 20 68 65 61 64 65 72 73 3a 0a 09 09 09 09 in headers:.....
0c90: 09 69 66 20 68 65 61 64 65 72 20 69 6e 20 64 65 .if header in de
0ca0: 73 63 5f 66 69 65 6c 64 73 3a 0a 09 09 09 09 09 sc_fields:......
0cb0: 09 69 66 20 68 65 61 64 65 72 20 3d 3d 20 27 50 .if header == 'P
0cc0: 72 61 67 6d 61 27 20 61 6e 64 20 68 65 61 64 65 ragma' and heade
0cd0: 72 73 5b 68 65 61 64 65 72 5d 20 21 3d 20 27 6e rs[header] != 'n
0ce0: 6f 2d 63 61 63 68 65 27 3a 0a 09 09 09 09 09 09 o-cache':.......
0cf0: 09 70 72 69 6e 74 28 27 50 72 61 67 6d 61 3a 27 .print('Pragma:'
0d00: 2c 20 68 65 61 64 65 72 73 5b 68 65 61 64 65 72 , headers[header
0d10: 5d 29 0a 09 09 09 09 09 09 6e 65 77 5f 68 65 61 ]).......new_hea
0d20: 64 65 72 73 5b 68 65 61 64 65 72 5d 20 3d 20 68 ders[header] = h
0d30: 65 61 64 65 72 73 5b 68 65 61 64 65 72 5d 0a 09 eaders[header]..
0d40: 09 09 09 09 65 6c 69 66 20 6e 6f 74 20 68 65 61 ....elif not hea
0d50: 64 65 72 20 69 6e 20 69 67 6e 6f 72 65 5f 66 69 der in ignore_fi
0d60: 65 6c 64 73 3a 0a 09 09 09 09 09 09 70 72 69 6e elds:.......prin
0d70: 74 28 27 55 6e 64 65 66 69 6e 65 64 20 68 65 61 t('Undefined hea
0d80: 64 65 72 20 22 27 2c 20 68 65 61 64 65 72 2c 20 der "', header,
0d90: 27 22 3a 20 27 2c 20 68 65 61 64 65 72 73 5b 68 '": ', headers[h
0da0: 65 61 64 65 72 5d 2c 20 73 65 70 3d 27 27 29 0a eader], sep='').
0db0: 0a 09 09 09 09 23 20 63 6f 6d 70 61 72 69 6e 67 .....# comparing
0dc0: 20 68 65 61 64 65 72 73 20 77 69 74 68 20 64 61 headers with da
0dd0: 74 61 20 66 6f 75 6e 64 20 69 6e 20 69 6e 64 65 ta found in inde
0de0: 78 0a 09 09 09 09 23 20 69 66 20 61 6e 79 20 68 x.....# if any h
0df0: 65 61 64 65 72 20 68 61 73 20 63 68 61 6e 67 65 eader has change
0e00: 64 20 28 65 78 63 65 70 74 20 50 72 61 67 6d 61 d (except Pragma
0e10: 29 20 66 69 6c 65 20 69 73 20 66 75 6c 6c 79 20 ) file is fully
0e20: 64 6f 77 6e 6c 6f 61 64 65 64 0a 09 09 09 09 23 downloaded.....#
0e30: 20 73 61 6d 65 20 69 66 20 77 65 20 67 65 74 20 same if we get
0e40: 6d 6f 72 65 20 6f 72 20 6c 65 73 73 20 68 65 61 more or less hea
0e50: 64 65 72 73 0a 09 09 09 09 6f 6c 64 5f 6b 65 79 ders.....old_key
0e60: 73 20 3d 20 73 65 74 28 69 6e 64 65 78 5b 75 72 s = set(index[ur
0e70: 6c 5d 2e 6b 65 79 73 28 29 29 0a 09 09 09 09 6f l].keys()).....o
0e80: 6c 64 5f 6b 65 79 73 2e 64 69 73 63 61 72 64 28 ld_keys.discard(
0e90: 27 5f 5f 74 69 6d 65 5f 5f 27 29 0a 09 09 09 09 '__time__').....
0ea0: 6f 6c 64 5f 6b 65 79 73 2e 64 69 73 63 61 72 64 old_keys.discard
0eb0: 28 27 50 72 61 67 6d 61 27 29 0a 09 09 09 09 6d ('Pragma').....m
0ec0: 6f 72 65 5f 6b 65 79 73 20 3d 20 73 65 74 28 6e ore_keys = set(n
0ed0: 65 77 5f 68 65 61 64 65 72 73 2e 6b 65 79 73 28 ew_headers.keys(
0ee0: 29 29 20 2d 20 6f 6c 64 5f 6b 65 79 73 0a 09 09 )) - old_keys...
0ef0: 09 09 6d 6f 72 65 5f 6b 65 79 73 2e 64 69 73 63 ..more_keys.disc
0f00: 61 72 64 28 27 50 72 61 67 6d 61 27 29 0a 09 09 ard('Pragma')...
0f10: 09 09 6c 65 73 73 5f 6b 65 79 73 20 3d 20 6f 6c ..less_keys = ol
0f20: 64 5f 6b 65 79 73 20 2d 20 73 65 74 28 6e 65 77 d_keys - set(new
0f30: 5f 68 65 61 64 65 72 73 2e 6b 65 79 73 28 29 29 _headers.keys())
0f40: 0a 09 09 09 09 69 66 20 6c 65 6e 28 6d 6f 72 65 .....if len(more
0f50: 5f 6b 65 79 73 29 20 3e 20 30 3a 0a 09 09 09 09 _keys) > 0:.....
0f60: 09 69 66 20 6c 65 6e 28 6f 6c 64 5f 6b 65 79 73 .if len(old_keys
0f70: 29 20 3d 3d 20 30 3a 0a 09 09 09 09 09 09 70 72 ) == 0:.......pr
0f80: 69 6e 74 28 27 4e 6f 20 64 61 74 61 20 6f 6e 20 int('No data on
0f90: 74 68 61 74 20 66 69 6c 65 20 79 65 74 2e 27 29 that file yet.')
0fa0: 0a 09 09 09 09 09 65 6c 73 65 3a 0a 09 09 09 09 ......else:.....
0fb0: 09 09 70 72 69 6e 74 28 27 4d 6f 72 65 20 68 65 ..print('More he
0fc0: 61 64 65 72 73 20 61 70 70 65 61 72 3a 27 2c 20 aders appear:',
0fd0: 6d 6f 72 65 5f 6b 65 79 73 29 0a 09 09 09 09 09 more_keys)......
0fe0: 72 65 6c 6f 61 64 20 3d 20 54 72 75 65 0a 09 09 reload = True...
0ff0: 09 09 65 6c 69 66 20 6c 65 6e 28 6c 65 73 73 5f ..elif len(less_
1000: 6b 65 79 73 29 20 3e 20 30 3a 0a 09 09 09 09 09 keys) > 0:......
1010: 70 72 69 6e 74 28 27 4c 65 73 73 20 68 65 61 64 print('Less head
1020: 65 72 73 20 61 70 70 65 61 72 3a 27 2c 20 6c 65 ers appear:', le
1030: 73 73 5f 6b 65 79 73 29 0a 09 09 09 09 09 72 65 ss_keys)......re
1040: 6c 6f 61 64 20 3d 20 54 72 75 65 0a 09 09 09 09 load = True.....
1050: 65 6c 73 65 3a 0a 09 09 09 09 09 66 6f 72 20 6b else:......for k
1060: 65 79 20 69 6e 20 69 6e 64 65 78 5b 75 72 6c 5d ey in index[url]
1070: 2e 6b 65 79 73 28 29 3a 0a 09 09 09 09 09 09 69 .keys():.......i
1080: 66 20 6b 65 79 20 6e 6f 74 20 69 6e 20 28 27 5f f key not in ('_
1090: 5f 74 69 6d 65 5f 5f 27 2c 20 27 50 72 61 67 6d _time__', 'Pragm
10a0: 61 27 29 20 61 6e 64 20 6e 6f 74 20 69 6e 64 65 a') and not inde
10b0: 78 5b 75 72 6c 5d 5b 6b 65 79 5d 20 3d 3d 20 6e x[url][key] == n
10c0: 65 77 5f 68 65 61 64 65 72 73 5b 6b 65 79 5d 3a ew_headers[key]:
10d0: 0a 09 09 09 09 09 09 09 70 72 69 6e 74 28 27 48 ........print('H
10e0: 65 61 64 65 72 20 22 27 2c 20 6b 65 79 2c 20 27 eader "', key, '
10f0: 22 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 5b " changed from [
1100: 27 2c 20 69 6e 64 65 78 5b 75 72 6c 5d 5b 6b 65 ', index[url][ke
1110: 79 5d 2c 20 27 5d 20 74 6f 20 5b 27 2c 20 6e 65 y], '] to [', ne
1120: 77 5f 68 65 61 64 65 72 73 5b 6b 65 79 5d 2c 20 w_headers[key],
1130: 27 5d 27 2c 20 73 65 70 3d 27 27 29 0a 09 09 09 ']', sep='')....
1140: 09 09 09 09 72 65 6c 6f 61 64 20 3d 20 54 72 75 ....reload = Tru
1150: 65 0a 0a 09 09 09 09 23 20 64 6f 77 6e 6c 6f 61 e......# downloa
1160: 64 69 6e 67 20 66 69 6c 65 0a 09 09 09 09 69 66 ding file.....if
1170: 20 72 65 6c 6f 61 64 3a 0a 09 09 09 09 09 69 66 reload:......if
1180: 20 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 'Content-Length
1190: 27 20 69 6e 20 68 65 61 64 65 72 73 3a 0a 09 09 ' in headers:...
11a0: 09 09 09 09 70 72 69 6e 74 28 27 44 6f 77 6e 6c ....print('Downl
11b0: 6f 61 64 69 6e 67 27 2c 20 68 65 61 64 65 72 73 oading', headers
11c0: 5b 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 ['Content-Length
11d0: 27 5d 2c 20 27 62 79 74 65 73 20 5b 27 2c 20 65 '], 'bytes [', e
11e0: 6e 64 3d 27 27 29 0a 09 09 09 09 09 65 6c 73 65 nd='')......else
11f0: 3a 0a 09 09 09 09 09 09 70 72 69 6e 74 28 27 44 :.......print('D
1200: 6f 77 6e 6c 6f 61 64 69 6e 67 20 5b 27 2c 20 65 ownloading [', e
1210: 6e 64 3d 27 27 29 0a 09 09 09 09 09 73 79 73 2e nd='')......sys.
1220: 73 74 64 6f 75 74 2e 66 6c 75 73 68 28 29 0a 0a stdout.flush()..
1230: 09 09 09 09 09 23 20 66 69 6c 65 20 69 73 20 63 .....# file is c
1240: 72 65 61 74 65 64 20 61 74 20 74 65 6d 70 6f 72 reated at tempor
1250: 61 72 79 20 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 ary location and
1260: 20 6d 6f 76 65 64 20 69 6e 20 70 6c 61 63 65 20 moved in place
1270: 6f 6e 6c 79 20 77 68 65 6e 20 64 6f 77 6e 6c 6f only when downlo
1280: 61 64 20 63 6f 6d 70 6c 65 74 65 73 0a 09 09 09 ad completes....
1290: 09 09 74 65 6d 70 5f 66 69 6c 65 20 3d 20 6f 70 ..temp_file = op
12a0: 65 6e 28 6f 70 74 69 6f 6e 73 2e 64 69 72 20 2b en(options.dir +
12b0: 20 27 2f 2e 74 6d 70 27 2c 20 27 77 62 27 29 0a '/.tmp', 'wb').
12c0: 09 09 09 09 09 62 75 66 66 65 72 20 3d 20 73 6f .....buffer = so
12d0: 75 72 63 65 2e 72 65 61 64 28 62 6c 6f 63 6b 5f urce.read(block_
12e0: 73 69 7a 65 29 0a 09 09 09 09 09 62 6c 6f 63 6b size)......block
12f0: 73 20 3d 20 30 0a 09 09 09 09 09 6d 65 67 73 20 s = 0......megs
1300: 3d 20 30 0a 09 09 09 09 09 77 68 69 6c 65 20 6c = 0......while l
1310: 65 6e 28 62 75 66 66 65 72 29 20 3e 20 30 3a 0a en(buffer) > 0:.
1320: 09 09 09 09 09 09 74 65 6d 70 5f 66 69 6c 65 2e ......temp_file.
1330: 77 72 69 74 65 28 62 75 66 66 65 72 29 0a 09 09 write(buffer)...
1340: 09 09 09 09 70 72 69 6e 74 28 27 2e 27 2c 20 65 ....print('.', e
1350: 6e 64 3d 27 27 29 0a 09 09 09 09 09 09 73 79 73 nd='').......sys
1360: 2e 73 74 64 6f 75 74 2e 66 6c 75 73 68 28 29 0a .stdout.flush().
1370: 09 09 09 09 09 09 62 75 66 66 65 72 20 3d 20 73 ......buffer = s
1380: 6f 75 72 63 65 2e 72 65 61 64 28 62 6c 6f 63 6b ource.read(block
1390: 5f 73 69 7a 65 29 0a 09 09 09 09 09 09 62 6c 6f _size).......blo
13a0: 63 6b 73 20 2b 3d 20 31 0a 09 09 09 09 09 09 69 cks += 1.......i
13b0: 66 20 62 6c 6f 63 6b 73 20 3e 20 31 30 32 34 2a f blocks > 1024*
13c0: 31 30 32 34 2f 62 6c 6f 63 6b 5f 73 69 7a 65 3a 1024/block_size:
13d0: 0a 09 09 09 09 09 09 09 62 6c 6f 63 6b 73 20 3d ........blocks =
13e0: 20 62 6c 6f 63 6b 73 20 2d 20 31 30 32 34 2a 31 blocks - 1024*1
13f0: 30 32 34 2f 62 6c 6f 63 6b 5f 73 69 7a 65 0a 09 024/block_size..
1400: 09 09 09 09 09 09 6d 65 67 73 20 2b 3d 20 31 0a ......megs += 1.
1410: 09 09 09 09 09 09 09 70 72 69 6e 74 28 27 7b 7d .......print('{}
1420: 4d 62 27 2e 66 6f 72 6d 61 74 28 6d 65 67 73 29 Mb'.format(megs)
1430: 2c 20 65 6e 64 3d 27 27 29 0a 09 09 09 09 09 74 , end='')......t
1440: 65 6d 70 5f 66 69 6c 65 2e 63 6c 6f 73 65 28 29 emp_file.close()
1450: 0a 09 09 09 09 09 70 72 69 6e 74 28 27 5d 27 29 ......print(']')
1460: 0a 09 09 09 09 09 6f 73 2e 72 65 6e 61 6d 65 73 ......os.renames
1470: 28 6f 70 74 69 6f 6e 73 2e 64 69 72 20 2b 20 27 (options.dir + '
1480: 2f 2e 74 6d 70 27 2c 20 66 69 6c 65 5f 6e 61 6d /.tmp', file_nam
1490: 65 29 0a 0a 09 09 09 09 63 68 65 63 6b 65 64 5f e)......checked_
14a0: 66 69 6c 65 73 20 2b 3d 20 31 0a 0a 09 09 09 09 files += 1......
14b0: 23 20 73 74 6f 72 69 6e 67 20 6e 65 77 20 74 69 # storing new ti
14c0: 6d 65 20 6d 61 72 6b 20 61 6e 64 20 73 74 6f 72 me mark and stor
14d0: 69 6e 67 20 6e 65 77 20 68 65 61 64 65 72 73 0a ing new headers.
14e0: 09 09 09 09 6e 65 77 5f 68 65 61 64 65 72 73 5b ....new_headers[
14f0: 27 5f 5f 74 69 6d 65 5f 5f 27 5d 20 3d 20 64 61 '__time__'] = da
1500: 74 65 74 69 6d 65 2e 64 61 74 65 74 69 6d 65 2e tetime.datetime.
1510: 6e 6f 77 28 29 0a 09 09 09 09 69 6e 64 65 78 5b now().....index[
1520: 75 72 6c 5d 20 3d 20 6e 65 77 5f 68 65 61 64 65 url] = new_heade
1530: 72 73 0a 09 09 09 09 69 6e 64 65 78 2e 73 79 6e rs.....index.syn
1540: 63 28 29 0a 0a 09 09 65 78 63 65 70 74 20 75 72 c()....except ur
1550: 6c 6c 69 62 2e 65 72 72 6f 72 2e 48 54 54 50 45 llib.error.HTTPE
1560: 72 72 6f 72 20 61 73 20 65 72 72 6f 72 3a 0a 09 rror as error:..
1570: 09 09 23 20 69 6e 20 63 61 73 65 20 6f 66 20 65 ..# in case of e
1580: 72 72 6f 72 20 77 65 20 64 6f 6e 27 74 20 6e 65 rror we don't ne
1590: 65 64 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e ed to do anythin
15a0: 67 20 61 63 74 75 61 6c 6c 79 2c 0a 09 09 09 23 g actually,....#
15b0: 20 69 66 20 66 69 6c 65 20 64 6f 77 6e 6c 6f 61 if file downloa
15c0: 64 20 73 74 61 6c 6c 73 20 6f 72 20 66 61 69 6c d stalls or fail
15d0: 73 20 74 68 65 20 66 69 6c 65 20 77 6f 75 6c 64 s the file would
15e0: 20 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 74 6f not be moved to
15f0: 20 69 74 27 73 20 6c 6f 63 61 74 69 6f 6e 0a 09 it's location..
1600: 09 09 70 72 69 6e 74 28 65 72 72 6f 72 29 0a 0a ..print(error)..
1610: 09 70 72 69 6e 74 28 27 5b 27 2c 20 6c 65 6e 28 .print('[', len(
1620: 75 6e 63 68 65 63 6b 65 64 5f 66 69 6c 65 73 29 unchecked_files)
1630: 2c 20 27 2f 27 2c 20 63 68 65 63 6b 65 64 5f 66 , '/', checked_f
1640: 69 6c 65 73 2c 20 27 5d 27 29 0a 0a 09 23 20 63 iles, ']')...# c
1650: 68 65 63 6b 69 6e 67 20 69 66 20 74 68 65 72 65 hecking if there
1660: 20 77 65 72 65 20 61 6e 79 20 66 69 6c 65 73 20 were any files
1670: 64 6f 77 6e 6c 6f 61 64 65 64 2c 20 69 66 20 79 downloaded, if y
1680: 65 73 20 2d 20 72 65 73 74 61 72 74 69 6e 67 20 es - restarting
1690: 73 65 71 75 65 6e 63 65 0a 09 69 66 20 63 68 65 sequence..if che
16a0: 63 6b 65 64 5f 66 69 6c 65 73 20 3d 3d 20 30 3a cked_files == 0:
16b0: 0a 09 09 62 72 65 61 6b 0a ...break.