Skip to content

otiai10/opengraph

Repository files navigation

Open Graph Parser for Golang

Go implementation of https://ogp.me/

reference Go codecov Maintainability Go Report Card License: MIT FOSSA Status GitHub tag (latest SemVer)

Code Example

package main

import (
	"fmt"
	"github.com/otiai10/opengraph/v2"
)

func main() {
	ogp, err := opengraph.Fetch("https://github.com/")
	fmt.Println(ogp, err)
}

You can try CLI as a working example

% go install github.com/otiai10/opengraph/v2/cmd/ogp@latest
% ogp --help
% ogp -A otiai10.com

You can also use custom headers (useful for sites with bot protection):

% ogp -H "User-Agent: Mozilla/5.0" -H "Accept-Language: en-US" https://example.com

Just for fun 😉

Advanced usage

Set an option for fetching:

intent := opengraph.Intent{
	Context:     ctx,
	HTTPClient:  client,
	Strict:      true,
	TrustedTags: []string{"meta", "title"},
	Headers:     map[string]string{
		"User-Agent": "MyCustomBot/1.0",
		"Accept-Language": "en-US,en;q=0.9",
	},
}
ogp, err := opengraph.Fetch("https://ogp.me", intent)

Use any io.Reader as a data source:

f, _ := os.Open("my_test.html")
defer f.Close()
ogp := &opengraph.OpenGraph{}
err := ogp.Parse(f)

or if you already have parsed *html.Node:

err := ogp.Walk(node)

Do you wanna make absolute URLs?:

ogp.Image[0].URL // /logo.png
ogp.ToAbs()
ogp.Image[0].URL // https://ogp.me/logo.png

Issues

Packages

No packages published

Contributors 9