Skip to content

Commit 3e69469

Browse files
committed
Almost complete rewrite of the package; also added a cli tool
1 parent dae8665 commit 3e69469

File tree

19 files changed

+640
-190
lines changed

19 files changed

+640
-190
lines changed

.github/workflows/release.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
on:
2+
release:
3+
types: [created]
4+
5+
jobs:
6+
releases-matrix:
7+
name: Release Go Binary
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
# build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64
12+
goos: [linux, windows, darwin]
13+
goarch: ["386", amd64, arm64]
14+
exclude:
15+
- goarch: "386"
16+
goos: darwin
17+
- goarch: arm64
18+
goos: windows
19+
steps:
20+
- uses: actions/checkout@v3
21+
- uses: wangyoucao577/go-release-action@v1.28
22+
with:
23+
github_token: ${{ secrets.GITHUB_TOKEN }}
24+
goos: ${{ matrix.goos }}
25+
goarch: ${{ matrix.goarch }}
26+
goversion: "https://go.dev/dl/go1.18.2.linux-amd64.tar.gz"
27+
project_path: "./cmd/opengraph"
28+
binary_name: "opengraph"
29+
extra_files: LICENSE README.md

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,31 @@ To download and install this package run:
99

1010
*NOTE: if you need to grab as much info from a page as possible consider using [dyatlov/go-htmlinfo](https://github.com/dyatlov/go-htmlinfo)*
1111

12-
Methods:
12+
## Command line tool
13+
14+
You can also use `opengraph` from CLI.
15+
You can download latest version of `opengraph` for your OS from [Releases](https://github.com/dyatlov/go-opengraph/releases).
16+
17+
You can query website endpoints using the tool directly or use it with other tools for your own workflows.
18+
19+
Example usages:
20+
21+
```bash
22+
# download and parse html page
23+
./opengraph https://www.youtube.com/watch?v=yhoI42bdwU4
24+
```
25+
26+
```bash
27+
# parse piped html
28+
curl https://www.youtube.com/watch?v=yhoI42bdwU4 | ./opengraph
29+
```
30+
31+
```bash
32+
# get video image
33+
./opengraph https://www.youtube.com/watch?v=yhoI42bdwU4 | jq '.images[0].url'
34+
```
35+
36+
## Package Methods
1337

1438
* `NewOpenGraph()` - create a new OpenGraph instance
1539
* `ProcessHTML(buffer io.Reader) error` - process given html into underlying data structure

cmd/opengraph/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module opengraph
2+
3+
go 1.7
4+
5+
require (
6+
github.com/dyatlov/go-opengraph v0.0.0-20210112100619-dae8665a5b09
7+
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
8+
)

cmd/opengraph/go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/dyatlov/go-opengraph v0.0.0-20210112100619-dae8665a5b09 h1:AQLr//nh20BzN3hIWj2+/Gt3FwSs8Nwo/nz4hMIcLPg=
2+
github.com/dyatlov/go-opengraph v0.0.0-20210112100619-dae8665a5b09/go.mod h1:nYia/MIs9OyvXXYboPmNOj0gVWo97Wx0sde+ZuKkoM4=
3+
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 h1:NWy5+hlRbC7HK+PmcXVUmW1IMyFce7to56IUvhUFm7Y=
4+
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
5+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
8+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
9+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

cmd/opengraph/main.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// This is a simple program utilising dyatlov/go-opengraph library.
2+
// It outputs Open Graph data either by downloading and parsing html
3+
// or by reading html directly from a pipe
4+
//
5+
// Examples:
6+
//
7+
// Download and parse html page:
8+
// ./opengraph https://www.youtube.com/watch?v=yhoI42bdwU4
9+
//
10+
// Parse piped html
11+
// curl https://www.youtube.com/watch?v=yhoI42bdwU4 | ./opengraph
12+
//
13+
14+
package main
15+
16+
import (
17+
"bufio"
18+
"encoding/json"
19+
"fmt"
20+
"io"
21+
"log"
22+
"net/http"
23+
"os"
24+
25+
"github.com/dyatlov/go-opengraph/opengraph"
26+
)
27+
28+
func printHelp() {
29+
fmt.Printf("Usage: %s <url>\n", os.Args[0])
30+
os.Exit(0)
31+
}
32+
33+
func main() {
34+
var reader io.Reader
35+
36+
if len(os.Args) == 2 {
37+
url := os.Args[1]
38+
resp, err := http.Get(url)
39+
if err != nil {
40+
log.Fatalf("Error while fetching url %s: %s", url, err)
41+
}
42+
43+
reader = resp.Body
44+
45+
defer resp.Body.Close()
46+
} else if len(os.Args) == 1 {
47+
fi, _ := os.Stdin.Stat()
48+
if (fi.Mode() & os.ModeCharDevice) == 0 {
49+
// pipe
50+
reader = bufio.NewReader(os.Stdin)
51+
} else {
52+
printHelp()
53+
}
54+
} else {
55+
printHelp()
56+
}
57+
58+
og := opengraph.NewOpenGraph()
59+
if err := og.ProcessHTML(reader); err != nil {
60+
log.Fatalf("Error processing html: %s", err)
61+
}
62+
63+
output, _ := json.MarshalIndent(og, "", " ")
64+
fmt.Println(string(output))
65+
}

cmd/opengraph/opengraph

6.16 MB
Binary file not shown.

examples/simple.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
func main() {
1111
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>`
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>`
1414

1515
og := opengraph.NewOpenGraph()
1616
err := og.ProcessHTML(strings.NewReader(html))

opengraph/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/dyatlov/go-opengraph/opengraph
2+
3+
go 1.7
4+
5+
require golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2

opengraph/go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 h1:NWy5+hlRbC7HK+PmcXVUmW1IMyFce7to56IUvhUFm7Y=
2+
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
3+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
6+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
7+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

0 commit comments

Comments
 (0)