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 0a 09 09 23 20 66 6f 72 63 69 62 6c 79 20 )....# forcibly
0810: 63 68 65 63 6b 69 6e 67 20 66 69 6c 65 20 69 66 checking file if
0820: 20 6e 6f 20 66 69 6c 65 20 70 72 65 73 65 6e 74 no file present
0830: 0a 09 09 69 66 20 6e 6f 74 20 6f 73 2e 61 63 63 ...if not os.acc
0840: 65 73 73 28 66 69 6c 65 5f 6e 61 6d 65 2c 20 6f ess(file_name, o
0850: 73 2e 52 5f 4f 4b 29 3a 0a 09 09 09 72 65 6c 6f s.R_OK):....relo
0860: 61 64 20 3d 20 54 72 75 65 0a 0a 09 09 23 20 66 ad = True....# f
0870: 6f 72 63 69 62 6c 79 20 63 68 65 63 6b 69 6e 67 orcibly checking
0880: 20 66 69 6c 65 20 69 66 20 66 69 6c 65 20 73 69 file if file si
0890: 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 74 63 68 ze doesn't match
08a0: 20 77 69 74 68 20 69 6e 64 65 78 20 64 61 74 61 with index data
08b0: 0a 09 09 65 6c 69 66 20 27 43 6f 6e 74 65 6e 74 ...elif 'Content
08c0: 2d 4c 65 6e 67 74 68 27 20 69 6e 20 69 6e 64 65 -Length' in inde
08d0: 78 5b 75 72 6c 5d 20 61 6e 64 20 6f 73 2e 73 74 x[url] and os.st
08e0: 61 74 28 66 69 6c 65 5f 6e 61 6d 65 29 2e 73 74 at(file_name).st
08f0: 5f 73 69 7a 65 20 21 3d 20 69 6e 74 28 69 6e 64 _size != int(ind
0900: 65 78 5b 75 72 6c 5d 5b 27 43 6f 6e 74 65 6e 74 ex[url]['Content
0910: 2d 4c 65 6e 67 74 68 27 5d 29 3a 0a 09 09 09 70 -Length']):....p
0920: 72 69 6e 74 28 27 46 69 6c 65 20 73 69 7a 65 20 rint('File size
0930: 69 73 27 2c 20 6f 73 2e 73 74 61 74 28 66 69 6c is', os.stat(fil
0940: 65 5f 6e 61 6d 65 29 2e 73 74 5f 73 69 7a 65 2c e_name).st_size,
0950: 20 27 61 6e 64 20 73 74 6f 72 65 64 20 66 69 6c 'and stored fil
0960: 65 20 73 69 7a 65 20 69 73 27 2c 20 69 6e 64 65 e size is', inde
0970: 78 5b 75 72 6c 5d 5b 27 43 6f 6e 74 65 6e 74 2d x[url]['Content-
0980: 4c 65 6e 67 74 68 27 5d 29 0a 09 09 09 72 65 6c Length'])....rel
0990: 6f 61 64 20 3d 20 54 72 75 65 0a 0a 09 09 23 20 oad = True....#
09a0: 66 6f 72 63 69 62 6c 79 20 63 68 65 63 6b 69 6e forcibly checkin
09b0: 67 20 66 69 6c 65 20 69 66 20 69 6e 64 65 78 20 g file if index
09c0: 68 6f 64 73 20 50 72 61 67 6d 61 20 68 65 61 64 hods Pragma head
09d0: 65 72 0a 09 09 69 66 20 27 50 72 61 67 6d 61 27 er...if 'Pragma'
09e0: 20 69 6e 20 69 6e 64 65 78 5b 75 72 6c 5d 20 61 in index[url] a
09f0: 6e 64 20 69 6e 64 65 78 5b 75 72 6c 5d 5b 27 50 nd index[url]['P
0a00: 72 61 67 6d 61 27 5d 20 3d 3d 20 27 6e 6f 2d 63 ragma'] == 'no-c
0a10: 61 63 68 65 27 3a 0a 09 09 09 72 65 6c 6f 61 64 ache':....reload
0a20: 20 3d 20 54 72 75 65 0a 0a 09 09 23 20 73 6b 69 = True....# ski
0a30: 70 70 69 6e 67 20 66 69 6c 65 20 70 72 6f 63 65 pping file proce
0a40: 73 73 69 6e 67 20 69 66 20 74 68 65 72 65 27 73 ssing if there's
0a50: 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 63 68 no need to rech
0a60: 65 63 6b 20 69 74 20 61 6e 64 20 77 65 20 68 61 eck it and we ha
0a70: 76 65 20 63 68 65 63 6b 65 64 20 69 74 20 61 74 ve checked it at
0a80: 20 6c 65 61 73 74 20 34 20 68 6f 75 72 73 20 61 least 4 hours a
0a90: 67 6f 0a 09 09 69 66 20 6e 6f 74 20 72 65 6c 6f go...if not relo
0aa0: 61 64 20 61 6e 64 20 27 5f 5f 74 69 6d 65 5f 5f ad and '__time__
0ab0: 27 20 69 6e 20 69 6e 64 65 78 5b 75 72 6c 5d 20 ' in index[url]
0ac0: 61 6e 64 20 28 64 61 74 65 74 69 6d 65 2e 64 61 and (datetime.da
0ad0: 74 65 74 69 6d 65 2e 6e 6f 77 28 29 20 2d 20 64 tetime.now() - d
0ae0: 61 74 65 74 69 6d 65 2e 74 69 6d 65 64 65 6c 74 atetime.timedelt
0af0: 61 28 68 6f 75 72 73 20 3d 20 34 29 20 2d 20 69 a(hours = 4) - i
0b00: 6e 64 65 78 5b 75 72 6c 5d 5b 27 5f 5f 74 69 6d ndex[url]['__tim
0b10: 65 5f 5f 27 5d 29 2e 64 61 79 73 20 3c 20 30 3a e__']).days < 0:
0b20: 0a 09 09 09 63 6f 6e 74 69 6e 75 65 0a 09 09 70 ....continue...p
0b30: 72 69 6e 74 28 27 43 68 65 63 6b 69 6e 67 20 66 rint('Checking f
0b40: 69 6c 65 3a 27 2c 20 75 72 6c 29 0a 09 09 74 72 ile:', url)...tr
0b50: 79 3a 0a 09 09 09 77 69 74 68 20 75 72 6c 6c 69 y:....with urlli
0b60: 62 2e 72 65 71 75 65 73 74 2e 75 72 6c 6f 70 65 b.request.urlope
0b70: 6e 28 6f 70 74 69 6f 6e 73 2e 72 6f 6f 74 20 2b n(options.root +
0b80: 20 75 72 6c 29 20 61 73 20 73 6f 75 72 63 65 3a url) as source:
0b90: 0a 09 09 09 09 6e 65 77 5f 68 65 61 64 65 72 73 .....new_headers
0ba0: 20 3d 20 7b 7d 0a 09 09 09 09 68 65 61 64 65 72 = {}.....header
0bb0: 73 20 3d 20 73 6f 75 72 63 65 2e 69 6e 66 6f 28 s = source.info(
0bc0: 29 0a 0a 09 09 09 09 23 20 73 74 72 69 70 70 69 )......# strippi
0bd0: 6e 67 20 75 6e 6e 65 65 64 65 64 20 68 65 61 64 ng unneeded head
0be0: 65 72 73 20 28 58 58 58 20 6d 61 6b 65 20 74 68 ers (XXX make th
0bf0: 69 73 20 69 6e 70 6c 61 63 65 3f 29 0a 09 09 09 is inplace?)....
0c00: 09 66 6f 72 20 68 65 61 64 65 72 20 69 6e 20 68 .for header in h
0c10: 65 61 64 65 72 73 3a 0a 09 09 09 09 09 69 66 20 eaders:......if
0c20: 68 65 61 64 65 72 20 69 6e 20 64 65 73 63 5f 66 header in desc_f
0c30: 69 65 6c 64 73 3a 0a 09 09 09 09 09 09 69 66 20 ields:.......if
0c40: 68 65 61 64 65 72 20 3d 3d 20 27 50 72 61 67 6d header == 'Pragm
0c50: 61 27 20 61 6e 64 20 68 65 61 64 65 72 73 5b 68 a' and headers[h
0c60: 65 61 64 65 72 5d 20 21 3d 20 27 6e 6f 2d 63 61 eader] != 'no-ca
0c70: 63 68 65 27 3a 0a 09 09 09 09 09 09 09 70 72 69 che':........pri
0c80: 6e 74 28 27 50 72 61 67 6d 61 3a 27 2c 20 68 65 nt('Pragma:', he
0c90: 61 64 65 72 73 5b 68 65 61 64 65 72 5d 29 0a 09 aders[header])..
0ca0: 09 09 09 09 09 6e 65 77 5f 68 65 61 64 65 72 73 .....new_headers
0cb0: 5b 68 65 61 64 65 72 5d 20 3d 20 68 65 61 64 65 [header] = heade
0cc0: 72 73 5b 68 65 61 64 65 72 5d 0a 09 09 09 09 09 rs[header]......
0cd0: 65 6c 69 66 20 6e 6f 74 20 68 65 61 64 65 72 20 elif not header
0ce0: 69 6e 20 69 67 6e 6f 72 65 5f 66 69 65 6c 64 73 in ignore_fields
0cf0: 3a 0a 09 09 09 09 09 09 70 72 69 6e 74 28 27 55 :.......print('U
0d00: 6e 64 65 66 69 6e 65 64 20 68 65 61 64 65 72 20 ndefined header
0d10: 22 27 2c 20 68 65 61 64 65 72 2c 20 27 22 3a 20 "', header, '":
0d20: 27 2c 20 68 65 61 64 65 72 73 5b 68 65 61 64 65 ', headers[heade
0d30: 72 5d 2c 20 73 65 70 3d 27 27 29 0a 0a 09 09 09 r], sep='').....
0d40: 09 23 20 63 6f 6d 70 61 72 69 6e 67 20 68 65 61 .# comparing hea
0d50: 64 65 72 73 20 77 69 74 68 20 64 61 74 61 20 66 ders with data f
0d60: 6f 75 6e 64 20 69 6e 20 69 6e 64 65 78 0a 09 09 ound in index...
0d70: 09 09 23 20 69 66 20 61 6e 79 20 68 65 61 64 65 ..# if any heade
0d80: 72 20 68 61 73 20 63 68 61 6e 67 65 64 20 28 65 r has changed (e
0d90: 78 63 65 70 74 20 50 72 61 67 6d 61 29 20 66 69 xcept Pragma) fi
0da0: 6c 65 20 69 73 20 66 75 6c 6c 79 20 64 6f 77 6e le is fully down
0db0: 6c 6f 61 64 65 64 0a 09 09 09 09 23 20 73 61 6d loaded.....# sam
0dc0: 65 20 69 66 20 77 65 20 67 65 74 20 6d 6f 72 65 e if we get more
0dd0: 20 6f 72 20 6c 65 73 73 20 68 65 61 64 65 72 73 or less headers
0de0: 0a 09 09 09 09 6f 6c 64 5f 6b 65 79 73 20 3d 20 .....old_keys =
0df0: 73 65 74 28 69 6e 64 65 78 5b 75 72 6c 5d 2e 6b set(index[url].k
0e00: 65 79 73 28 29 29 0a 09 09 09 09 6f 6c 64 5f 6b eys()).....old_k
0e10: 65 79 73 2e 64 69 73 63 61 72 64 28 27 5f 5f 74 eys.discard('__t
0e20: 69 6d 65 5f 5f 27 29 0a 09 09 09 09 6f 6c 64 5f ime__').....old_
0e30: 6b 65 79 73 2e 64 69 73 63 61 72 64 28 27 50 72 keys.discard('Pr
0e40: 61 67 6d 61 27 29 0a 09 09 09 09 6d 6f 72 65 5f agma').....more_
0e50: 6b 65 79 73 20 3d 20 73 65 74 28 6e 65 77 5f 68 keys = set(new_h
0e60: 65 61 64 65 72 73 2e 6b 65 79 73 28 29 29 20 2d eaders.keys()) -
0e70: 20 6f 6c 64 5f 6b 65 79 73 0a 09 09 09 09 6d 6f old_keys.....mo
0e80: 72 65 5f 6b 65 79 73 2e 64 69 73 63 61 72 64 28 re_keys.discard(
0e90: 27 50 72 61 67 6d 61 27 29 0a 09 09 09 09 6c 65 'Pragma').....le
0ea0: 73 73 5f 6b 65 79 73 20 3d 20 6f 6c 64 5f 6b 65 ss_keys = old_ke
0eb0: 79 73 20 2d 20 73 65 74 28 6e 65 77 5f 68 65 61 ys - set(new_hea
0ec0: 64 65 72 73 2e 6b 65 79 73 28 29 29 0a 09 09 09 ders.keys())....
0ed0: 09 69 66 20 6c 65 6e 28 6d 6f 72 65 5f 6b 65 79 .if len(more_key
0ee0: 73 29 20 3e 20 30 3a 0a 09 09 09 09 09 70 72 69 s) > 0:......pri
0ef0: 6e 74 28 27 4d 6f 72 65 20 68 65 61 64 65 72 73 nt('More headers
0f00: 20 61 70 70 65 61 72 3a 27 2c 20 6d 6f 72 65 5f appear:', more_
0f10: 6b 65 79 73 29 0a 09 09 09 09 09 72 65 6c 6f 61 keys)......reloa
0f20: 64 20 3d 20 54 72 75 65 0a 09 09 09 09 65 6c 69 d = True.....eli
0f30: 66 20 6c 65 6e 28 6c 65 73 73 5f 6b 65 79 73 29 f len(less_keys)
0f40: 20 3e 20 30 3a 0a 09 09 09 09 09 70 72 69 6e 74 > 0:......print
0f50: 28 27 4c 65 73 73 20 68 65 61 64 65 72 73 20 61 ('Less headers a
0f60: 70 70 65 61 72 3a 27 2c 20 6c 65 73 73 5f 6b 65 ppear:', less_ke
0f70: 79 73 29 0a 09 09 09 09 09 72 65 6c 6f 61 64 20 ys)......reload
0f80: 3d 20 54 72 75 65 0a 09 09 09 09 65 6c 73 65 3a = True.....else:
0f90: 0a 09 09 09 09 09 66 6f 72 20 6b 65 79 20 69 6e ......for key in
0fa0: 20 69 6e 64 65 78 5b 75 72 6c 5d 2e 6b 65 79 73 index[url].keys
0fb0: 28 29 3a 0a 09 09 09 09 09 09 69 66 20 6b 65 79 ():.......if key
0fc0: 20 6e 6f 74 20 69 6e 20 28 27 5f 5f 74 69 6d 65 not in ('__time
0fd0: 5f 5f 27 2c 20 27 50 72 61 67 6d 61 27 29 20 61 __', 'Pragma') a
0fe0: 6e 64 20 6e 6f 74 20 69 6e 64 65 78 5b 75 72 6c nd not index[url
0ff0: 5d 5b 6b 65 79 5d 20 3d 3d 20 6e 65 77 5f 68 65 ][key] == new_he
1000: 61 64 65 72 73 5b 6b 65 79 5d 3a 0a 09 09 09 09 aders[key]:.....
1010: 09 09 09 70 72 69 6e 74 28 27 48 65 61 64 65 72 ...print('Header
1020: 20 22 27 2c 20 6b 65 79 2c 20 27 22 20 63 68 61 "', key, '" cha
1030: 6e 67 65 64 20 66 72 6f 6d 20 5b 27 2c 20 69 6e nged from [', in
1040: 64 65 78 5b 75 72 6c 5d 5b 6b 65 79 5d 2c 20 27 dex[url][key], '
1050: 5d 20 74 6f 20 5b 27 2c 20 6e 65 77 5f 68 65 61 ] to [', new_hea
1060: 64 65 72 73 5b 6b 65 79 5d 2c 20 27 5d 27 2c 20 ders[key], ']',
1070: 73 65 70 3d 27 27 29 0a 09 09 09 09 09 09 09 72 sep='')........r
1080: 65 6c 6f 61 64 20 3d 20 54 72 75 65 0a 0a 09 09 eload = True....
1090: 09 09 23 20 64 6f 77 6e 6c 6f 61 64 69 6e 67 20 ..# downloading
10a0: 66 69 6c 65 0a 09 09 09 09 69 66 20 72 65 6c 6f file.....if relo
10b0: 61 64 3a 0a 09 09 09 09 09 69 66 20 27 43 6f 6e ad:......if 'Con
10c0: 74 65 6e 74 2d 4c 65 6e 67 74 68 27 20 69 6e 20 tent-Length' in
10d0: 68 65 61 64 65 72 73 3a 0a 09 09 09 09 09 09 70 headers:.......p
10e0: 72 69 6e 74 28 27 44 6f 77 6e 6c 6f 61 64 69 6e rint('Downloadin
10f0: 67 27 2c 20 68 65 61 64 65 72 73 5b 27 43 6f 6e g', headers['Con
1100: 74 65 6e 74 2d 4c 65 6e 67 74 68 27 5d 2c 20 27 tent-Length'], '
1110: 62 79 74 65 73 20 5b 27 2c 20 65 6e 64 3d 27 27 bytes [', end=''
1120: 29 0a 09 09 09 09 09 65 6c 73 65 3a 0a 09 09 09 )......else:....
1130: 09 09 09 70 72 69 6e 74 28 27 44 6f 77 6e 6c 6f ...print('Downlo
1140: 61 64 69 6e 67 20 5b 27 2c 20 65 6e 64 3d 27 27 ading [', end=''
1150: 29 0a 09 09 09 09 09 73 79 73 2e 73 74 64 6f 75 )......sys.stdou
1160: 74 2e 66 6c 75 73 68 28 29 0a 0a 09 09 09 09 09 t.flush().......
1170: 23 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 # file is create
1180: 64 20 61 74 20 74 65 6d 70 6f 72 61 72 79 20 6c d at temporary l
1190: 6f 63 61 74 69 6f 6e 20 61 6e 64 20 6d 6f 76 65 ocation and move
11a0: 64 20 69 6e 20 70 6c 61 63 65 20 6f 6e 6c 79 20 d in place only
11b0: 77 68 65 6e 20 64 6f 77 6e 6c 6f 61 64 20 63 6f when download co
11c0: 6d 70 6c 65 74 65 73 0a 09 09 09 09 09 74 65 6d mpletes......tem
11d0: 70 5f 66 69 6c 65 20 3d 20 6f 70 65 6e 28 6f 70 p_file = open(op
11e0: 74 69 6f 6e 73 2e 64 69 72 20 2b 20 27 2f 2e 74 tions.dir + '/.t
11f0: 6d 70 27 2c 20 27 77 62 27 29 0a 09 09 09 09 09 mp', 'wb')......
1200: 62 75 66 66 65 72 20 3d 20 73 6f 75 72 63 65 2e buffer = source.
1210: 72 65 61 64 28 62 6c 6f 63 6b 5f 73 69 7a 65 29 read(block_size)
1220: 0a 09 09 09 09 09 62 6c 6f 63 6b 73 20 3d 20 30 ......blocks = 0
1230: 0a 09 09 09 09 09 6d 65 67 73 20 3d 20 30 0a 09 ......megs = 0..
1240: 09 09 09 09 77 68 69 6c 65 20 6c 65 6e 28 62 75 ....while len(bu
1250: 66 66 65 72 29 20 3e 20 30 3a 0a 09 09 09 09 09 ffer) > 0:......
1260: 09 74 65 6d 70 5f 66 69 6c 65 2e 77 72 69 74 65 .temp_file.write
1270: 28 62 75 66 66 65 72 29 0a 09 09 09 09 09 09 70 (buffer).......p
1280: 72 69 6e 74 28 27 2e 27 2c 20 65 6e 64 3d 27 27 rint('.', end=''
1290: 29 0a 09 09 09 09 09 09 73 79 73 2e 73 74 64 6f ).......sys.stdo
12a0: 75 74 2e 66 6c 75 73 68 28 29 0a 09 09 09 09 09 ut.flush()......
12b0: 09 62 75 66 66 65 72 20 3d 20 73 6f 75 72 63 65 .buffer = source
12c0: 2e 72 65 61 64 28 62 6c 6f 63 6b 5f 73 69 7a 65 .read(block_size
12d0: 29 0a 09 09 09 09 09 09 62 6c 6f 63 6b 73 20 2b ).......blocks +
12e0: 3d 20 31 0a 09 09 09 09 09 09 69 66 20 62 6c 6f = 1.......if blo
12f0: 63 6b 73 20 3e 20 31 30 32 34 2a 31 30 32 34 2f cks > 1024*1024/
1300: 62 6c 6f 63 6b 5f 73 69 7a 65 3a 0a 09 09 09 09 block_size:.....
1310: 09 09 09 62 6c 6f 63 6b 73 20 3d 20 62 6c 6f 63 ...blocks = bloc
1320: 6b 73 20 2d 20 31 30 32 34 2a 31 30 32 34 2f 62 ks - 1024*1024/b
1330: 6c 6f 63 6b 5f 73 69 7a 65 0a 09 09 09 09 09 09 lock_size.......
1340: 09 6d 65 67 73 20 2b 3d 20 31 0a 09 09 09 09 09 .megs += 1......
1350: 09 09 70 72 69 6e 74 28 27 7b 7d 4d 62 27 2e 66 ..print('{}Mb'.f
1360: 6f 72 6d 61 74 28 6d 65 67 73 29 2c 20 65 6e 64 ormat(megs), end
1370: 3d 27 27 29 0a 09 09 09 09 09 74 65 6d 70 5f 66 ='')......temp_f
1380: 69 6c 65 2e 63 6c 6f 73 65 28 29 0a 09 09 09 09 ile.close().....
1390: 09 70 72 69 6e 74 28 27 5d 27 29 0a 09 09 09 09 .print(']').....
13a0: 09 6f 73 2e 72 65 6e 61 6d 65 73 28 6f 70 74 69 .os.renames(opti
13b0: 6f 6e 73 2e 64 69 72 20 2b 20 27 2f 2e 74 6d 70 ons.dir + '/.tmp
13c0: 27 2c 20 66 69 6c 65 5f 6e 61 6d 65 29 0a 0a 09 ', file_name)...
13d0: 09 09 09 63 68 65 63 6b 65 64 5f 66 69 6c 65 73 ...checked_files
13e0: 20 2b 3d 20 31 0a 0a 09 09 09 09 23 20 73 74 6f += 1......# sto
13f0: 72 69 6e 67 20 6e 65 77 20 74 69 6d 65 20 6d 61 ring new time ma
1400: 72 6b 20 61 6e 64 20 73 74 6f 72 69 6e 67 20 6e rk and storing n
1410: 65 77 20 68 65 61 64 65 72 73 0a 09 09 09 09 6e ew headers.....n
1420: 65 77 5f 68 65 61 64 65 72 73 5b 27 5f 5f 74 69 ew_headers['__ti
1430: 6d 65 5f 5f 27 5d 20 3d 20 64 61 74 65 74 69 6d me__'] = datetim
1440: 65 2e 64 61 74 65 74 69 6d 65 2e 6e 6f 77 28 29 e.datetime.now()
1450: 0a 09 09 09 09 69 6e 64 65 78 5b 75 72 6c 5d 20 .....index[url]
1460: 3d 20 6e 65 77 5f 68 65 61 64 65 72 73 0a 09 09 = new_headers...
1470: 09 09 69 6e 64 65 78 2e 73 79 6e 63 28 29 0a 0a ..index.sync()..
1480: 09 09 65 78 63 65 70 74 20 75 72 6c 6c 69 62 2e ..except urllib.
1490: 65 72 72 6f 72 2e 48 54 54 50 45 72 72 6f 72 20 error.HTTPError
14a0: 61 73 20 65 72 72 6f 72 3a 0a 09 09 09 23 20 69 as error:....# i
14b0: 6e 20 63 61 73 65 20 6f 66 20 65 72 72 6f 72 20 n case of error
14c0: 77 65 20 64 6f 6e 27 74 20 6e 65 65 64 20 74 6f we don't need to
14d0: 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 61 63 74 do anything act
14e0: 75 61 6c 6c 79 2c 0a 09 09 09 23 20 69 66 20 66 ually,....# if f
14f0: 69 6c 65 20 64 6f 77 6e 6c 6f 61 64 20 73 74 61 ile download sta
1500: 6c 6c 73 20 6f 72 20 66 61 69 6c 73 20 74 68 65 lls or fails the
1510: 20 66 69 6c 65 20 77 6f 75 6c 64 20 6e 6f 74 20 file would not
1520: 62 65 20 6d 6f 76 65 64 20 74 6f 20 69 74 27 73 be moved to it's
1530: 20 6c 6f 63 61 74 69 6f 6e 0a 09 09 09 70 72 69 location....pri
1540: 6e 74 28 65 72 72 6f 72 29 0a 0a 09 70 72 69 6e nt(error)...prin
1550: 74 28 27 5b 27 2c 20 6c 65 6e 28 75 6e 63 68 65 t('[', len(unche
1560: 63 6b 65 64 5f 66 69 6c 65 73 29 2c 20 27 2f 27 cked_files), '/'
1570: 2c 20 63 68 65 63 6b 65 64 5f 66 69 6c 65 73 2c , checked_files,
1580: 20 27 5d 27 29 0a 0a 09 23 20 63 68 65 63 6b 69 ']')...# checki
1590: 6e 67 20 69 66 20 74 68 65 72 65 20 77 65 72 65 ng if there were
15a0: 20 61 6e 79 20 66 69 6c 65 73 20 64 6f 77 6e 6c any files downl
15b0: 6f 61 64 65 64 2c 20 69 66 20 79 65 73 20 2d 20 oaded, if yes -
15c0: 72 65 73 74 61 72 74 69 6e 67 20 73 65 71 75 65 restarting seque
15d0: 6e 63 65 0a 09 69 66 20 63 68 65 63 6b 65 64 5f nce..if checked_
15e0: 66 69 6c 65 73 20 3d 3d 20 30 3a 0a 09 09 62 72 files == 0:...br
15f0: 65 61 6b 0a eak.