Skip to content

Commit

Permalink
Almost complete rewrite of the package; also added a cli tool
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov committed May 24, 2022
1 parent dae8665 commit 3e69469
Show file tree
Hide file tree
Showing 19 changed files with 640 additions and 190 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
on:
release:
types: [created]

jobs:
releases-matrix:
name: Release Go Binary
runs-on: ubuntu-latest
strategy:
matrix:
# build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64
goos: [linux, windows, darwin]
goarch: ["386", amd64, arm64]
exclude:
- goarch: "386"
goos: darwin
- goarch: arm64
goos: windows
steps:
- uses: actions/checkout@v3
- uses: wangyoucao577/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://go.dev/dl/go1.18.2.linux-amd64.tar.gz"
project_path: "./cmd/opengraph"
binary_name: "opengraph"
extra_files: LICENSE README.md
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,31 @@ To download and install this package run:

*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)*

Methods:
## Command line tool

You can also use `opengraph` from CLI.
You can download latest version of `opengraph` for your OS from [Releases](https://github.com/dyatlov/go-opengraph/releases).

You can query website endpoints using the tool directly or use it with other tools for your own workflows.

Example usages:

```bash
# download and parse html page
./opengraph https://www.youtube.com/watch?v=yhoI42bdwU4
```

```bash
# parse piped html
curl https://www.youtube.com/watch?v=yhoI42bdwU4 | ./opengraph
```

```bash
# get video image
./opengraph https://www.youtube.com/watch?v=yhoI42bdwU4 | jq '.images[0].url'
```

## Package Methods

* `NewOpenGraph()` - create a new OpenGraph instance
* `ProcessHTML(buffer io.Reader) error` - process given html into underlying data structure
Expand Down
8 changes: 8 additions & 0 deletions cmd/opengraph/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module opengraph

go 1.7

require (
github.com/dyatlov/go-opengraph v0.0.0-20210112100619-dae8665a5b09
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
)
9 changes: 9 additions & 0 deletions cmd/opengraph/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/dyatlov/go-opengraph v0.0.0-20210112100619-dae8665a5b09 h1:AQLr//nh20BzN3hIWj2+/Gt3FwSs8Nwo/nz4hMIcLPg=
github.com/dyatlov/go-opengraph v0.0.0-20210112100619-dae8665a5b09/go.mod h1:nYia/MIs9OyvXXYboPmNOj0gVWo97Wx0sde+ZuKkoM4=
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 h1:NWy5+hlRbC7HK+PmcXVUmW1IMyFce7to56IUvhUFm7Y=
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
65 changes: 65 additions & 0 deletions cmd/opengraph/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// This is a simple program utilising dyatlov/go-opengraph library.
// It outputs Open Graph data either by downloading and parsing html
// or by reading html directly from a pipe
//
// Examples:
//
// Download and parse html page:
// ./opengraph https://www.youtube.com/watch?v=yhoI42bdwU4
//
// Parse piped html
// curl https://www.youtube.com/watch?v=yhoI42bdwU4 | ./opengraph
//

package main

import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"

"github.com/dyatlov/go-opengraph/opengraph"
)

func printHelp() {
fmt.Printf("Usage: %s <url>\n", os.Args[0])
os.Exit(0)
}

func main() {
var reader io.Reader

if len(os.Args) == 2 {
url := os.Args[1]
resp, err := http.Get(url)
if err != nil {
log.Fatalf("Error while fetching url %s: %s", url, err)
}

reader = resp.Body

defer resp.Body.Close()
} else if len(os.Args) == 1 {
fi, _ := os.Stdin.Stat()
if (fi.Mode() & os.ModeCharDevice) == 0 {
// pipe
reader = bufio.NewReader(os.Stdin)
} else {
printHelp()
}
} else {
printHelp()
}

og := opengraph.NewOpenGraph()
if err := og.ProcessHTML(reader); err != nil {
log.Fatalf("Error processing html: %s", err)
}

output, _ := json.MarshalIndent(og, "", " ")
fmt.Println(string(output))
}
Binary file added cmd/opengraph/opengraph
Binary file not shown.
4 changes: 2 additions & 2 deletions examples/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

func main() {
html := `<html><head><meta property="og:type" content="article" />
<meta property="og:title" content="WordPress 4.3 &quot;Billie&quot;" />
<meta property="og:url" content="https://wordpress.org/news/2015/08/billie/" /></head><body></body></html>`
<meta property="og:title" content="WordPress 4.3 &quot;Billie&quot;" />
<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))
Expand Down
5 changes: 5 additions & 0 deletions opengraph/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/dyatlov/go-opengraph/opengraph

go 1.7

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

0 comments on commit 3e69469

Please sign in to comment.