summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dyatlov/go-opengraph/opengraph/opengraph.go
blob: 96feb78a561b64aaf93696006be0aff618c9487a (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package opengraph

import (
	"encoding/json"
	"io"
	"strconv"
	"time"

	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

// Image defines Open Graph Image type
type Image struct {
	URL       string `json:"url"`
	SecureURL string `json:"secure_url"`
	Type      string `json:"type"`
	Width     uint64 `json:"width"`
	Height    uint64 `json:"height"`
	draft     bool    `json:"-"`
}

// Video defines Open Graph Video type
type Video struct {
	URL       string `json:"url"`
	SecureURL string `json:"secure_url"`
	Type      string `json:"type"`
	Width     uint64 `json:"width"`
	Height    uint64 `json:"height"`
	draft     bool    `json:"-"`
}

// Audio defines Open Graph Audio Type
type Audio struct {
	URL       string `json:"url"`
	SecureURL string `json:"secure_url"`
	Type      string `json:"type"`
	draft     bool    `json:"-"`
}

// Article contain Open Graph Article structure
type Article struct {
	PublishedTime  *time.Time `json:"published_time"`
	ModifiedTime   *time.Time `json:"modified_time"`
	ExpirationTime *time.Time `json:"expiration_time"`
	Section        string     `json:"section"`
	Tags           []string   `json:"tags"`
	Authors        []*Profile `json:"authors"`
}

// Profile contains Open Graph Profile structure
type Profile struct {
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	Username  string `json:"username"`
	Gender    string `json:"gender"`
}

// Book contains Open Graph Book structure
type Book struct {
	ISBN        string     `json:"isbn"`
	ReleaseDate *time.Time `json:"release_date"`
	Tags        []string   `json:"tags"`
	Authors     []*Profile `json:"authors"`
}

// OpenGraph contains facebook og data
type OpenGraph struct {
	isArticle        bool
	isBook           bool
	isProfile        bool
	Type             string   `json:"type"`
	URL              string   `json:"url"`
	Title            string   `json:"title"`
	Description      string   `json:"description"`
	Determiner       string   `json:"determiner"`
	SiteName         string   `json:"site_name"`
	Locale           string   `json:"locale"`
	LocalesAlternate []string `json:"locales_alternate"`
	Images           []*Image `json:"images"`
	Audios           []*Audio `json:"audios"`
	Videos           []*Video `json:"videos"`
	Article          *Article `json:"article,omitempty"`
	Book             *Book    `json:"book,omitempty"`
	Profile          *Profile `json:"profile,omitempty"`
}

// NewOpenGraph returns new instance of Open Graph structure
func NewOpenGraph() *OpenGraph {
	return &OpenGraph{}
}

// ToJSON a simple wrapper around json.Marshal
func (og *OpenGraph) ToJSON() ([]byte, error) {
	return json.Marshal(og)
}

// String return json representation of structure, or error string
func (og *OpenGraph) String() string {
	data, err := og.ToJSON()

	if err != nil {
		return err.Error()
	}

	return string(data[:])
}

// ProcessHTML parses given html from Reader interface and fills up OpenGraph structure
func (og *OpenGraph) ProcessHTML(buffer io.Reader) error {
	z := html.NewTokenizer(buffer)
	for {
		tt := z.Next()
		switch tt {
		case html.ErrorToken:
			if z.Err() == io.EOF {
				return nil
			}
			return z.Err()
		case html.StartTagToken, html.SelfClosingTagToken, html.EndTagToken:
			name, hasAttr := z.TagName()
			if atom.Lookup(name) == atom.Body {
				return nil // OpenGraph is only in head, so we don't need body
			}
			if atom.Lookup(name) != atom.Meta || !hasAttr {
				continue
			}
			m := make(map[string]string)
			var key, val []byte
			for hasAttr {
				key, val, hasAttr = z.TagAttr()
				m[atom.String(key)] = string(val)
			}
			og.ProcessMeta(m)
		}
	}
}

func (og *OpenGraph) ensureHasVideo() {
	if len(og.Videos) > 0 {
		return
	}
	og.Videos = append(og.Videos, &Video{draft: true})
}

func (og *OpenGraph) ensureHasImage() {
	if len(og.Images) > 0 {
		return
	}
	og.Images = append(og.Images, &Image{draft: true})
}

func (og *OpenGraph) ensureHasAudio() {
	if len(og.Audios) > 0 {
		return
	}
	og.Audios = append(og.Audios, &Audio{draft: true})
}

// ProcessMeta processes meta attributes and adds them to Open Graph structure if they are suitable for that
func (og *OpenGraph) ProcessMeta(metaAttrs map[string]string) {
	switch metaAttrs["property"] {
	case "og:description":
		og.Description = metaAttrs["content"]
	case "og:type":
		og.Type = metaAttrs["content"]
		switch og.Type {
		case "article":
			og.isArticle = true
		case "book":
			og.isBook = true
		case "profile":
			og.isProfile = true
		}
	case "og:title":
		og.Title = metaAttrs["content"]
	case "og:url":
		og.URL = metaAttrs["content"]
	case "og:determiner":
		og.Determiner = metaAttrs["content"]
	case "og:site_name":
		og.SiteName = metaAttrs["content"]
	case "og:locale":
		og.Locale = metaAttrs["content"]
	case "og:locale:alternate":
		og.LocalesAlternate = append(og.LocalesAlternate, metaAttrs["content"])
	case "og:audio":
		if len(og.Audios)>0 && og.Audios[len(og.Audios)-1].draft {
			og.Audios[len(og.Audios)-1].URL = metaAttrs["content"]
			og.Audios[len(og.Audios)-1].draft = false
		} else {
			og.Audios = append(og.Audios, &Audio{URL: metaAttrs["content"]})
		}
	case "og:audio:secure_url":
		og.ensureHasAudio()
		og.Audios[len(og.Audios)-1].SecureURL = metaAttrs["content"]
	case "og:audio:type":
		og.ensureHasAudio()
		og.Audios[len(og.Audios)-1].Type = metaAttrs["content"]
	case "og:image":
		if len(og.Images)>0 && og.Images[len(og.Images)-1].draft {
			og.Images[len(og.Images)-1].URL = metaAttrs["content"]
			og.Images[len(og.Images)-1].draft = false
		} else {
			og.Images = append(og.Images, &Image{URL: metaAttrs["content"]})
		}
	case "og:image:url":
		og.ensureHasImage()
		og.Images[len(og.Images)-1].URL = metaAttrs["content"]
	case "og:image:secure_url":
		og.ensureHasImage()
		og.Images[len(og.Images)-1].SecureURL = metaAttrs["content"]
	case "og:image:type":
		og.ensureHasImage()
		og.Images[len(og.Images)-1].Type = metaAttrs["content"]
	case "og:image:width":
		w, err := strconv.ParseUint(metaAttrs["content"], 10, 64)
		if err == nil {
			og.ensureHasImage()
			og.Images[len(og.Images)-1].Width = w
		}
	case "og:image:height":
		h, err := strconv.ParseUint(metaAttrs["content"], 10, 64)
		if err == nil {
			og.ensureHasImage()
			og.Images[len(og.Images)-1].Height = h
		}
	case "og:video":
		if len(og.Videos)>0 && og.Videos[len(og.Videos)-1].draft {
			og.Videos[len(og.Videos)-1].URL = metaAttrs["content"]
			og.Videos[len(og.Videos)-1].draft = false
		} else {
			og.Videos = append(og.Videos, &Video{URL: metaAttrs["content"]})
		}
	case "og:video:url":
		og.ensureHasVideo()
		og.Videos[len(og.Videos)-1].URL = metaAttrs["content"]
	case "og:video:secure_url":
		og.ensureHasVideo()
		og.Videos[len(og.Videos)-1].SecureURL = metaAttrs["content"]
	case "og:video:type":
		og.ensureHasVideo()
		og.Videos[len(og.Videos)-1].Type = metaAttrs["content"]
	case "og:video:width":
		w, err := strconv.ParseUint(metaAttrs["content"], 10, 64)
		if err == nil {
			og.ensureHasVideo()
			og.Videos[len(og.Videos)-1].Width = w
		}
	case "og:video:height":
		h, err := strconv.ParseUint(metaAttrs["content"], 10, 64)
		if err == nil {
			og.ensureHasVideo()
			og.Videos[len(og.Videos)-1].Height = h
		}
	default:
		if og.isArticle {
			og.processArticleMeta(metaAttrs)
		} else if og.isBook {
			og.processBookMeta(metaAttrs)
		} else if og.isProfile {
			og.processProfileMeta(metaAttrs)
		}
	}
}

func (og *OpenGraph) processArticleMeta(metaAttrs map[string]string) {
	if og.Article == nil {
		og.Article = &Article{}
	}
	switch metaAttrs["property"] {
	case "article:published_time":
		t, err := time.Parse(time.RFC3339, metaAttrs["content"])
		if err == nil {
			og.Article.PublishedTime = &t
		}
	case "article:modified_time":
		t, err := time.Parse(time.RFC3339, metaAttrs["content"])
		if err == nil {
			og.Article.ModifiedTime = &t
		}
	case "article:expiration_time":
		t, err := time.Parse(time.RFC3339, metaAttrs["content"])
		if err == nil {
			og.Article.ExpirationTime = &t
		}
	case "article:section":
		og.Article.Section = metaAttrs["content"]
	case "article:tag":
		og.Article.Tags = append(og.Article.Tags, metaAttrs["content"])
	case "article:author:first_name":
		if len(og.Article.Authors) == 0 {
			og.Article.Authors = append(og.Article.Authors, &Profile{})
		}
		og.Article.Authors[len(og.Article.Authors)-1].FirstName = metaAttrs["content"]
	case "article:author:last_name":
		if len(og.Article.Authors) == 0 {
			og.Article.Authors = append(og.Article.Authors, &Profile{})
		}
		og.Article.Authors[len(og.Article.Authors)-1].LastName = metaAttrs["content"]
	case "article:author:username":
		if len(og.Article.Authors) == 0 {
			og.Article.Authors = append(og.Article.Authors, &Profile{})
		}
		og.Article.Authors[len(og.Article.Authors)-1].Username = metaAttrs["content"]
	case "article:author:gender":
		if len(og.Article.Authors) == 0 {
			og.Article.Authors = append(og.Article.Authors, &Profile{})
		}
		og.Article.Authors[len(og.Article.Authors)-1].Gender = metaAttrs["content"]
	}
}

func (og *OpenGraph) processBookMeta(metaAttrs map[string]string) {
	if og.Book == nil {
		og.Book = &Book{}
	}
	switch metaAttrs["property"] {
	case "book:release_date":
		t, err := time.Parse(time.RFC3339, metaAttrs["content"])
		if err == nil {
			og.Book.ReleaseDate = &t
		}
	case "book:isbn":
		og.Book.ISBN = metaAttrs["content"]
	case "book:tag":
		og.Book.Tags = append(og.Book.Tags, metaAttrs["content"])
	case "book:author:first_name":
		if len(og.Book.Authors) == 0 {
			og.Book.Authors = append(og.Book.Authors, &Profile{})
		}
		og.Book.Authors[len(og.Book.Authors)-1].FirstName = metaAttrs["content"]
	case "book:author:last_name":
		if len(og.Book.Authors) == 0 {
			og.Book.Authors = append(og.Book.Authors, &Profile{})
		}
		og.Book.Authors[len(og.Book.Authors)-1].LastName = metaAttrs["content"]
	case "book:author:username":
		if len(og.Book.Authors) == 0 {
			og.Book.Authors = append(og.Book.Authors, &Profile{})
		}
		og.Book.Authors[len(og.Book.Authors)-1].Username = metaAttrs["content"]
	case "book:author:gender":
		if len(og.Book.Authors) == 0 {
			og.Book.Authors = append(og.Book.Authors, &Profile{})
		}
		og.Book.Authors[len(og.Book.Authors)-1].Gender = metaAttrs["content"]
	}
}

func (og *OpenGraph) processProfileMeta(metaAttrs map[string]string) {
	if og.Profile == nil {
		og.Profile = &Profile{}
	}
	switch metaAttrs["property"] {
	case "profile:first_name":
		og.Profile.FirstName = metaAttrs["content"]
	case "profile:last_name":
		og.Profile.LastName = metaAttrs["content"]
	case "profile:username":
		og.Profile.Username = metaAttrs["content"]
	case "profile:gender":
		og.Profile.Gender = metaAttrs["content"]
	}
}