-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.go
86 lines (71 loc) · 1.93 KB
/
meta.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
package giawarc
import (
"bufio"
"fmt"
"io"
"log"
"net/http"
"strings"
"github.com/paracrawl/go-warc/warc"
"github.com/paracrawl/giawarc/cld2"
)
type WARCMetaProcessor struct {
wf *warc.WARCFile
Filename string
}
func NewWARCMetaProcessor(rc io.ReadCloser, filename string) (wp *WARCMetaProcessor, err error) {
var p WARCMetaProcessor
p.Filename = filename
p.wf, err = warc.NewWARCFile(rc)
if err != nil {
return
}
wp = &p
return
}
// Loop through each record and process it
func (p *WARCMetaProcessor) Process() {
reader := p.wf.GetReader()
reader.Iterate(p.processRecord)
}
func (p *WARCMetaProcessor) processRecord(wr *warc.WARCRecord, err error) {
if err != nil {
if err != io.EOF {
log.Printf("Error reading WARC record: %v", err)
}
return
}
// warc_type := wr.GetHeader().GetType()
// if warc_type == "warcinfo" {
// p.Filename, _ = wr.GetHeader().Get("WARC-Filename")
// }
// content type of the WARC record not the payload
content_type, _ := wr.GetHeader().Get("Content-Type")
if !strings.Contains(content_type,"application/http") || !strings.Contains(content_type,"response") {
return
}
uri, _ := wr.GetHeader().Get("WARC-Target-URI")
content_length := wr.GetHeader().GetContentLength()
date, _ := wr.GetHeader().Get("WARC-Date")
// get HTTP response out of the WARC file, and parse it
payload := wr.GetPayload()
resp, err := http.ReadResponse(bufio.NewReader(payload.GetReader()), nil)
reader := payload.GetReader()
charset := "utf-8"
if err != nil {
return
}
content_type, charset = CleanContentType(resp.Header.Get("Content-Type"))
var lang string
if IsText(content_type) {
text, err := CleanText(reader, charset)
if err == nil {
lang, _ = cld2.DetectLang(text)
} else {
log.Printf("Error reading content for %v: %v", uri, err)
}
}
recid := wr.GetHeader().GetRecordId()
fmt.Printf("%s\t%s\t%s\t%s\t%v\t%s\t%s\n",
p.Filename, recid, uri, date, content_length, content_type, lang)
}