Mercurial > hg > cc > cirrus_work
annotate bin/warc.py @ 58:299e3d0f2310
but skip at eobp is not working (with test 2)
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Wed, 12 Jul 2023 19:07:56 +0100 |
parents | 61b0a1582af8 |
children | 5d40d7511374 |
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): |
39 | 10 types=[(t if isinstance(t,bytes) else bytes(t,'utf8')) for t in types] |
11 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
|
12 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
|
13 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
|
14 else: |
44d3a4f4ea51
support on-board unzipping, reduce buffer size to 2MB
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
43
diff
changeset
|
15 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
|
16 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
|
17 hdrMax=16*1024 |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
18 buf=bytearray(bufSize) |
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
|
19 #with memoryview(buf)[:hdrMax] as hdrBuf: |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
20 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
|
21 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
|
22 done=False |
50 | 23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 # 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
|
30 return |
51
c0b4359dd26a
working better, gets confused by 3-part response
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
50
diff
changeset
|
31 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
|
32 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
|
33 bp+=10 |
39 | 34 wtype=None |
35 length=None | |
36 state=1 | |
37 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
|
38 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
|
39 # 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
|
40 # 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
|
41 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
|
42 if buf.startswith(b"Content-Length: ",bp): |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
43 length=wl=int(buf[bp+16:eol-2]) |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
44 elif buf.startswith(b"WARC-Truncated: ",bp): |
57
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
45 tr=bytes(buf[bp+16:eol-2]) |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
46 tr=b"EMPTY" if tr==b"" else tr |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
47 elif buf.startswith(b'WARC-Type: ',bp): |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
48 wtype = bytes(buf[bp+11:eol-2]) |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
49 bp=eol |
51
c0b4359dd26a
working better, gets confused by 3-part response
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
50
diff
changeset
|
50 bp=eol+2 |
57
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
51 if done: |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
52 if (bp+length)>bl: |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
53 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
|
54 length,bl)) |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
55 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
|
56 # 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
|
57 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
|
58 # 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
|
59 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
|
60 keepLen=bl-keepFrom |
57
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
61 buf[0:keepLen]=buf[keepFrom: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
|
62 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
|
63 # we can skip the rest of this part |
f8c8f79b2532
rework completely to refill as much as possible only when necessary,
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
54
diff
changeset
|
64 keepLen=0 |
57
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
65 fpos=stream.seek(fpos+(bp+length-bl)) |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
66 spaceToFill=bufSize-keepLen |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
67 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
|
68 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
|
69 fpos+=nb |
57
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
70 eol=eol-start_1 |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
71 start_1=0 |
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 continue |
39 | 78 if (wtype in types): |
79 if whole: | |
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
|
80 pass # buf[bp:(bp:=bp+ln)]=l @fixme |
39 | 81 elif (parts & 1): |
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 OUT=callback(wtype,buf[start_1:eol],1) |
50 | 83 if parts!=1: |
84 # everything from bv= goes here | |
85 pass | |
58
299e3d0f2310
but skip at eobp is not working (with test 2)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
57
diff
changeset
|
86 bp+=length |
57
61b0a1582af8
works with all types, part=1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
56
diff
changeset
|
87 print('end of loop',wtype,start_1,bp,eol,length,bl,file=sys.stderr) |
50 | 88 #while not buf.startswith(b'\r\n',bp): |
89 continue | |
39 | 90 bv=memoryview(buf)[start_2:start_2+length] |
91 ii=0 | |
92 while True and not stream.closed: | |
93 if (i:=stream.readinto(bv))==0: | |
94 break | |
95 ii+=i | |
96 if ii>=length: | |
97 break | |
98 bv=memoryview(buf)[start_2+ii:start_2+length] | |
99 if ii!=length: | |
100 raise ValueError("Chunk read losing, from %s got %s expected %s"%(nb,ii,length)) | |
101 nb+=length | |
102 if wtype in types: | |
103 if whole: | |
104 callback(wtype,buf[0:start_2+length],7) | |
105 continue | |
106 # Only output parts (1 = WARC header, 2 = HTTP header, 4 = body) that are wanted | |
107 bl=None # for HTTP Content-Length for the length of the body? | |
108 L_start=start_2 | |
109 state=2 | |
110 bv=memoryview(buf)[start_2:start_2+length] | |
111 with io.BytesIO(bv) as rec_text: | |
112 for L in rec_text: | |
113 if state==2: | |
114 # HTTP header | |
115 wl -= len(L) | |
116 if not (L==b"" or L.startswith(b"\r")): | |
117 # Non-empty, it's (a continuation of) a header | |
118 if bl is None and L.startswith(b"Content-Length: "): | |
119 bl=int(L[16:].rstrip()) | |
120 else: | |
121 # Blank line, HTTP header is finished | |
122 if parts & 2: | |
123 callback(wtype,buf[start_2:start_2+L_start],2) | |
124 state=4 | |
125 # The above is just for sanity, because we do _not_ | |
126 # continue with the outer loop, | |
127 # since we can now block-output the entire rest of the | |
128 # input buffer. | |
129 if bl is not None: | |
130 if bl!=wl: | |
131 print("length mismatch: %s %s %s here: %s given: %s trunc: %s"%\ | |
132 (length,offset,filename,wl,bl,tr),file=sys.stderr) | |
133 # HTTP body | |
134 balance=start_2+rec_text.tell() | |
135 #print(balance,bl,wl,ll,ll-balance,file=sys.stderr) | |
136 # Output whatever is left | |
137 if parts & 4: | |
138 callback(wtype,buf[balance:balance+wl],4) | |
139 state=1 | |
140 | |
141 L_start=rec_text.tell() |