annotate bin/warc.py @ 143:ddff993994be

too clever by half, keys won't work in parallel for e.g. media types
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Wed, 20 Oct 2021 15:47:55 +0000
parents e96d444b0f84
children d123ef7fdb82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
138
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/env python3
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 '''Stream a warc format file, invoking a callback on each part.
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 Callback can be limited by WARC-Type'''
139
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
4 import sys,os
138
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6 def warc(callback,types=['response']):
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 nb=0
139
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
8 stream=open(sys.argv[1],'rb',0)
138
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 bufsize=128*1024*1024
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 buf=bytearray(128*1024*1024)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 l=b'\r\n'
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12 while True:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 while l==b'\r\n':
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 l=stream.readline()
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 nb+=len(l)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16 if l!=b'WARC/1.0\r\n':
139
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
17 if l==b'':
138
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18 return
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19 raise ValueError("Not a WARC file? At %s: %s[%s]"%(nb-len(l),
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
20 l.decode('latin-1'),len(l)))
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
21 wtype=None
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
22 length=None
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
23 while l!=b'\r\n':
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
24 l=stream.readline()
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
25 nb+=len(l)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
26 if l.startswith(b'WARC-Type: '):
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
27 wtype = l[11:-2]
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
28 elif l.startswith(b'Content-Length: '):
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
29 length = int(l[16:])
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
30 bv=memoryview(buf)[:length]
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
31 ii=0
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
32 while True:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
33 i=stream.readinto(bv)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
34 ii+=i
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
35 if ii>=length:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
36 break
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
37 bv=memoryview(buf)[ii:length]
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
38 if ii!=length:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
39 raise ValueError("Chunk read losing, from %s got %s expected %s"%(nb,ii,length))
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
40 nb+=length
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
41 if wtype in types:
139
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
42 callback(wtype,memoryview(buf[:length]))
138
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
43 if whole and options.zipped:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
44 _output(bv)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
45 return
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
46 gzip_chunk = io.BytesIO(bv)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
47 uv=memoryview(buf)[length:]
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
48 with igzip.IGzipFile(fileobj=gzip_chunk) as gzip_fin:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
49 ll=0
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
50 while True:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
51 l=gzip_fin.readinto(uv)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
52 if not l:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
53 break
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
54 ll+=l
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
55 cb=memoryview(uv)[:ll]
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
56 if whole:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
57 _output(cb)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
58 return
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
59 # Only output parts (0 = WARC header, 1 = HTTP header, 2 = body) that are wanted
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
60 state=0
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
61 tr=None # Was this record truncated?
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
62 bl=None # for HTTP Content-Length for the length of the body?
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
63 with io.BytesIO(cb) as clear_text:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
64 for L in clear_text:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
65 if state==0:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
66 # WARC header
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
67 if L.startswith(b"Content-Length: "):
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
68 wl=int(L[16:].rstrip())
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
69 elif L.startswith(b"WARC-Truncated: "):
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
70 tr=L[16:].rstrip()
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71 tr="EMPTY" if tr=="" else tr
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
72 elif L==b"" or L.startswith(b"\r"): # for idempotency
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
73 # Blank line, WARC header is finished
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
74 if not (options.headers or options.body):
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75 return
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
76 state=1
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
77 # Note we preserve the empty line
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
78 if options.warc:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
79 _output(L)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
80 continue
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
81 if state==1:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
82 # HTTP header
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
83 wl -= len(L)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
84 if not (L==b"" or L.startswith(b"\r")):
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
85 # Non-blank, it's a header
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
86 if bl is None and L.startswith(b"Content-Length: "):
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
87 bl=int(L[16:].rstrip())
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
88 if options.headers:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
89 _output(L)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
90 else:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
91 # Blank line, HTTP header is finished
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
92 if not options.body:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
93 return
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
94 if options.headers:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
95 _output(L)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
96 state=2
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
97 # The above is just for sanity, because we do _not_
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
98 # continue with the outer loop,
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
99 # since we can now block-output the entire rest of the
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
100 # input buffer.
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
101 if bl is not None:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
102 if bl!=wl:
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
103 print("length mismatch: %s %s %s here: %s given: %s trunc: %s"%\
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
104 (length,offset,filename,wl,bl,tr),file=sys.stderr)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
105 # HTTP body
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
106 balance=clear_text.tell()
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
107 #print(balance,bl,wl,ll,ll-balance,file=sys.stderr)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
108 # Output whatever is left
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
109 _output(cb[balance:balance+wl])
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
110 return
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
111
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
112 OUT=open(sys.stdout.fileno(),'wb')
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
113
139
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
114 import re
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
115 LMPAT=re.compile(b'^Last-Modified: (.*?)\r',re.MULTILINE)
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
116
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
117 def showmeLMH(wtype,buf):
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
118 m=LMPAT.search(buf.tobytes(order='A'))
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
119 if m:
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
120 OUT.write(m[1])
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
121 OUT.write(b'\n')
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
122
138
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
123 def showme(wtype,buf):
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
124 OUT.write(buf)
9ea12f7b304b just barely working
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
125
139
e96d444b0f84 fixed bug(s) wrt large payload files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 138
diff changeset
126 warc(showmeLMH,[b'response'])