This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.go
141 lines (129 loc) · 3.54 KB
/
extract.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
// Resources is a package for extracting urls of dependant files for displaying a web page.
// It's primary use is for establishing the list of assets an archive would need to cache in order
// to properly display a target resource
package resources
import (
"bytes"
"github.com/datatogether/warc"
"golang.org/x/net/html"
)
type Extractor struct {
Tags map[string][]string
}
func NewExtractor() *Extractor {
return &Extractor{
Tags: extractTags(),
}
}
func (e Extractor) ExtractResponseUrls(rec *warc.Record) ([]string, error) {
rt := rec.Headers[warc.FieldNameWARCIdentifiedPayloadType]
switch rt {
case "text/html; charset=utf-8":
return e.ExtractHtmlResources(rec)
default:
// TODO - for now we just return nothing.
return nil, nil
// return nil, fmt.Errorf("can't extract urls from record with content type: '%s'", rt)
}
return nil, nil
}
func (e Extractor) ExtractHtmlResources(rec *warc.Record) (urls []string, err error) {
var p []byte
p, err = rec.Body()
if err != nil {
return
}
urls = []string{}
rdr := bytes.NewReader(p)
tokenizer := html.NewTokenizer(rdr)
for {
tt := tokenizer.Next()
// token := tokenizer.Token()
switch tt {
// case html.TextToken:
// case html.CommentToken:
case html.ErrorToken:
// ErrorToken means that an error occurred during tokenization.
// most common is end-of-file (EOF)
if tokenizer.Err().Error() == "EOF" {
return urls, nil
}
return urls, tokenizer.Err()
case html.StartTagToken:
name, hasAttr := tokenizer.TagName()
token := html.Token{
Type: html.StartTagToken,
Data: string(name),
}
if hasAttr {
e.extractUrls(&token, tokenizer, &urls)
}
continue
case html.SelfClosingTagToken:
name, hasAttr := tokenizer.TagName()
token := html.Token{
Type: html.SelfClosingTagToken,
Data: string(name),
}
if hasAttr {
e.extractUrls(&token, tokenizer, &urls)
}
continue
}
}
return urls, nil
}
func (e Extractor) extractUrls(t *html.Token, tok *html.Tokenizer, urls *[]string) {
attrs := e.Tags[t.Data]
for {
key, val, more := tok.TagAttr()
lk := string(bytes.ToLower(key))
for _, t := range attrs {
if t == lk {
*urls = append(*urls, string(val))
}
}
if !more {
return
}
}
return
}
func extractTags() map[string][]string {
// oe := PrefixRewriter{Prefix: []byte("oe_")}
// im := PrefixRewriter{Prefix: []byte("im_")}
// if_ := PrefixRewriter{Prefix: []byte("if_")}
// fr_ := PrefixRewriter{Prefix: []byte("fr_")}
// js_ := PrefixRewriter{Prefix: []byte("js_")}
return map[string][]string{
// "a": []string{"href"},
"applet": []string{"codebase", "archive"},
"area": []string{"href"},
"audio": []string{"src"},
"base": []string{"href"},
"blockquote": []string{"cite"},
"body": []string{"background"},
// "button": []string{"formaction"},
"command": []string{"icon"},
"del": []string{"cite"},
"embed": []string{"src"},
// "head": []string {"": defmod}, // for heang
// "iframe": []string{"src"},
"image": []string{"src", "xlink:href"},
"img": []string{"src", "srcset"},
"ins": []string{"cite"},
// "input": []string{"src", "formaction"},
"input": []string{"src"},
"form": []string{"action"},
"frame": []string{"src"},
"link": []string{"href"},
// "meta": []string{"content"},
"object": []string{"codebase", "data"},
"param": []string{"value"},
"q": []string{"cite"},
"ref": []string{"href"},
"script": []string{"src"},
"source": []string{"src"},
"video": []string{"src", "poster"},
}
}