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