Mercurial > hg > cc > cirrus_work
annotate bin/warc.py @ 49:699ef141af10
just barely working for 1, need to rethink buffering
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Thu, 06 Jul 2023 14:53:28 +0100 |
parents | d0d2fd9830d6 |
children | 55943918794e |
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) |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
19 hdrBuf=memoryview(buf)[:hdrMax] |
39 | 20 while not stream.closed: |
21 bp=0 | |
49
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
22 fpos=stream.tell() |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
23 bl=stream.readinto(hdrBuf) |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
24 if bl==0: |
43 | 25 break |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
26 while buf.startswith(b'\r\n',bp): |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
27 bp+=2 |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
28 if not buf.startswith(b'WARC/1.0\r\n',bp): |
49
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
29 raise ValueError("Not a WARC file? At %s: %s[%s]"%(fpos, |
48
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
30 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
|
31 bob=bp # in case 1 or whole |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
32 bp+=10 |
39 | 33 wtype=None |
34 length=None | |
35 state=1 | |
36 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
|
37 while not buf.startswith(b'\r\n',bp): |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
38 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
|
39 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
|
40 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
|
41 elif buf.startswith(b"WARC-Truncated: ",bp): |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
42 tr=l[bp+16:eol-2] |
39 | 43 tr="EMPTY" if tr=="" else tr |
48
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-Type: ',bp): |
d0d2fd9830d6
starting on conversion to direct-querying of buffer
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
46
diff
changeset
|
45 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
|
46 bp=eol |
49
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
47 start_2=eol+2 |
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
48 # need to read more if bp+length>hdrMax |
39 | 49 if (wtype in types): |
50 if whole: | |
49
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
51 pass # buf[bp:(bp:=bp+ln)]=l |
39 | 52 elif (parts & 1): |
49
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
53 callback(wtype,buf[bob:start_2],1) |
39 | 54 if parts==1: |
49
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
55 stream.seek(fpos+(bp-bob)+length) |
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
56 continue |
39 | 57 else: |
58 start_2=bp | |
59 else: | |
49
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
60 print(fpos,bp,bp-bob,length) |
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
61 stream.seek(fpos+(bp-bob)+length) |
699ef141af10
just barely working for 1, need to rethink buffering
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
48
diff
changeset
|
62 continue |
39 | 63 bv=memoryview(buf)[start_2:start_2+length] |
64 ii=0 | |
65 while True and not stream.closed: | |
66 if (i:=stream.readinto(bv))==0: | |
67 break | |
68 ii+=i | |
69 if ii>=length: | |
70 break | |
71 bv=memoryview(buf)[start_2+ii:start_2+length] | |
72 if ii!=length: | |
73 raise ValueError("Chunk read losing, from %s got %s expected %s"%(nb,ii,length)) | |
74 nb+=length | |
75 if wtype in types: | |
76 if whole: | |
77 callback(wtype,buf[0:start_2+length],7) | |
78 continue | |
79 # Only output parts (1 = WARC header, 2 = HTTP header, 4 = body) that are wanted | |
80 bl=None # for HTTP Content-Length for the length of the body? | |
81 L_start=start_2 | |
82 state=2 | |
83 bv=memoryview(buf)[start_2:start_2+length] | |
84 with io.BytesIO(bv) as rec_text: | |
85 for L in rec_text: | |
86 if state==2: | |
87 # HTTP header | |
88 wl -= len(L) | |
89 if not (L==b"" or L.startswith(b"\r")): | |
90 # Non-empty, it's (a continuation of) a header | |
91 if bl is None and L.startswith(b"Content-Length: "): | |
92 bl=int(L[16:].rstrip()) | |
93 else: | |
94 # Blank line, HTTP header is finished | |
95 if parts & 2: | |
96 callback(wtype,buf[start_2:start_2+L_start],2) | |
97 state=4 | |
98 # The above is just for sanity, because we do _not_ | |
99 # continue with the outer loop, | |
100 # since we can now block-output the entire rest of the | |
101 # input buffer. | |
102 if bl is not None: | |
103 if bl!=wl: | |
104 print("length mismatch: %s %s %s here: %s given: %s trunc: %s"%\ | |
105 (length,offset,filename,wl,bl,tr),file=sys.stderr) | |
106 # HTTP body | |
107 balance=start_2+rec_text.tell() | |
108 #print(balance,bl,wl,ll,ll-balance,file=sys.stderr) | |
109 # Output whatever is left | |
110 if parts & 4: | |
111 callback(wtype,buf[balance:balance+wl],4) | |
112 state=1 | |
113 | |
114 L_start=rec_text.tell() |