-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
opengraph.go
63 lines (59 loc) · 2.65 KB
/
opengraph.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
package opengraph
import (
"github.com/PuerkitoBio/goquery"
)
type OpenGraph struct {
// The title of your object as it should appear within the graph,
// e.g., "The Rock". Property: og:title
Title string
// The type of your object, e.g., "video.movie". Depending on the
// type you specify, other properties may also be required.
// Property: og:type
Type string
// An image URL which should represent your object within the graph.
// Property: og:image
Image string
// The canonical URL of your object that will be used as its permanent ID in the graph, e.g., "https://www.imdb.com/title/tt0117500/".
// Property: og:url
URL string
// A URL to an audio file to accompany this object. Property: og:audio
Audio string
// A one to two sentence description of your object. Property: og:description
Description string
// The word that appears before this object's title in a sentence.
// An enum of (a, an, the, "", auto). If auto is chosen, the consumer
// of your data should chose between "a" or "an". Default is "" (blank).
// Property: og:determiner
Determiner string
// The locale these tags are marked up in. Of the format language_TERRITORY. Default is en_US.
// Property: og:locale
Locale string
// An array of other locales this page is available in.
// Property: og:locale:alternate
LocaleAlternate []string
// If your object is part of a larger web site, the name which should be displayed for the overall site. e.g., "IMDb".
// Property: og:site_name
SiteName string
// A URL to a video file that complements this object.
// Property: og:video
Video string
}
func NewOpenGraphMetadataFromDocument(doc *goquery.Document) OpenGraph {
return OpenGraph{
Title: doc.Find("head > meta[property='og:title']").AttrOr("content", ""),
Type: doc.Find("head > meta[property='og:type']").AttrOr("content", ""),
Image: doc.Find("head > meta[property='og:image']").AttrOr("content", ""),
URL: doc.Find("head > meta[property='og:url']").AttrOr("content", ""),
Audio: doc.Find("head > meta[property='og:audio']").AttrOr("content", ""),
Description: doc.Find("head > meta[property='og:description']").AttrOr("content", ""),
Determiner: doc.Find("head > meta[property='og:determiner']").AttrOr("content", ""),
Locale: doc.Find("head > meta[property='og:locale']").AttrOr("content", ""),
LocaleAlternate: doc.Find("head > meta[property='og:locale:alternate']").Map(
func(i int, s *goquery.Selection) string {
return s.AttrOr("content", "")
},
),
SiteName: doc.Find("head > meta[property='og:site_name']").AttrOr("content", ""),
Video: doc.Find("head > meta[property='og:video']").AttrOr("content", ""),
}
}