-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkii.go
148 lines (137 loc) · 3.41 KB
/
kii.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
package kii
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
)
func FromURL(u string) (result []string, err error) {
base, err := url.Parse(u)
if err != nil {
return result, err
}
res, err := http.Get(u)
if err != nil {
return result, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return result, errors.New(res.Status)
}
if (res.Request.Response != nil) {
base, err = res.Request.Response.Location()
if err != nil {
return result, err
}
}
result, err = FromReader(res.Body)
if err != nil {
return result, err
}
result = append(result, "/favicon.ico")
icons := []string{}
for _, i := range result {
u, err := url.Parse(i)
if err != nil {
continue
}
if !u.IsAbs() {
if u.Host != "" {
u.Scheme = base.Scheme
i = u.String()
} else {
i = base.ResolveReference(u).String()
}
}
icons = append(icons, i)
}
return icons, nil
}
func FromHTML(html string) (result []string, err error) {
return FromReader(strings.NewReader(html))
}
func FromReader(r io.Reader) (results []string, err error) {
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
return results, err
}
results = append(results, findJSONLD(doc)...)
results = append(results, findOpenGraph(doc)...)
results = append(results, findLink(doc)...)
results = append(results, findAppleTouchIcon(doc)...)
results = append(results, findTwitterCard(doc)...)
results = append(results, findMicrosoftTileImage(doc)...)
return results, nil
}
func findLink(doc *goquery.Document) (result []string) {
selector := `link[rel~="icon"]`
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
content, set := s.Attr("href")
if set && content != "" {
result = append(result, content)
}
})
return result
}
func findOpenGraph(doc *goquery.Document) (result []string) {
selector := `meta[property="og:image"]`
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
content, set := s.Attr("content")
if set && content != "" {
result = append(result, content)
}
})
return result
}
func findTwitterCard(doc *goquery.Document) (result []string) {
selector := `meta[name="twitter:image"]`
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
content, set := s.Attr("content")
if set && content != "" {
result = append(result, content)
}
})
return result
}
func findMicrosoftTileImage(doc *goquery.Document) (result []string) {
selector := `meta[name="msapplication-TileImage"]`
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
content, set := s.Attr("content")
if set && content != "" {
result = append(result, content)
}
})
return result
}
func findAppleTouchIcon(doc *goquery.Document) (result []string) {
selector := `link[rel="apple-touch-icon"]`
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
content, set := s.Attr("href")
if set && content != "" {
result = append(result, content)
}
})
return result
}
func findJSONLD(doc *goquery.Document) (result []string) {
selector := `script[type="application/ld+json"]`
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
d := &struct {
Logo string `json:"logo"`
Image string `json:"image"`
}{}
err := json.Unmarshal([]byte(s.Text()), d)
if err == nil {
if d.Logo != "" {
result = append(result, d.Logo)
}
if d.Image != "" {
result = append(result, d.Image)
}
}
})
return result
}