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