-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructures.go
195 lines (178 loc) · 5.04 KB
/
structures.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
package grabber
import (
"fmt"
"io"
"net/url"
"path"
"strings"
"time"
"golang.org/x/net/html"
)
type Page struct {
URL string `json:"url,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Keywords string `json:"keywords,omitempty"`
Author string `json:"author,omitempty"`
Favicon []string `json:"favicon,omitempty"`
OpenGraph *OpenGraph `json:"open_graph,omitempty"`
Article *Article `json:"article,omitempty"`
}
type Article struct {
PublishedTime *time.Time `json:"published_time,omitempty"`
ModifiedTime *time.Time `json:"modified_time,omitempty"`
Publisher string `json:"publisher,omitempty"`
Author string `json:"author,omitempty"`
Section []string `json:"section,omitempty"`
}
type OpenGraph struct {
Title string `json:"title,omitempty"`
Type string `json:"type,omitempty"`
URL string `json:"url,omitempty"`
Description string `json:"description,omitempty"`
Locale string `json:"locale,omitempty"`
SiteName string `json:"site_name,omitempty"`
UpdatedTime *time.Time `json:"updated_time,omitempty"`
Video []Video `json:"video,omitempty"`
Image []Image `json:"image,omitempty"`
Audio []Audio `json:"audio,omitempty"`
}
// Image represents a structure of "og:image".
// "og:image" might have following properties:
// - og:image:url
// - og:image:secure_url
// - og:image:type
// - og:image:width
// - og:image:height
// - og:image:alt
type Image struct {
URL string `json:"url,omitempty"`
SecureURL string `json:"secure_url,omitempty"`
Type string `json:"type,omitempty"` // Content-Type
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Alt string `json:"alt,omitempty"`
}
// Video represents a structure of "og:video".
// "og:video" might have following properties:
// - og:video:url
// - og:video:secure_url
// - og:video:type
// - og:video:width
// - og:video:height
// - og:video:tag
type Video struct {
URL string `json:"url,omitempty"`
SecureURL string `json:"secure_url,omitempty"`
Type string `json:"type,omitempty"` // Content-Type
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
// Duration in seconds
Duration int `json:"duration,omitempty"`
Tag []string `json:"tag,omitempty"`
}
// Audio represents a structure of "og:audio".
// "og:audio" might have following properties:
// - og:audio:url
// - og:audio:secure_url
// - og:audio:type
type Audio struct {
URL string `json:"url,omitempty"`
SecureURL string `json:"secure_url,omitempty"`
Type string `json:"type,omitempty"` // Content-Type
}
func NewPage(url string) *Page {
return &Page{
URL: url,
OpenGraph: new(OpenGraph),
Article: new(Article),
}
}
// Parse parses http.Response.Body and construct Page informations.
// Caller should close body after it gets parsed.
func (p *Page) Parse(body io.Reader) error {
tokens := html.NewTokenizer(body)
isTitle := false
for {
switch tokens.Next() {
case html.ErrorToken:
if err := tokens.Err(); err != io.EOF {
return err
}
return nil
case html.EndTagToken:
if tokens.Token().Data == "head" {
return nil
}
case html.StartTagToken:
t := tokens.Token()
isTitle = false
switch t.Data {
case "title":
isTitle = true
case "meta":
if err := MetaTag(t.Attr).Contribute(p); err != nil {
return err
}
case "link":
LinkTag(t.Attr).Contribute(p)
}
case html.TextToken:
if isTitle && p.Title == "" {
isTitle = false
p.Title = tokens.Token().Data
}
case html.SelfClosingTagToken:
t := tokens.Token()
switch t.Data {
case "meta":
if err := MetaTag(t.Attr).Contribute(p); err != nil {
return err
}
case "link":
LinkTag(t.Attr).Contribute(p)
}
default:
}
}
}
// ToAbs makes all relative URLs to absolute URLs
func (p *Page) ToAbs() error {
raw := p.URL
base, err := url.Parse(raw)
if err != nil {
return err
}
// For og:image.
for i, img := range p.OpenGraph.Image {
p.OpenGraph.Image[i].URL = joinToAbsolute(base, img.URL)
}
// For og:audio
for i, audio := range p.OpenGraph.Audio {
p.OpenGraph.Audio[i].URL = joinToAbsolute(base, audio.URL)
}
// For og:video
for i, video := range p.OpenGraph.Video {
p.OpenGraph.Video[i].URL = joinToAbsolute(base, video.URL)
}
if len(p.Favicon) == 0 {
p.Favicon = []string{"/favicon.ico"}
}
for i, favicon := range p.Favicon {
p.Favicon[i] = joinToAbsolute(base, favicon)
}
return nil
}
func joinToAbsolute(base *url.URL, relpath string) string {
src, err := url.Parse(relpath)
if err == nil && src.IsAbs() {
return src.String()
}
if strings.HasPrefix(relpath, "//") {
return fmt.Sprintf("%s:%s", base.Scheme, relpath)
}
if strings.HasPrefix(relpath, "/") {
return fmt.Sprintf("%s://%s%s", base.Scheme, base.Host, relpath)
}
return fmt.Sprintf("%s://%s%s", base.Scheme, base.Host, path.Join(base.Path, relpath))
}