/
info.go
182 lines (151 loc) · 4.84 KB
/
info.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
package oembed
import (
"encoding/json"
"strconv"
"io"
"github.com/Jeffail/gabs"
)
// Info returns information for embedding website
type Info struct {
Status int `json:"-"`
Type string `json:"type,omitempty"`
CacheAge uint64 `json:"cache_age,omitempty"`
URL string `json:"url,omitempty"`
ProviderURL string `json:"provider_url,omitempty"`
ProviderName string `json:"provider_name,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Width uint64 `json:"width,omitempty"`
Height uint64 `json:"height,omitempty"`
ThumbnailURL string `json:"thumbnail_url,omitempty"`
ThumbnailWidth uint64 `json:"thumbnail_width,omitempty"`
ThumbnailHeight uint64 `json:"thumbnail_height,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorURL string `json:"author_url,omitempty"`
HTML string `json:"html,omitempty"`
}
// NewInfo creater new instance of oembed.Info
func NewInfo() *Info {
return &Info{}
}
func jsonUint64(n json.Number) (uint64, error) {
return strconv.ParseUint(string(n), 10, 64)
}
// FillFromJSON fills the structure from provided Oembed JSON
func (info *Info) FillFromJSON(r io.Reader) error {
data := json.NewDecoder(r)
data.UseNumber()
// We are not using standard json parsing into struct mechanism because it sucks in real life..
// when you expect a string some sites will return string, some will return null but some will return false
// when you expect an integer, some will return integer, some will return string but some will return null or false..
jsonParsed, err := gabs.ParseJSONDecoder(data)
if err != nil {
return err
}
var strVal string
var jsonNumberVal json.Number
var jsonNumber uint64
var ok bool
if strVal, ok = jsonParsed.Path("type").Data().(string); ok {
info.Type = strVal
}
if jsonNumberVal, ok = jsonParsed.Path("cache_age").Data().(json.Number); ok {
if jsonNumber, err = jsonUint64(jsonNumberVal); err != nil {
return err
}
info.CacheAge = jsonNumber
}
if strVal, ok = jsonParsed.Path("url").Data().(string); ok {
info.URL = strVal
}
if strVal, ok = jsonParsed.Path("provider_url").Data().(string); ok {
info.ProviderURL = strVal
}
if strVal, ok = jsonParsed.Path("provider_name").Data().(string); ok {
info.ProviderName = strVal
}
if strVal, ok = jsonParsed.Path("title").Data().(string); ok {
info.Title = strVal
}
if strVal, ok = jsonParsed.Path("description").Data().(string); ok {
info.Description = strVal
}
if strVal, ok = jsonParsed.Path("summary").Data().(string); ok {
info.Description = strVal // stupid nyt oembed uses summary not description
}
if strVal, ok = jsonParsed.Path("thumbnail_url").Data().(string); ok {
info.ThumbnailURL = strVal
}
if strVal, ok = jsonParsed.Path("author_name").Data().(string); ok {
info.AuthorName = strVal
}
if strVal, ok = jsonParsed.Path("author_url").Data().(string); ok {
info.AuthorURL = strVal
}
if strVal, ok = jsonParsed.Path("html").Data().(string); ok {
info.HTML = strVal
}
if jsonNumberVal, ok = jsonParsed.Path("width").Data().(json.Number); ok {
if jsonNumber, err = jsonUint64(jsonNumberVal); err != nil {
return err
}
info.Width = jsonNumber
}
if jsonNumberVal, ok = jsonParsed.Path("height").Data().(json.Number); ok {
if jsonNumber, err = jsonUint64(jsonNumberVal); err != nil {
return err
}
info.Height = jsonNumber
}
if jsonNumberVal, ok = jsonParsed.Path("thumbnail_width").Data().(json.Number); ok {
if jsonNumber, err = jsonUint64(jsonNumberVal); err != nil {
return err
}
info.ThumbnailWidth = jsonNumber
}
if jsonNumberVal, ok = jsonParsed.Path("thumbnail_height").Data().(json.Number); ok {
if jsonNumber, err = jsonUint64(jsonNumberVal); err != nil {
return err
}
info.ThumbnailHeight = jsonNumber
}
return nil
}
// MergeWith adds missing data to the structure from another oembed.Info structure
func (info *Info) MergeWith(base *Info) {
if len(info.Type) == 0 {
info.Type = base.Type
}
if len(info.URL) == 0 {
info.URL = base.URL
}
if len(info.ProviderURL) == 0 {
info.ProviderURL = base.ProviderURL
}
if len(info.ProviderName) == 0 {
info.ProviderName = base.ProviderName
}
if len(info.Title) == 0 {
info.Title = base.Title
}
if len(info.Description) == 0 {
info.Description = base.Description
}
if len(info.ThumbnailURL) == 0 {
info.ThumbnailURL = base.ThumbnailURL
info.ThumbnailWidth = base.ThumbnailWidth
info.ThumbnailHeight = base.ThumbnailHeight
}
}
// ToJSON a simple wrapper around json.Marshal
func (info *Info) ToJSON() ([]byte, error) {
return json.Marshal(info)
}
// String return json representation of structure, or error string
func (info *Info) String() string {
data, err := info.ToJSON()
if err != nil {
return err.Error()
}
return string(data[:])
}