func BenchmarkOpenGraph_ProcessHTML(b *testing.B) { og := opengraph.NewOpenGraph() b.ReportAllocs() b.SetBytes(int64(len(html))) for i := 0; i < b.N; i++ { if err := og.ProcessHTML(strings.NewReader(html)); err != nil { b.Fatal(err) } } }
func TestOpenGraphProcessHTML(t *testing.T) { og := opengraph.NewOpenGraph() err := og.ProcessHTML(strings.NewReader(html)) if err != nil { t.Fatal(err) } if og.Type != "article" { t.Error("type parsed incorrectly") } if len(og.Title) == 0 { t.Error("title parsed incorrectly") } if len(og.URL) == 0 { t.Error("url parsed incorrectly") } if len(og.Description) == 0 { t.Error("description parsed incorrectly") } if len(og.Images) == 0 { t.Error("images parsed incorrectly") } else { if len(og.Images[0].URL) == 0 { t.Error("image url parsed incorrectly") } } if len(og.Locale) == 0 { t.Error("locale parsed incorrectly") } if len(og.SiteName) == 0 { t.Error("site name parsed incorrectly") } if og.Article == nil { t.Error("articles parsed incorrectly") } else { ev, _ := time.Parse(time.RFC3339, "2015-08-18T19:12:38+00:00") if !og.Article.PublishedTime.Equal(ev) { t.Error("article published time parsed incorrectly") } } }
func main() { h := `<html><head><meta property="og:type" content="article" /> <meta property="og:title" content="WordPress 4.3 "Billie"" /> <meta property="og:url" content="https://wordpress.org/news/2015/08/billie/" /></head><body></body></html>` og := opengraph.NewOpenGraph() doc, err := html.Parse(strings.NewReader(h)) if err != nil { fmt.Println(err) return } var parseHead func(*html.Node) parseHead = func(n *html.Node) { for c := n.FirstChild; c != nil; c = c.NextSibling { if c.Type == html.ElementNode && c.Data == "meta" { m := make(map[string]string) for _, a := range c.Attr { m[a.Key] = a.Val } og.ProcessMeta(m) } } } var f func(*html.Node) f = func(n *html.Node) { for c := n.FirstChild; c != nil; c = c.NextSibling { if c.Type == html.ElementNode { if c.Data == "head" { parseHead(c) continue } else if c.Data == "body" { // OpenGraph is only in head, so we don't need body break } } f(c) } } f(doc) fmt.Printf("Type: %s\n", og.Type) fmt.Printf("Title: %s\n", og.Title) fmt.Printf("URL: %s\n", og.URL) fmt.Printf("String/JSON Representation: %s\n", og) }
func main() { html := `<html><head><meta property="og:type" content="article" /> <meta property="og:title" content="WordPress 4.3 "Billie"" /> <meta property="og:url" content="https://wordpress.org/news/2015/08/billie/" /></head><body></body></html>` og := opengraph.NewOpenGraph() err := og.ProcessHTML(strings.NewReader(html)) if err != nil { fmt.Println(err) return } fmt.Printf("Type: %s\n", og.Type) fmt.Printf("Title: %s\n", og.Title) fmt.Printf("URL: %s\n", og.URL) fmt.Printf("String/JSON Representation: %s\n", og) }
func TestOpenGraphProcessMeta(t *testing.T) { og := opengraph.NewOpenGraph() og.ProcessMeta(map[string]string{"property": "og:type", "content": "book"}) if og.Type != "book" { t.Error("wrong og:type processing") } og.ProcessMeta(map[string]string{"property": "book:isbn", "content": "123456"}) if og.Book == nil { t.Error("wrong book type processing") } else { if og.Book.ISBN != "123456" { t.Error("wrong book isbn processing") } } og.ProcessMeta(map[string]string{"property": "article:section", "content": "testsection"}) if og.Article != nil { t.Error("article processed when it should not be") } og.ProcessMeta(map[string]string{"property": "book:author:first_name", "content": "John"}) if og.Book != nil { if len(og.Book.Authors) == 0 { t.Error("book author was not processed") } else { if og.Book.Authors[0].FirstName != "John" { t.Error("author first name was processed incorrectly") } } } }
// NewHTMLInfo return new instance of HTMLInfo func NewHTMLInfo() *HTMLInfo { info := &HTMLInfo{AllowOembedFetching: true, AllowMainContentExtraction: true, OGInfo: opengraph.NewOpenGraph(), AcceptLanguage: "en-us"} return info }