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