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 09 09 72 65 6c 6f 61 64 20 3d 20 46 61 6c 73 ...reload = Fals
0750: 65 0a 09 09 72 65 63 68 65 63 6b 20 3d 20 46 61 e...recheck = Fa
0760: 6c 73 65 0a 09 09 70 72 69 6e 74 28 27 43 68 65 lse...print('Che
0770: 63 6b 69 6e 67 20 66 69 6c 65 3a 27 2c 20 75 72 cking file:', ur
0780: 6c 29 0a 0a 09 09 23 20 63 72 65 61 74 69 6e 67 l)....# creating
0790: 20 65 6d 70 74 79 20 70 6c 61 63 65 68 6f 6c 64 empty placehold
07a0: 65 72 20 69 6e 20 69 6e 64 65 78 0a 09 09 69 66 er in index...if
07b0: 20 6e 6f 74 20 75 72 6c 20 69 6e 20 69 6e 64 65 not url in inde
07c0: 78 3a 0a 09 09 09 70 72 69 6e 74 28 27 54 68 69 x:....print('Thi
07d0: 73 20 6f 6e 65 20 69 73 20 6e 65 77 2e 27 29 0a s one is new.').
07e0: 09 09 09 69 6e 64 65 78 5b 75 72 6c 5d 20 3d 20 ...index[url] =
07f0: 7b 7d 0a 09 09 09 72 65 6c 6f 61 64 20 3d 20 54 {}....reload = T
0800: 72 75 65 0a 0a 09 09 23 20 63 72 65 61 74 69 6e rue....# creatin
0810: 67 20 66 69 6c 65 20 6e 61 6d 65 20 66 72 6f 6d g file name from
0820: 20 75 72 6c 0a 09 09 66 69 6c 65 5f 6e 61 6d 65 url...file_name
0830: 20 3d 20 6f 70 74 69 6f 6e 73 2e 64 69 72 20 2b = options.dir +
0840: 20 72 65 2e 63 6f 6d 70 69 6c 65 28 27 25 32 30 re.compile('%20
0850: 27 29 2e 73 75 62 28 27 20 27 2c 20 75 72 6c 29 ').sub(' ', url)
0860: 0a 0a 09 09 23 20 66 6f 72 63 69 62 6c 79 20 63 ....# forcibly c
0870: 68 65 63 6b 69 6e 67 20 66 69 6c 65 20 69 66 20 hecking file if
0880: 6e 6f 20 66 69 6c 65 20 70 72 65 73 65 6e 74 0a no file present.
0890: 09 09 69 66 20 6e 6f 74 20 72 65 6c 6f 61 64 20 ..if not reload
08a0: 61 6e 64 20 6e 6f 74 20 6f 73 2e 61 63 63 65 73 and not os.acces
08b0: 73 28 66 69 6c 65 5f 6e 61 6d 65 2c 20 6f 73 2e s(file_name, os.
08c0: 52 5f 4f 4b 29 3a 0a 09 09 09 70 72 69 6e 74 28 R_OK):....print(
08d0: 27 46 69 6c 65 20 6e 6f 74 20 66 6f 75 6e 64 20 'File not found
08e0: 6f 72 20 69 6e 61 63 63 65 73 73 69 62 6c 65 2e or inaccessible.
08f0: 27 29 0a 09 09 09 72 65 6c 6f 61 64 20 3d 20 54 ')....reload = T
0900: 72 75 65 0a 0a 09 09 23 20 66 6f 72 63 69 62 6c rue....# forcibl
0910: 79 20 63 68 65 63 6b 69 6e 67 20 66 69 6c 65 20 y checking file
0920: 69 66 20 66 69 6c 65 20 73 69 7a 65 20 64 6f 65 if file size doe
0930: 73 6e 27 74 20 6d 61 74 63 68 20 77 69 74 68 20 sn't match with
0940: 69 6e 64 65 78 20 64 61 74 61 0a 09 09 65 6c 69 index data...eli
0950: 66 20 6e 6f 74 20 72 65 6c 6f 61 64 20 61 6e 64 f not reload and
0960: 20 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 'Content-Length
0970: 27 20 69 6e 20 69 6e 64 65 78 5b 75 72 6c 5d 20 ' in index[url]
0980: 61 6e 64 20 6f 73 2e 73 74 61 74 28 66 69 6c 65 and os.stat(file
0990: 5f 6e 61 6d 65 29 2e 73 74 5f 73 69 7a 65 20 21 _name).st_size !
09a0: 3d 20 69 6e 74 28 69 6e 64 65 78 5b 75 72 6c 5d = int(index[url]
09b0: 5b 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 ['Content-Length
09c0: 27 5d 29 3a 0a 09 09 09 70 72 69 6e 74 28 27 46 ']):....print('F
09d0: 69 6c 65 20 73 69 7a 65 20 69 73 20 27 2c 20 6f ile size is ', o
09e0: 73 2e 73 74 61 74 28 66 69 6c 65 5f 6e 61 6d 65 s.stat(file_name
09f0: 29 2e 73 74 5f 73 69 7a 65 2c 20 27 20 61 6e 64 ).st_size, ' and
0a00: 20 73 74 6f 72 65 64 20 66 69 6c 65 20 73 69 7a stored file siz
0a10: 65 20 69 73 20 27 2c 20 69 6e 64 65 78 5b 75 72 e is ', index[ur
0a20: 6c 5d 5b 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 l]['Content-Leng
0a30: 74 68 27 5d 2c 20 27 2e 27 2c 20 73 65 70 3d 27 th'], '.', sep='
0a40: 27 29 0a 09 09 09 72 65 6c 6f 61 64 20 3d 20 54 ')....reload = T
0a50: 72 75 65 0a 0a 09 09 23 20 66 6f 72 63 69 62 6c rue....# forcibl
0a60: 79 20 63 68 65 63 6b 69 6e 67 20 66 69 6c 65 20 y checking file
0a70: 69 66 20 69 6e 64 65 78 20 68 6f 64 73 20 50 72 if index hods Pr
0a80: 61 67 6d 61 20 68 65 61 64 65 72 0a 09 09 69 66 agma header...if
0a90: 20 6e 6f 74 20 72 65 6c 6f 61 64 20 61 6e 64 20 not reload and
0aa0: 27 50 72 61 67 6d 61 27 20 69 6e 20 69 6e 64 65 'Pragma' in inde
0ab0: 78 5b 75 72 6c 5d 20 61 6e 64 20 69 6e 64 65 78 x[url] and index
0ac0: 5b 75 72 6c 5d 5b 27 50 72 61 67 6d 61 27 5d 20 [url]['Pragma']
0ad0: 3d 3d 20 27 6e 6f 2d 63 61 63 68 65 27 3a 0a 09 == 'no-cache':..
0ae0: 09 09 70 72 69 6e 74 28 27 50 72 61 67 6d 61 20 ..print('Pragma
0af0: 6f 6e 3a 20 72 65 63 68 65 63 6b 20 69 6d 6d 69 on: recheck immi
0b00: 6e 65 6e 74 2e 27 29 0a 09 09 09 72 65 63 68 65 nent.')....reche
0b10: 63 6b 20 3d 20 54 72 75 65 0a 0a 09 09 23 20 73 ck = True....# s
0b20: 6b 69 70 70 69 6e 67 20 66 69 6c 65 20 70 72 6f kipping file pro
0b30: 63 65 73 73 69 6e 67 20 69 66 20 74 68 65 72 65 cessing if there
0b40: 27 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 's no need to re
0b50: 63 68 65 63 6b 20 69 74 20 61 6e 64 20 77 65 20 check it and we
0b60: 68 61 76 65 20 63 68 65 63 6b 65 64 20 69 74 20 have checked it
0b70: 61 74 20 6c 65 61 73 74 20 34 20 68 6f 75 72 73 at least 4 hours
0b80: 20 61 67 6f 0a 09 09 69 66 20 6e 6f 74 20 72 65 ago...if not re
0b90: 63 68 65 63 6b 20 61 6e 64 20 6e 6f 74 20 72 65 check and not re
0ba0: 6c 6f 61 64 20 61 6e 64 20 27 5f 5f 74 69 6d 65 load and '__time
0bb0: 5f 5f 27 20 69 6e 20 69 6e 64 65 78 5b 75 72 6c __' in index[url
0bc0: 5d 20 61 6e 64 20 28 64 61 74 65 74 69 6d 65 2e ] and (datetime.
0bd0: 64 61 74 65 74 69 6d 65 2e 6e 6f 77 28 29 20 2d datetime.now() -
0be0: 20 64 61 74 65 74 69 6d 65 2e 74 69 6d 65 64 65 datetime.timede
0bf0: 6c 74 61 28 68 6f 75 72 73 20 3d 20 34 29 20 2d lta(hours = 4) -
0c00: 20 69 6e 64 65 78 5b 75 72 6c 5d 5b 27 5f 5f 74 index[url]['__t
0c10: 69 6d 65 5f 5f 27 5d 29 2e 64 61 79 73 20 3c 20 ime__']).days <
0c20: 30 3a 0a 09 09 09 63 6f 6e 74 69 6e 75 65 0a 0a 0:....continue..
0c30: 09 09 74 72 79 3a 0a 09 09 09 77 69 74 68 20 75 ..try:....with u
0c40: 72 6c 6c 69 62 2e 72 65 71 75 65 73 74 2e 75 72 rllib.request.ur
0c50: 6c 6f 70 65 6e 28 6f 70 74 69 6f 6e 73 2e 72 6f lopen(options.ro
0c60: 6f 74 20 2b 20 75 72 6c 29 20 61 73 20 73 6f 75 ot + url) as sou
0c70: 72 63 65 3a 0a 09 09 09 09 6e 65 77 5f 68 65 61 rce:.....new_hea
0c80: 64 65 72 73 20 3d 20 7b 7d 0a 09 09 09 09 68 65 ders = {}.....he
0c90: 61 64 65 72 73 20 3d 20 73 6f 75 72 63 65 2e 69 aders = source.i
0ca0: 6e 66 6f 28 29 0a 0a 09 09 09 09 23 20 73 74 72 nfo()......# str
0cb0: 69 70 70 69 6e 67 20 75 6e 6e 65 65 64 65 64 20 ipping unneeded
0cc0: 68 65 61 64 65 72 73 20 28 58 58 58 20 6d 61 6b headers (XXX mak
0cd0: 65 20 74 68 69 73 20 69 6e 70 6c 61 63 65 3f 29 e this inplace?)
0ce0: 0a 09 09 09 09 66 6f 72 20 68 65 61 64 65 72 20 .....for header
0cf0: 69 6e 20 68 65 61 64 65 72 73 3a 0a 09 09 09 09 in headers:.....
0d00: 09 69 66 20 68 65 61 64 65 72 20 69 6e 20 64 65 .if header in de
0d10: 73 63 5f 66 69 65 6c 64 73 3a 0a 09 09 09 09 09 sc_fields:......
0d20: 09 69 66 20 68 65 61 64 65 72 20 3d 3d 20 27 50 .if header == 'P
0d30: 72 61 67 6d 61 27 20 61 6e 64 20 68 65 61 64 65 ragma' and heade
0d40: 72 73 5b 68 65 61 64 65 72 5d 20 21 3d 20 27 6e rs[header] != 'n
0d50: 6f 2d 63 61 63 68 65 27 3a 0a 09 09 09 09 09 09 o-cache':.......
0d60: 09 70 72 69 6e 74 28 27 50 72 61 67 6d 61 3a 27 .print('Pragma:'
0d70: 2c 20 68 65 61 64 65 72 73 5b 68 65 61 64 65 72 , headers[header
0d80: 5d 29 0a 09 09 09 09 09 09 6e 65 77 5f 68 65 61 ]).......new_hea
0d90: 64 65 72 73 5b 68 65 61 64 65 72 5d 20 3d 20 68 ders[header] = h
0da0: 65 61 64 65 72 73 5b 68 65 61 64 65 72 5d 0a 09 eaders[header]..
0db0: 09 09 09 09 65 6c 69 66 20 6e 6f 74 20 68 65 61 ....elif not hea
0dc0: 64 65 72 20 69 6e 20 69 67 6e 6f 72 65 5f 66 69 der in ignore_fi
0dd0: 65 6c 64 73 3a 0a 09 09 09 09 09 09 70 72 69 6e elds:.......prin
0de0: 74 28 27 55 6e 64 65 66 69 6e 65 64 20 68 65 61 t('Undefined hea
0df0: 64 65 72 20 22 27 2c 20 68 65 61 64 65 72 2c 20 der "', header,
0e00: 27 22 3a 20 27 2c 20 68 65 61 64 65 72 73 5b 68 '": ', headers[h
0e10: 65 61 64 65 72 5d 2c 20 73 65 70 3d 27 27 29 0a eader], sep='').
0e20: 0a 09 09 09 09 23 20 63 6f 6d 70 61 72 69 6e 67 .....# comparing
0e30: 20 68 65 61 64 65 72 73 20 77 69 74 68 20 64 61 headers with da
0e40: 74 61 20 66 6f 75 6e 64 20 69 6e 20 69 6e 64 65 ta found in inde
0e50: 78 0a 09 09 09 09 23 20 69 66 20 61 6e 79 20 68 x.....# if any h
0e60: 65 61 64 65 72 20 68 61 73 20 63 68 61 6e 67 65 eader has change
0e70: 64 20 28 65 78 63 65 70 74 20 50 72 61 67 6d 61 d (except Pragma
0e80: 29 20 66 69 6c 65 20 69 73 20 66 75 6c 6c 79 20 ) file is fully
0e90: 64 6f 77 6e 6c 6f 61 64 65 64 0a 09 09 09 09 23 downloaded.....#
0ea0: 20 73 61 6d 65 20 69 66 20 77 65 20 67 65 74 20 same if we get
0eb0: 6d 6f 72 65 20 6f 72 20 6c 65 73 73 20 68 65 61 more or less hea
0ec0: 64 65 72 73 0a 09 09 09 09 6f 6c 64 5f 6b 65 79 ders.....old_key
0ed0: 73 20 3d 20 73 65 74 28 69 6e 64 65 78 5b 75 72 s = set(index[ur
0ee0: 6c 5d 2e 6b 65 79 73 28 29 29 0a 09 09 09 09 6f l].keys()).....o
0ef0: 6c 64 5f 6b 65 79 73 2e 64 69 73 63 61 72 64 28 ld_keys.discard(
0f00: 27 5f 5f 74 69 6d 65 5f 5f 27 29 0a 09 09 09 09 '__time__').....
0f10: 6f 6c 64 5f 6b 65 79 73 2e 64 69 73 63 61 72 64 old_keys.discard
0f20: 28 27 50 72 61 67 6d 61 27 29 0a 09 09 09 09 6d ('Pragma').....m
0f30: 6f 72 65 5f 6b 65 79 73 20 3d 20 73 65 74 28 6e ore_keys = set(n
0f40: 65 77 5f 68 65 61 64 65 72 73 2e 6b 65 79 73 28 ew_headers.keys(
0f50: 29 29 20 2d 20 6f 6c 64 5f 6b 65 79 73 0a 09 09 )) - old_keys...
0f60: 09 09 6d 6f 72 65 5f 6b 65 79 73 2e 64 69 73 63 ..more_keys.disc
0f70: 61 72 64 28 27 50 72 61 67 6d 61 27 29 0a 09 09 ard('Pragma')...
0f80: 09 09 6c 65 73 73 5f 6b 65 79 73 20 3d 20 6f 6c ..less_keys = ol
0f90: 64 5f 6b 65 79 73 20 2d 20 73 65 74 28 6e 65 77 d_keys - set(new
0fa0: 5f 68 65 61 64 65 72 73 2e 6b 65 79 73 28 29 29 _headers.keys())
0fb0: 0a 09 09 09 09 69 66 20 6c 65 6e 28 6d 6f 72 65 .....if len(more
0fc0: 5f 6b 65 79 73 29 20 3e 20 30 3a 0a 09 09 09 09 _keys) > 0:.....
0fd0: 09 69 66 20 6e 6f 74 20 6c 65 6e 28 6f 6c 64 5f .if not len(old_
0fe0: 6b 65 79 73 29 20 3d 3d 20 30 3a 0a 09 09 09 09 keys) == 0:.....
0ff0: 09 09 70 72 69 6e 74 28 27 4d 6f 72 65 20 68 65 ..print('More he
1000: 61 64 65 72 73 20 61 70 70 65 61 72 3a 27 2c 20 aders appear:',
1010: 6d 6f 72 65 5f 6b 65 79 73 29 0a 09 09 09 09 09 more_keys)......
1020: 72 65 6c 6f 61 64 20 3d 20 54 72 75 65 0a 09 09 reload = True...
1030: 09 09 65 6c 69 66 20 6c 65 6e 28 6c 65 73 73 5f ..elif len(less_
1040: 6b 65 79 73 29 20 3e 20 30 3a 0a 09 09 09 09 09 keys) > 0:......
1050: 70 72 69 6e 74 28 27 4c 65 73 73 20 68 65 61 64 print('Less head
1060: 65 72 73 20 61 70 70 65 61 72 3a 27 2c 20 6c 65 ers appear:', le
1070: 73 73 5f 6b 65 79 73 29 0a 09 09 09 09 09 72 65 ss_keys)......re
1080: 6c 6f 61 64 20 3d 20 54 72 75 65 0a 09 09 09 09 load = True.....
1090: 65 6c 73 65 3a 0a 09 09 09 09 09 66 6f 72 20 6b else:......for k
10a0: 65 79 20 69 6e 20 69 6e 64 65 78 5b 75 72 6c 5d ey in index[url]
10b0: 2e 6b 65 79 73 28 29 3a 0a 09 09 09 09 09 09 69 .keys():.......i
10c0: 66 20 6b 65 79 20 6e 6f 74 20 69 6e 20 28 27 5f f key not in ('_
10d0: 5f 74 69 6d 65 5f 5f 27 2c 20 27 50 72 61 67 6d _time__', 'Pragm
10e0: 61 27 29 20 61 6e 64 20 6e 6f 74 20 69 6e 64 65 a') and not inde
10f0: 78 5b 75 72 6c 5d 5b 6b 65 79 5d 20 3d 3d 20 6e x[url][key] == n
1100: 65 77 5f 68 65 61 64 65 72 73 5b 6b 65 79 5d 3a ew_headers[key]:
1110: 0a 09 09 09 09 09 09 09 70 72 69 6e 74 28 27 48 ........print('H
1120: 65 61 64 65 72 20 22 27 2c 20 6b 65 79 2c 20 27 eader "', key, '
1130: 22 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 5b " changed from [
1140: 27 2c 20 69 6e 64 65 78 5b 75 72 6c 5d 5b 6b 65 ', index[url][ke
1150: 79 5d 2c 20 27 5d 20 74 6f 20 5b 27 2c 20 6e 65 y], '] to [', ne
1160: 77 5f 68 65 61 64 65 72 73 5b 6b 65 79 5d 2c 20 w_headers[key],
1170: 27 5d 27 2c 20 73 65 70 3d 27 27 29 0a 09 09 09 ']', sep='')....
1180: 09 09 09 09 72 65 6c 6f 61 64 20 3d 20 54 72 75 ....reload = Tru
1190: 65 0a 0a 09 09 09 09 23 20 64 6f 77 6e 6c 6f 61 e......# downloa
11a0: 64 69 6e 67 20 66 69 6c 65 0a 09 09 09 09 69 66 ding file.....if
11b0: 20 72 65 6c 6f 61 64 3a 0a 09 09 09 09 09 69 66 reload:......if
11c0: 20 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 'Content-Length
11d0: 27 20 69 6e 20 68 65 61 64 65 72 73 3a 0a 09 09 ' in headers:...
11e0: 09 09 09 09 70 72 69 6e 74 28 27 44 6f 77 6e 6c ....print('Downl
11f0: 6f 61 64 69 6e 67 27 2c 20 68 65 61 64 65 72 73 oading', headers
1200: 5b 27 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 ['Content-Length
1210: 27 5d 2c 20 27 62 79 74 65 73 20 5b 27 2c 20 65 '], 'bytes [', e
1220: 6e 64 3d 27 27 29 0a 09 09 09 09 09 65 6c 73 65 nd='')......else
1230: 3a 0a 09 09 09 09 09 09 70 72 69 6e 74 28 27 44 :.......print('D
1240: 6f 77 6e 6c 6f 61 64 69 6e 67 20 5b 27 2c 20 65 ownloading [', e
1250: 6e 64 3d 27 27 29 0a 09 09 09 09 09 73 79 73 2e nd='')......sys.
1260: 73 74 64 6f 75 74 2e 66 6c 75 73 68 28 29 0a 0a stdout.flush()..
1270: 09 09 09 09 09 23 20 66 69 6c 65 20 69 73 20 63 .....# file is c
1280: 72 65 61 74 65 64 20 61 74 20 74 65 6d 70 6f 72 reated at tempor
1290: 61 72 79 20 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 ary location and
12a0: 20 6d 6f 76 65 64 20 69 6e 20 70 6c 61 63 65 20 moved in place
12b0: 6f 6e 6c 79 20 77 68 65 6e 20 64 6f 77 6e 6c 6f only when downlo
12c0: 61 64 20 63 6f 6d 70 6c 65 74 65 73 0a 09 09 09 ad completes....
12d0: 09 09 74 65 6d 70 5f 66 69 6c 65 20 3d 20 6f 70 ..temp_file = op
12e0: 65 6e 28 6f 70 74 69 6f 6e 73 2e 64 69 72 20 2b en(options.dir +
12f0: 20 27 2f 2e 74 6d 70 27 2c 20 27 77 62 27 29 0a '/.tmp', 'wb').
1300: 09 09 09 09 09 62 75 66 66 65 72 20 3d 20 73 6f .....buffer = so
1310: 75 72 63 65 2e 72 65 61 64 28 62 6c 6f 63 6b 5f urce.read(block_
1320: 73 69 7a 65 29 0a 09 09 09 09 09 62 6c 6f 63 6b size)......block
1330: 73 20 3d 20 30 0a 09 09 09 09 09 6d 65 67 73 20 s = 0......megs
1340: 3d 20 30 0a 09 09 09 09 09 77 68 69 6c 65 20 6c = 0......while l
1350: 65 6e 28 62 75 66 66 65 72 29 20 3e 20 30 3a 0a en(buffer) > 0:.
1360: 09 09 09 09 09 09 74 65 6d 70 5f 66 69 6c 65 2e ......temp_file.
1370: 77 72 69 74 65 28 62 75 66 66 65 72 29 0a 09 09 write(buffer)...
1380: 09 09 09 09 70 72 69 6e 74 28 27 2e 27 2c 20 65 ....print('.', e
1390: 6e 64 3d 27 27 29 0a 09 09 09 09 09 09 73 79 73 nd='').......sys
13a0: 2e 73 74 64 6f 75 74 2e 66 6c 75 73 68 28 29 0a .stdout.flush().
13b0: 09 09 09 09 09 09 62 75 66 66 65 72 20 3d 20 73 ......buffer = s
13c0: 6f 75 72 63 65 2e 72 65 61 64 28 62 6c 6f 63 6b ource.read(block
13d0: 5f 73 69 7a 65 29 0a 09 09 09 09 09 09 62 6c 6f _size).......blo
13e0: 63 6b 73 20 2b 3d 20 31 0a 09 09 09 09 09 09 69 cks += 1.......i
13f0: 66 20 62 6c 6f 63 6b 73 20 3e 20 31 30 32 34 2a f blocks > 1024*
1400: 31 30 32 34 2f 62 6c 6f 63 6b 5f 73 69 7a 65 3a 1024/block_size:
1410: 0a 09 09 09 09 09 09 09 62 6c 6f 63 6b 73 20 3d ........blocks =
1420: 20 62 6c 6f 63 6b 73 20 2d 20 31 30 32 34 2a 31 blocks - 1024*1
1430: 30 32 34 2f 62 6c 6f 63 6b 5f 73 69 7a 65 0a 09 024/block_size..
1440: 09 09 09 09 09 09 6d 65 67 73 20 2b 3d 20 31 0a ......megs += 1.
1450: 09 09 09 09 09 09 09 70 72 69 6e 74 28 27 7b 7d .......print('{}
1460: 4d 62 27 2e 66 6f 72 6d 61 74 28 6d 65 67 73 29 Mb'.format(megs)
1470: 2c 20 65 6e 64 3d 27 27 29 0a 09 09 09 09 09 74 , end='')......t
1480: 65 6d 70 5f 66 69 6c 65 2e 63 6c 6f 73 65 28 29 emp_file.close()
1490: 0a 09 09 09 09 09 70 72 69 6e 74 28 27 5d 27 29 ......print(']')
14a0: 0a 09 09 09 09 09 6f 73 2e 72 65 6e 61 6d 65 73 ......os.renames
14b0: 28 6f 70 74 69 6f 6e 73 2e 64 69 72 20 2b 20 27 (options.dir + '
14c0: 2f 2e 74 6d 70 27 2c 20 66 69 6c 65 5f 6e 61 6d /.tmp', file_nam
14d0: 65 29 0a 0a 09 09 09 09 63 68 65 63 6b 65 64 5f e)......checked_
14e0: 66 69 6c 65 73 20 2b 3d 20 31 0a 0a 09 09 09 09 files += 1......
14f0: 23 20 73 74 6f 72 69 6e 67 20 6e 65 77 20 74 69 # storing new ti
1500: 6d 65 20 6d 61 72 6b 20 61 6e 64 20 73 74 6f 72 me mark and stor
1510: 69 6e 67 20 6e 65 77 20 68 65 61 64 65 72 73 0a ing new headers.
1520: 09 09 09 09 6e 65 77 5f 68 65 61 64 65 72 73 5b ....new_headers[
1530: 27 5f 5f 74 69 6d 65 5f 5f 27 5d 20 3d 20 64 61 '__time__'] = da
1540: 74 65 74 69 6d 65 2e 64 61 74 65 74 69 6d 65 2e tetime.datetime.
1550: 6e 6f 77 28 29 0a 09 09 09 09 69 6e 64 65 78 5b now().....index[
1560: 75 72 6c 5d 20 3d 20 6e 65 77 5f 68 65 61 64 65 url] = new_heade
1570: 72 73 0a 09 09 09 09 69 6e 64 65 78 2e 73 79 6e rs.....index.syn
1580: 63 28 29 0a 0a 09 09 65 78 63 65 70 74 20 75 72 c()....except ur
1590: 6c 6c 69 62 2e 65 72 72 6f 72 2e 48 54 54 50 45 llib.error.HTTPE
15a0: 72 72 6f 72 20 61 73 20 65 72 72 6f 72 3a 0a 09 rror as error:..
15b0: 09 09 23 20 69 6e 20 63 61 73 65 20 6f 66 20 65 ..# in case of e
15c0: 72 72 6f 72 20 77 65 20 64 6f 6e 27 74 20 6e 65 rror we don't ne
15d0: 65 64 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e ed to do anythin
15e0: 67 20 61 63 74 75 61 6c 6c 79 2c 0a 09 09 09 23 g actually,....#
15f0: 20 69 66 20 66 69 6c 65 20 64 6f 77 6e 6c 6f 61 if file downloa
1600: 64 20 73 74 61 6c 6c 73 20 6f 72 20 66 61 69 6c d stalls or fail
1610: 73 20 74 68 65 20 66 69 6c 65 20 77 6f 75 6c 64 s the file would
1620: 20 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 74 6f not be moved to
1630: 20 69 74 27 73 20 6c 6f 63 61 74 69 6f 6e 0a 09 it's location..
1640: 09 09 70 72 69 6e 74 28 65 72 72 6f 72 29 0a 0a ..print(error)..
1650: 09 70 72 69 6e 74 28 27 5b 27 2c 20 6c 65 6e 28 .print('[', len(
1660: 75 6e 63 68 65 63 6b 65 64 5f 66 69 6c 65 73 29 unchecked_files)
1670: 2c 20 27 2f 27 2c 20 63 68 65 63 6b 65 64 5f 66 , '/', checked_f
1680: 69 6c 65 73 2c 20 27 5d 27 29 0a 0a 09 23 20 63 iles, ']')...# c
1690: 68 65 63 6b 69 6e 67 20 69 66 20 74 68 65 72 65 hecking if there
16a0: 20 77 65 72 65 20 61 6e 79 20 66 69 6c 65 73 20 were any files
16b0: 64 6f 77 6e 6c 6f 61 64 65 64 2c 20 69 66 20 79 downloaded, if y
16c0: 65 73 20 2d 20 72 65 73 74 61 72 74 69 6e 67 20 es - restarting
16d0: 73 65 71 75 65 6e 63 65 0a 09 69 66 20 63 68 65 sequence..if che
16e0: 63 6b 65 64 5f 66 69 6c 65 73 20 3d 3d 20 30 3a cked_files == 0:
16f0: 0a 09 09 62 72 65 61 6b 0a ...break.