opengraph

package module
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2020 License: MIT Imports: 11 Imported by: 0

README

Open Graph Parser for Golang

Go codecov GoDoc

Code Example

package main

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

func main() {
	og, err := opengraph.Fetch("https://www.youtube.com/watch?v=5blm22DeeHY")
	fmt.Printf("OpenGraph: %+v\nError: %v\n", og, err)
}

CLI as a working example

% go get github.com/otiai10/opengraph/ogp
% ogp --help

For more details, see ogp/main.go.

Advanced

Documentation

Overview

Package opengraph implements and parses "The Open Graph Protocol" of web pages. See http://ogp.me/ for more information.

Index

Examples

Constants

View Source
const (
	// HTMLLinkTag is a tag name of <link>
	HTMLLinkTag string = "link"
	// HTMLMetaTag is a tag name of <meta>
	HTMLMetaTag string = "meta"
	// HTMLTitleTag is a tag name of <title>
	HTMLTitleTag string = "title"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Link struct {
	Rel  string
	Href string
}

Link represents any "<link ...>" HTML tag

func LinkTag

func LinkTag(n *html.Node) *Link

LinkTag constructs Link

func (*Link) Contribute

func (link *Link) Contribute(og *OpenGraph) error

Contribute contributes OpenGraph

func (*Link) IsCanonical added in v1.1.3

func (link *Link) IsCanonical() bool

IsCanonical returns if it can be "canonical" of *opengraph.OpenGraph

func (*Link) IsFavicon

func (link *Link) IsFavicon() bool

IsFavicon returns if it can be "favicon" of *opengraph.OpenGraph

type Meta

type Meta struct {
	Name     string
	Property string
	Content  string
}

Meta represents any "<meta ...>" HTML tag.

func MetaTag

func MetaTag(n *html.Node) *Meta

MetaTag constructs MetaTag.

func (*Meta) Contribute

func (m *Meta) Contribute(og *OpenGraph) error

Contribute ...

func (*Meta) IsDescription

func (m *Meta) IsDescription() bool

IsDescription returns if it can be "description" of OGP

func (*Meta) IsImage

func (m *Meta) IsImage() bool

IsImage returns if it can be a root of "og:image"

func (*Meta) IsImageProperty

func (m *Meta) IsImageProperty() bool

IsImageProperty returns if it can be a property of "og:image" struct

func (*Meta) IsOGDescription added in v1.1.3

func (m *Meta) IsOGDescription() bool

IsOGDescription returns if it can be "description" of OGP

func (*Meta) IsSiteName

func (m *Meta) IsSiteName() bool

IsSiteName returns if it can be "og:site_name"

func (*Meta) IsTitle

func (m *Meta) IsTitle() bool

IsTitle returns if it can be "title" of OGP

func (*Meta) IsType

func (m *Meta) IsType() bool

IsType returns if it can be "og:type"

func (*Meta) IsURL added in v1.1.3

func (m *Meta) IsURL() bool

IsURL returns if it can be "og:url"

type OGAudio

type OGAudio struct {
	URL  string
	SURL string
	Type string
}

OGAudio represents "og:audio" structure.

type OGImage

type OGImage struct {
	URL    string
	SURL   string
	Type   string
	Width  int
	Height int
	Alt    string
}

OGImage represents "og:image" structure.

type OGVideo

type OGVideo struct {
	URL    string
	SURL   string
	Type   string
	Width  int
	Height int
}

OGVideo represents "og:video" structure.

type OpenGraph

type OpenGraph struct {

	// Policy specifies a policy to parse HTML document.
	Policy struct {
		TrustedTags []string
	}

	// Basics
	Title    string
	Type     string
	URL      URL
	SiteName string

	// Structures
	Image []*OGImage
	Video []*OGVideo
	Audio []*OGAudio

	// Optionals
	Description string
	Determiner  string // TODO: enum?
	Locale      string
	LocaleAlt   []string

	// Additionals
	Favicon      string
	CanonicalURL string

	// Utils
	HTTPClient *http.Client `json:"-"`
	Error      error        `json:"-"`
}

OpenGraph represents web page information according to OGP <ogp.me>, and some more additional informations like URL.Host and so.

func Fetch

func Fetch(rawurl string, customHTTPClient ...*http.Client) (*OpenGraph, error)

Fetch creates and parses OpenGraph with specified URL.

Example
ogp, _ := Fetch("https://github.com/otiai10/gosseract")
fmt.Println(ogp.Title)
Output:

otiai10/gosseract

func FetchWithContext added in v1.1.3

func FetchWithContext(ctx context.Context, rawurl string, customHTTPClient ...*http.Client) (*OpenGraph, error)

FetchWithContext creates and parses OpenGraph with specified URL. Timeout can be handled with provided context.

func New

func New(rawurl string) *OpenGraph

New creates new OpenGraph struct with specified URL.

func (*OpenGraph) Fulfill

func (og *OpenGraph) Fulfill() error

Fulfill fulfills OG informations with some expectations.

func (*OpenGraph) Parse

func (og *OpenGraph) Parse(body io.Reader) error

Parse parses http.Response.Body and construct OpenGraph informations. Caller should close body after it get parsed.

Example
client := http.DefaultClient
res, err := client.Get("https://github.com/otiai10/amesh")
if err != nil {
	log.Fatal(err)
}
defer res.Body.Close()

ogp := new(OpenGraph)
ogp.Parse(res.Body)
fmt.Println(ogp.Title)
Output:

otiai10/amesh

func (*OpenGraph) ToAbsURL

func (og *OpenGraph) ToAbsURL() *OpenGraph

ToAbsURL make og.Image and og.Favicon absolute URL if relative.

type Title

type Title struct {
	Text string
}

Title represents any "<title ...>" HTML tag.

func TitleTag

func TitleTag(n *html.Node) *Title

TitleTag constructs Title.

func (*Title) Contribute

func (t *Title) Contribute(og *OpenGraph) error

Contribute contributes to OpenGraph

type URL

type URL struct {
	Source string
	*url.URL

	Value string
}

URL includes *url.URL

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL