Mercurial > hg > cc > cirrus_work
annotate bin/warc.py @ 61:f182d09ad1cd
whole working
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Fri, 14 Jul 2023 12:08:09 +0100 |
parents | 7b68c3ebc35a |
children | 11cbaee8bbc8 |
rev | line source |
---|---|
39 | 1 #!/usr/bin/env python3 |
46
44d3a4f4ea51
support on-board unzipping, reduce buffer size to 2MB
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
43
diff
changeset
|
2 '''Stream a warc format file, unzipping if necessary, invoking a |
44d3a4f4ea51
support on-board unzipping, reduce buffer size to 2MB
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
43
diff
changeset
|
3 callback on each record. Callback can be limited by WARC-Type, record |
44d3a4f4ea51
support on-board unzipping, reduce buffer size to 2MB
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
43
diff
changeset
|
4 part''' |
39 | 5 |
42
689a0e311cd2
make warc.py a library, separate out testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
6 import sys,io |
689a0e311cd2
make warc.py a library, separate out testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
7 from isal import igzip |
39 | 8 |
42
689a0e311cd2
make warc.py a library, separate out testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
39
diff
changeset
|
9 def warc(filename,callback,types=['response'],whole=False,parts=7,debug=False): |
61 | 10 '''parts is a bit-mask: |
11 1 for warc header; | |
12 2 for req/resp HTTP header, warcinfo/metadata features; | |
13 4 for req/resp body''' | |
39 | 14 types=[(t if isinstance(t,bytes) else bytes(t,'utf8')) for t in types] |
15 nb=0 | |
46
44d3a4f4ea51
support on-board unzipping, reduce buffer size to 2MB
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
43
diff
changeset
|
16 if filename.endswith(".gz"): |
44d3a4f4ea51
support on-board unzipping, reduce buffer size to 2MB
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
43
diff
changeset
|
17 stream=igzip.IGzipFile(filename=filename) |
44d3a4f4ea51
support on-board unzipping, reduce buffer size to 2MB
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
43
diff
changeset
|
18 else: |
44d3a4f4ea51
support on-board unzipping, reduce buffer size to 2MB
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
43
diff
changeset
|
19 stream=open(filename,'rb',0) |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
20 bufSize=2*1024*1024 |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
21 hdrMax=16*1024 |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
22 buf=bytearray(bufSize) |
59
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
23 bufView=memoryview(buf) |
56
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
24 fpos=bl=stream.readinto(buf) |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
25 bp=0 |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
26 done=False |
50 | 27 while True: |
56
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
28 while buf.startswith(b'\r\n',bp): # will Fail if buffer (nearly) empty |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
29 bp+=2 |
56
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
30 start_1=bp |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
31 if not buf.startswith(b'WARC/1.0\r\n',bp): |
56
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
32 if done and bl-bp==0: |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
33 # really done |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
34 return |
51
c0b4359dd26a
working better, gets confused by 3-part response
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
50
diff
changeset
|
35 raise ValueError("Not a WARC file? At %s: %s[%s]"%(bp, |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
36 buf[bp:min(bl,bp+20)].decode('latin-1'), bl-bp)) |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
37 bp+=10 |
39 | 38 wtype=None |
39 length=None | |
40 state=1 | |
41 tr=None # Was this record truncated? | |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
42 while not buf.startswith(b'\r\n',bp): |
56
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
43 # there should always be enough in the buffer to complete this loop, |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
44 # because of the buffer update logic below |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
45 eol=buf.index(b'\r\n',bp)+2 |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
46 if buf.startswith(b"Content-Length: ",bp): |
59
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
47 length=wl=int(bufView[bp+16:eol-2]) |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
48 elif buf.startswith(b"WARC-Truncated: ",bp): |
59
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
49 if bp+16==eol-2: |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
50 tr=b"EMPTY" |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
51 else: |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
52 tr=bytes(bufView[bp+16:eol-2]) |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
53 elif buf.startswith(b'WARC-Type: ',bp): |
59
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
54 if buf.startswith(b's',bp+13): |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
55 wtype = b'response' |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
56 elif buf.startswith(b'q',bp+13): |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
57 wtype = b'request' |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
58 elif buf.startswith(b'm',bp+11): |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
59 wtype = b'metadata' |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
60 elif buf.startswith(b'w',bp+11): |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
61 wtype = b'warcinfo' |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
62 else: |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
63 raise ValueError("Unknown WARC-Type: %s at %s"%( |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
64 bytes(bufView[bp+11:eol-2]), |
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
65 fpos-(bl-bp))) |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
66 bp=eol |
51
c0b4359dd26a
working better, gets confused by 3-part response
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
50
diff
changeset
|
67 bp=eol+2 |
57
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
68 if done: |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
69 if (bp+length)>bl: |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
70 raise ValueError("Done but need more! %s + %s > %s"%(bp, |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
71 length,bl)) |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
72 elif (bp+(length+hdrMax))>bl: |
56
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
73 # Need more data |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
74 if wtype in types: |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
75 # we need to keep from start_1 to bl |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
76 keepFrom=start_1 |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
77 keepLen=bl-keepFrom |
59
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
78 buf[0:keepLen]=bufView[keepFrom:bl] |
60 | 79 eol=eol-start_1 |
80 start_1=0 | |
81 bp=eol+2 | |
56
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
82 else: |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
83 # we can skip the rest of this part |
60 | 84 if (bp+length)<=bl: |
85 # we have at least some bytes from the next part | |
86 keepLen=bl-(bp+length) | |
87 buf[0:keepLen]=bufView[bl-keepLen:bl] | |
88 else: | |
89 # we don't have all of the bytes from the current part | |
90 # so can skip the rest of it | |
91 keepLen=0 | |
92 fpos=stream.seek(fpos+(bp+length-bl)) | |
93 bp=0 | |
57
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
94 spaceToFill=bufSize-keepLen |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
95 with memoryview(buf)[keepLen:bufSize] as xBuf: |
56
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
96 nb=stream.readinto(xBuf) |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
97 fpos+=nb |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
98 bl=keepLen+nb |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
99 if nb<spaceToFill: |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
100 done=True |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
101 if wtype not in types: |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
102 continue |
39 | 103 if (wtype in types): |
104 if whole: | |
61 | 105 bp+=length |
106 OUT=callback(wtype,bufView[start_1:bp],7) | |
107 continue | |
39 | 108 elif (parts & 1): |
59
5d40d7511374
avoid slicing buf by using memoryview to save copying
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
58
diff
changeset
|
109 OUT=callback(wtype,bufView[start_1:eol],1) |
50 | 110 if parts!=1: |
61 | 111 bv=bufView[start_2:start_2+length] |
112 ii=0 | |
113 while True and not stream.closed: | |
114 if (i:=stream.readinto(bv))==0: | |
115 break | |
116 ii+=i | |
117 if ii>=length: | |
118 break | |
119 bv=memoryview(buf)[start_2+ii:start_2+length] | |
120 if ii!=length: | |
121 raise ValueError("Chunk read losing, from %s got %s expected %s"%(nb,ii,length)) | |
122 nb+=length | |
123 if wtype in types: | |
124 if whole: | |
125 callback(wtype,bufView[0:start_2+length],7) | |
126 continue | |
127 # Only output parts (1 = WARC header, 2 = HTTP header, 4 = body) that are wanted | |
128 bl=None # for HTTP Content-Length for the length of the body? | |
129 L_start=start_2 | |
130 state=2 | |
131 bv=memoryview(buf)[start_2:start_2+length] | |
132 with io.BytesIO(bv) as rec_text: | |
133 for L in rec_text: | |
134 if state==2: | |
135 # HTTP header | |
136 wl -= len(L) | |
137 if not (L==b"" or L.startswith(b"\r")): | |
138 # Non-empty, it's (a continuation of) a header | |
139 if bl is None and L.startswith(b"Content-Length: "): | |
140 bl=int(L[16:].rstrip()) | |
141 else: | |
142 # Blank line, HTTP header is finished | |
143 if parts & 2: | |
144 callback(wtype,bufView[start_2:start_2+L_start],2) | |
145 state=4 | |
146 # The above is just for sanity, because we do _not_ | |
147 # continue with the outer loop, | |
148 # since we can now block-output the entire rest of the | |
149 # input buffer. | |
150 if bl is not None: | |
151 if bl!=wl: | |
152 print("length mismatch: %s %s %s here: %s given: %s trunc: %s"%\ | |
153 (length,offset,filename,wl,bl,tr),file=sys.stderr) | |
154 # HTTP body | |
155 balance=start_2+rec_text.tell() | |
156 #print(balance,bl,wl,ll,ll-balance,file=sys.stderr) | |
157 # Output whatever is left | |
158 if parts & 4: | |
159 callback(wtype,bufView[balance:balance+wl],4) | |
160 state=1 | |
161 | |
162 L_start=rec_text.tell() | |
58
299e3d0f2310
but skip at eobp is not working (with test 2)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
57
diff
changeset
|
163 bp+=length |
61 | 164 #print('end of loop',wtype,start_1,bp,eol,length,bl,file=sys.stderr) |
50 | 165 #while not buf.startswith(b'\r\n',bp): |
166 continue |