-
Notifications
You must be signed in to change notification settings - Fork 6
/
metabolize.go
153 lines (129 loc) · 2.73 KB
/
metabolize.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
package metabolize
import (
"fmt"
"golang.org/x/net/html"
"io"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
const (
TagName = `meta`
htmlRegion = `head`
htmlTag = `meta`
)
var (
NotStructError = fmt.Errorf(`Destination is not a struct`)
)
type MetaData map[string]string
type Meta struct {
Title string `meta:og:title`
Desc string `meta:og:image`
}
func Metabolize(doc io.Reader, obj interface{}) error {
data, err := ParseDocument(doc)
if err != nil {
return err
}
return Decode(data, obj)
}
func Decode(data MetaData, obj interface{}) error {
elem := reflect.ValueOf(obj).Elem()
if elem.Kind() != reflect.Struct {
return NotStructError
}
for i := 0; i < elem.NumField(); i++ {
field := elem.Type().Field(i)
fieldValue := elem.FieldByName(field.Name)
if !fieldValue.IsValid() {
continue
}
if !fieldValue.CanSet() {
continue
}
tag := field.Tag.Get(TagName)
if tag == "" {
continue
}
tags := strings.Split(tag, ",")
for _, tagItem := range tags {
if data[tagItem] == "" {
continue
}
if fieldValue.Kind() == reflect.String {
val := string(data[tagItem])
fieldValue.SetString(val)
}
if fieldValue.Kind() == reflect.Bool {
val, err := strconv.ParseBool(data[tagItem])
if err != nil {
continue
}
fieldValue.SetBool(val)
}
if fieldValue.Kind() == reflect.Float64 {
val, err := strconv.ParseFloat(data[tagItem], 64)
if err != nil {
continue
}
fieldValue.SetFloat(val)
}
if fieldValue.Kind() == reflect.Int64 {
val, err := strconv.ParseInt(data[tagItem], 0, 64)
if err != nil {
continue
}
fieldValue.SetInt(val)
}
if field.Type.Name() == "URL" {
val, err := url.Parse(data[tagItem])
if err != nil {
continue
}
fieldValue.Set(reflect.ValueOf(*val))
}
if field.Type.Name() == "Time" {
val, err := time.Parse(time.RFC3339, data[tagItem])
if err != nil {
continue
}
fieldValue.Set(reflect.ValueOf(val))
}
}
}
return nil
}
func ParseDocument(doc io.Reader) (MetaData, error) {
data := MetaData{}
tokenizer := html.NewTokenizer(doc)
for {
tt := tokenizer.Next()
if tt == html.ErrorToken {
if tokenizer.Err() == io.EOF {
return data, nil
}
return nil, tokenizer.Err()
}
token := tokenizer.Token()
if token.Type == html.EndTagToken && token.Data == htmlRegion {
return data, nil
}
if token.Data == htmlTag {
var property, content string
for _, attr := range token.Attr {
switch attr.Key {
case "property", "name":
property = strings.ToLower(attr.Val)
case "content":
content = attr.Val
}
}
if property != "" {
data[strings.TrimSpace(property)] = content
}
}
}
return data, nil
}