-
Notifications
You must be signed in to change notification settings - Fork 0
/
opengraph.go
38 lines (32 loc) · 965 Bytes
/
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
/*
Package opengraph provide feature that to fetch website information defined by Open Graph protcol from specified url
The Open Graph protocol
https://ogp.me/
*/
package opengraph
import (
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Fetch the website information specified by OpenGraph protcol
func Fetch(url string) (map[string]interface{}, error) {
doc, err := goquery.NewDocument(url)
if err != nil {
return nil, err
}
ogTypePrefix := "og:"
ogTypeAttributeName := "property"
ogValueAttributeName := "content"
selection := doc.Find(fmt.Sprintf("meta[property^='%s']", ogTypePrefix))
og := make(map[string]interface{})
selection.Each(func(index int, s *goquery.Selection) {
if attr, exists := s.Attr(ogTypeAttributeName); exists == true {
ogTypeName := strings.Replace(attr, ogTypePrefix, "", 1)
if ogValue, exists := s.Attr(ogValueAttributeName); exists == true {
og[ogTypeName] = ogValue
}
}
})
return og, nil
}