Skip to content

Commit a02887e

Browse files
committed
Initial commit
0 parents  commit a02887e

File tree

5 files changed

+659
-0
lines changed

5 files changed

+659
-0
lines changed

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
Go OpenGraph
2+
===
3+
4+
Parses given html data into Facebook OpenGraph structure.
5+
6+
To download and install this package run:
7+
8+
`go get github.com/dyatlov/go-opengraph/opengraph`
9+
10+
Methods:
11+
12+
* `NewOpenGraph()` - create a new OpenGraph instance
13+
* `ProcessHTML(buffer io.Reader) error` - process given html into underlying data structure
14+
* `ProcessMeta(metaAttrs map[string]string)` - add data to the structure based on meta attributes
15+
* `ToJSON() (string, error)` - return JSON representation of data or error
16+
* `String() string` - return JSON representation of structure
17+
18+
Source docs: http://godoc.org/github.com/dyatlov/go-opengraph/opengraph
19+
20+
If you just need to parse an OpenGraph data from HTML then method `ProcessHTML` is your needed one.
21+
22+
Example:
23+
24+
```golang
25+
package main
26+
27+
import (
28+
"fmt"
29+
"strings"
30+
31+
"github.com/dyatlov/go-opengraph/opengraph"
32+
)
33+
34+
func main() {
35+
html := `<html><head><meta property="og:type" content="article" />
36+
<meta property="og:title" content="WordPress 4.3 &quot;Billie&quot;" />
37+
<meta property="og:url" content="https://wordpress.org/news/2015/08/billie/" /></head><body></body></html>`
38+
39+
og := opengraph.NewOpenGraph()
40+
err := og.ProcessHTML(strings.NewReader(html))
41+
42+
if err != nil {
43+
fmt.Println(err)
44+
return
45+
}
46+
47+
fmt.Printf("Type: %s\n", og.Type)
48+
fmt.Printf("Title: %s\n", og.Title)
49+
fmt.Printf("URL: %s\n", og.URL)
50+
fmt.Printf("String/JSON Representation: %s\n", og)
51+
}
52+
```
53+
54+
If you have your own parsing engine and just need an intelligent OpenGraph parsing, then `ProcessMeta` is the method you need.
55+
While using this method you don't need to reparse your parsed html again, just feed it with meta atributes as they appear and OpenGraph will be built based on the data.
56+
57+
Example:
58+
59+
```golang
60+
package main
61+
62+
import (
63+
"fmt"
64+
"strings"
65+
66+
"golang.org/x/net/html"
67+
"github.com/dyatlov/go-opengraph/opengraph"
68+
)
69+
70+
func main() {
71+
h := `<html><head><meta property="og:type" content="article" />
72+
<meta property="og:title" content="WordPress 4.3 &quot;Billie&quot;" />
73+
<meta property="og:url" content="https://wordpress.org/news/2015/08/billie/" /></head><body></body></html>`
74+
75+
og := opengraph.NewOpenGraph()
76+
77+
doc, err := html.Parse(strings.NewReader(h))
78+
if err != nil {
79+
fmt.Println(err)
80+
return
81+
}
82+
83+
var parseHead func(*html.Node)
84+
parseHead = func(n *html.Node) {
85+
for c := n.FirstChild; c != nil; c = c.NextSibling {
86+
if c.Type == html.ElementNode && c.Data == "meta" {
87+
m := make(map[string]string)
88+
for _, a := range c.Attr {
89+
m[a.Key] = a.Val
90+
}
91+
92+
og.ProcessMeta(m)
93+
}
94+
}
95+
}
96+
97+
var f func(*html.Node)
98+
f = func(n *html.Node) {
99+
for c := n.FirstChild; c != nil; c = c.NextSibling {
100+
if c.Type == html.ElementNode {
101+
if c.Data == "head" {
102+
parseHead(c)
103+
continue
104+
} else if c.Data == "body" { // OpenGraph is only in head, so we don't need body
105+
break
106+
}
107+
}
108+
f(c)
109+
}
110+
}
111+
f(doc)
112+
113+
fmt.Printf("Type: %s\n", og.Type)
114+
fmt.Printf("Title: %s\n", og.Title)
115+
fmt.Printf("URL: %s\n", og.URL)
116+
fmt.Printf("String/JSON Representation: %s\n", og)
117+
}
118+
```

examples/advanced.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/dyatlov/go-opengraph/opengraph"
8+
"golang.org/x/net/html"
9+
)
10+
11+
func main() {
12+
h := `<html><head><meta property="og:type" content="article" />
13+
<meta property="og:title" content="WordPress 4.3 &quot;Billie&quot;" />
14+
<meta property="og:url" content="https://wordpress.org/news/2015/08/billie/" /></head><body></body></html>`
15+
16+
og := opengraph.NewOpenGraph()
17+
18+
doc, err := html.Parse(strings.NewReader(h))
19+
if err != nil {
20+
fmt.Println(err)
21+
return
22+
}
23+
24+
var parseHead func(*html.Node)
25+
parseHead = func(n *html.Node) {
26+
for c := n.FirstChild; c != nil; c = c.NextSibling {
27+
if c.Type == html.ElementNode && c.Data == "meta" {
28+
m := make(map[string]string)
29+
for _, a := range c.Attr {
30+
m[a.Key] = a.Val
31+
}
32+
33+
og.ProcessMeta(m)
34+
}
35+
}
36+
}
37+
38+
var f func(*html.Node)
39+
f = func(n *html.Node) {
40+
for c := n.FirstChild; c != nil; c = c.NextSibling {
41+
if c.Type == html.ElementNode {
42+
if c.Data == "head" {
43+
parseHead(c)
44+
continue
45+
} else if c.Data == "body" { // OpenGraph is only in head, so we don't need body
46+
break
47+
}
48+
}
49+
f(c)
50+
}
51+
}
52+
f(doc)
53+
54+
fmt.Printf("Type: %s\n", og.Type)
55+
fmt.Printf("Title: %s\n", og.Title)
56+
fmt.Printf("URL: %s\n", og.URL)
57+
fmt.Printf("String/JSON Representation: %s\n", og)
58+
}

examples/simple.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/dyatlov/go-opengraph/opengraph"
8+
)
9+
10+
func main() {
11+
html := `<html><head><meta property="og:type" content="article" />
12+
<meta property="og:title" content="WordPress 4.3 &quot;Billie&quot;" />
13+
<meta property="og:url" content="https://wordpress.org/news/2015/08/billie/" /></head><body></body></html>`
14+
15+
og := opengraph.NewOpenGraph()
16+
err := og.ProcessHTML(strings.NewReader(html))
17+
18+
if err != nil {
19+
fmt.Println(err)
20+
return
21+
}
22+
23+
fmt.Printf("Type: %s\n", og.Type)
24+
fmt.Printf("Title: %s\n", og.Title)
25+
fmt.Printf("URL: %s\n", og.URL)
26+
fmt.Printf("String/JSON Representation: %s\n", og)
27+
}

0 commit comments

Comments
 (0)