forked from datatogether/warc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
198 lines (191 loc) · 6.89 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package warc
import (
"fmt"
"strconv"
"time"
)
const (
content_length = "content-length"
content_type = "content-type"
warc_block_digest = "warc-block-digest"
warc_concurrent_to = "warc-concurrent-to"
warc_filename = "warc-filename"
warc_date = "warc-date"
warc_identified_payload_type = "warc-identified-payload-type"
warc_ip_address = "warc-ip-address"
warc_payload_digest = "warc-payload-digest"
warc_profile = "warc-profile"
warc_record_id = "warc-record-id"
warc_refers_to = "warc-refers-to"
warc_segment_origin_id = "warc-segment-origin-id"
warc_segment_number = "warc-segment-number"
warc_segment_total_length = "warc-segment-total-length"
warc_target_uri = "warc-target-uri"
warc_truncated = "warc-truncated"
warc_type = "warc-type"
warc_warcinfo_id = "warc-warcinfo-id"
)
func recordType(headers map[string]string) (t RecordType) {
switch headers[warc_type] {
case RecordTypeWarcInfo.String():
return RecordTypeWarcInfo
case RecordTypeResponse.String():
return RecordTypeResponse
case RecordTypeResource.String():
return RecordTypeResource
case RecordTypeRequest.String():
return RecordTypeRequest
case RecordTypeMetadata.String():
return RecordTypeMetadata
case RecordTypeRevisit.String():
return RecordTypeRevisit
case RecordTypeConversion.String():
return RecordTypeConversion
case RecordTypeContinuation.String():
return RecordTypeContinuation
}
return
}
func newRecord(h map[string]string, content []byte) (Record, error) {
warcDate, err := time.Parse(time.RFC3339, h[warc_date])
if err != nil {
return nil, err
}
contentLength, err := strconv.ParseInt(h[content_length], 10, 64)
if err != nil {
return nil, err
}
// fmt.Println(string(content))
// fmt.Println("* * * * * * * * *")
switch recordType(h) {
case RecordTypeWarcInfo:
return &WARCInfo{
WARCRecordId: h[warc_record_id],
WARCDate: warcDate,
ContentLength: contentLength,
ContentType: h[content_type],
WARCBlockDigest: h[warc_block_digest],
WARCPayloadDigest: h[warc_payload_digest],
WARCTruncated: h[warc_truncated],
WARCFilename: h[warc_filename],
Content: content,
}, nil
case RecordTypeResponse:
return &Response{
WARCRecordId: h[warc_record_id],
WARCDate: warcDate,
ContentLength: contentLength,
ContentType: h[content_type],
WARCConcurrentTo: h[warc_concurrent_to],
WARCBlockDigest: h[warc_block_digest],
WARCPayloadDigest: h[warc_payload_digest],
WARCIPAddress: h[warc_ip_address],
WARCTargetURI: h[warc_target_uri],
WARCTruncated: h[warc_truncated],
WARCWarcinfoID: h[warc_warcinfo_id],
WARCIdentifiedPayloadType: h[warc_identified_payload_type],
Content: content,
}, nil
case RecordTypeResource:
return &Resource{
WARCRecordId: h[warc_record_id],
WARCDate: warcDate,
ContentLength: contentLength,
ContentType: h[content_type],
WARCConcurrentTo: h[warc_concurrent_to],
WARCPayloadDigest: h[warc_payload_digest],
WARCBlockDigest: h[warc_block_digest],
WARCIPAddress: h[warc_ip_address],
WARCTargetURI: h[warc_target_uri],
WARCTruncated: h[warc_truncated],
WARCWarcinfoID: h[warc_warcinfo_id],
WARCIdentifiedPayloadType: h[warc_identified_payload_type],
Content: content,
}, nil
case RecordTypeRequest:
return &Request{
WARCRecordId: h[warc_record_id],
WARCDate: warcDate,
ContentLength: contentLength,
ContentType: h[content_type],
WARCConcurrentTo: h[warc_concurrent_to],
WARCBlockDigest: h[warc_block_digest],
WARCPayloadDigest: h[warc_payload_digest],
WARCIPAddress: h[warc_ip_address],
WARCTargetURI: h[warc_target_uri],
WARCTruncated: h[warc_truncated],
WARCWarcinfoID: h[warc_warcinfo_id],
WARCIdentifiedPayloadType: h[warc_identified_payload_type],
Content: content,
}, nil
case RecordTypeMetadata:
return &Metadata{
WARCRecordId: h[warc_record_id],
WARCDate: warcDate,
ContentLength: contentLength,
ContentType: h[content_type],
WARCConcurrentTo: h[warc_concurrent_to],
WARCBlockDigest: h[warc_block_digest],
WARCIPAddress: h[warc_ip_address],
WARCRefersTo: h[warc_refers_to],
WARCTargetURI: h[warc_target_uri],
WARCTruncated: h[warc_truncated],
WARCWarcinfoID: h[warc_warcinfo_id],
Content: content,
}, nil
case RecordTypeRevisit:
return &Revisit{
WARCRecordId: h[warc_record_id],
WARCDate: warcDate,
ContentLength: contentLength,
ContentType: h[content_type],
WARCConcurrentTo: h[warc_concurrent_to],
WARCBlockDigest: h[warc_block_digest],
WARCPayloadDigest: h[warc_payload_digest],
WARCIPAddress: h[warc_ip_address],
WARCRefersTo: h[warc_refers_to],
WARCTargetURI: h[warc_target_uri],
WARCTruncated: h[warc_truncated],
WARCWarcinfoID: h[warc_warcinfo_id],
WARCProfile: h[warc_profile],
Content: content,
}, nil
case RecordTypeConversion:
return &Conversion{
WARCRecordId: h[warc_record_id],
WARCDate: warcDate,
ContentLength: contentLength,
ContentType: h[content_type],
WARCBlockDigest: h[warc_block_digest],
WARCPayloadDigest: h[warc_payload_digest],
WARCRefersTo: h[warc_refers_to],
WARCTruncated: h[warc_truncated],
WARCWarcinfoID: h[warc_warcinfo_id],
Content: content,
}, nil
case RecordTypeContinuation:
seg, err := strconv.ParseInt(h[warc_segment_number], 10, 0)
if err != nil {
return nil, fmt.Errorf("error parsing WARCSegmentNumber: %s", err.Error())
}
length, err := strconv.ParseInt(h[warc_segment_total_length], 10, 64)
if err != nil {
return nil, fmt.Errorf("error parsing WARCSegmentTotalLength: %s", err.Error())
}
return &Continuation{
WARCRecordId: h[warc_record_id],
WARCDate: warcDate,
ContentLength: contentLength,
WARCBlockDigest: h[warc_block_digest],
WARCPayloadDigest: h[warc_payload_digest],
WARCTruncated: h[warc_truncated],
WARCWarcinfoID: h[warc_warcinfo_id],
WARCSegmentNumber: int(seg),
WARCSegmentOriginID: h[warc_segment_origin_id],
WARCSegmentTotalLength: length,
Content: content,
}, nil
default:
return nil, fmt.Errorf("unrecognized record format")
}
}