-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopengraph_parser.go
50 lines (45 loc) · 1.46 KB
/
opengraph_parser.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
// Implements the basic Open Graph parser ( http://ogp.me/ )
// Currently we only parse Title, Description, Type and the first Image
package unfurlist
import (
"bytes"
"net/http"
"strings"
"golang.org/x/net/html/charset"
"github.com/dyatlov/go-opengraph/opengraph"
)
func openGraphParseHTML(chunk *pageChunk) *unfurlResult {
if !strings.HasPrefix(http.DetectContentType(chunk.data), "text/html") {
return nil
}
// use explicit content type received from headers here but not the one returned by
// http.DetectContentType because this function scans only first 512
// bytes and can report content as "text/html; charset=utf-8" even for
// bodies having characters outside utf8 range later; use
// charset.NewReader that relies on charset.DetermineEncoding which
// implements more elaborate encoding detection specific to html content
bodyReader, err := charset.NewReader(bytes.NewReader(chunk.data), chunk.ct)
if err != nil {
return nil
}
og := opengraph.NewOpenGraph()
err = og.ProcessHTML(bodyReader)
if err != nil || og.Title == "" {
return nil
}
res := &unfurlResult{
Type: og.Type,
Title: og.Title,
Description: og.Description,
SiteName: og.SiteName,
}
if len(og.Images) > 0 {
res.Image = og.Images[0].URL
}
if chunk.url.Host == "twitter.com" &&
strings.Contains(chunk.url.Path, "/status/") &&
!bytes.Contains(chunk.data, []byte(`property="og:image:user_generated" content="true"`)) {
res.Image = ""
}
return res
}