-
Notifications
You must be signed in to change notification settings - Fork 7
/
meta_fetch.go
41 lines (32 loc) · 922 Bytes
/
meta_fetch.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
package urlzap
import (
"io"
"strings"
"github.com/PuerkitoBio/goquery"
)
type MetaData struct {
Title string
Tags []string
}
// GetMetaData Fetches meta data from URL, such as title, open graph and twitter data
func GetMetaData(html io.ReadCloser) (meta MetaData, err error) {
defer html.Close()
doc, err := goquery.NewDocumentFromReader(html)
if err != nil {
return meta, err
}
meta.Title = doc.Find("title").Text()
doc.Find("meta").Each(func(i int, s *goquery.Selection) {
if name, _ := s.Attr("name"); name == "description" || strings.Contains(name, "twitter:") {
if tag, err := goquery.OuterHtml(s); err == nil {
meta.Tags = append(meta.Tags, tag)
}
}
if property, _ := s.Attr("property"); strings.Contains(property, "og:") || property == "fb:app_id" {
if tag, err := goquery.OuterHtml(s); err == nil {
meta.Tags = append(meta.Tags, tag)
}
}
})
return meta, nil
}