opengraph

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: MIT Imports: 9 Imported by: 10

README

Open Graph Parser for Golang

Go implementation of https://ogp.me/

reference Go codecov Maintainability Go Report Card 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 get github.com/otiai10/opengraph/ogp
% ogp --help
% ogp -A otiai10.com

Just for fun 😉

Advanced usage

Set an option for fetching:

intent := opengraph.Intent{
	Context:     ctx,
	HTTPClient:  client,
	Strict:      true,
	TrustedTags: []string{"meta", "title"},
}
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

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 (
	// HTMLMetaTag is a tag name of <meta>
	HTMLMetaTag string = "meta"
	// HTMLLinkTag is a tag name of <link>
	HTMLLinkTag string = "link"
	// HTMLTitleTag is a tag name of <title>
	HTMLTitleTag string = "title"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Audio

type Audio struct {
	URL       string `json:"url"`
	SecureURL string `json:"secure_url"`
	Type      string `json:"type"` // Content-Type
}

Audio represents a structure of "og:audio". "og:audio" might have following properties:

  • og:audio:url
  • og:audio:secure_url
  • og:audio:type

type Favicon

type Favicon struct {
	URL string `json:"url"`
}

Favicon represents an extra structure for "shortcut icon".

type Image

type Image struct {
	URL       string `json:"url"`
	SecureURL string `json:"secure_url"`
	Type      string `json:"type"` // Content-Type
	Width     int    `json:"width"`
	Height    int    `json:"height"`
	Alt       string `json:"alt"`
}

Image represents a structure of "og:image". "og:image" might have following properties:

  • og:image:url
  • og:image:secure_url
  • og:image:type
  • og:image:width
  • og:image:height
  • og:image:alt

type Intent

type Intent struct {

	// URL of this intent to fetch an OGP.
	// This does NOT mean `og:url` of the page.
	URL string

	// Context of the web request of this Intent.
	Context context.Context
	// HTTP Client to be used for this intent.
	HTTPClient *http.Client

	// Scrict is just an alias of `TrustedTags`.
	// `Strict == true` means `TrustedTags = ["meta"]`,
	// and `Strict == false` means `TrustedTags == ["meta", "title", "link"]`.
	Strict bool
	// TrustedTags specify which tags to be respected.
	TrustedTags []string
}

Intent represents how to fetch, parse, and complete properties of this OpenGraph object. This SHOULD NOT have any meaning for "OpenGraph Protocol".

type Link struct {
	Rel  string
	Href string
}

Link represents any "<link ...>" HTML tag. <link> will NOT be used when Intent.String == true.

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) 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(node *html.Node) *Meta

MetaTag constructs MetaTag.

func (*Meta) Contribute

func (meta *Meta) Contribute(og *OpenGraph) (err error)

Contribute ...

func (*Meta) IsAudio

func (meta *Meta) IsAudio() bool

IsAudio reeturns if it can be a root of "og:audio"

func (*Meta) IsDescription

func (meta *Meta) IsDescription() bool

IsDescription returns if it can be "description" of OGP. CAUTION: This property SHOULD NOT be used when Intent.Strict == true.

func (*Meta) IsImage

func (meta *Meta) IsImage() bool

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

func (*Meta) IsOGDescription

func (meta *Meta) IsOGDescription() bool

IsOGDescription returns if it can be "description" of OGP

func (*Meta) IsPropertyOf

func (meta *Meta) IsPropertyOf(name string) bool

IsPropertyOf returns if it can be a property of specified struct

func (*Meta) IsSiteName

func (meta *Meta) IsSiteName() bool

IsSiteName returns if it can be "og:site_name"

func (*Meta) IsTitle

func (meta *Meta) IsTitle() bool

IsTitle returns if it can be "title" of OGP

func (*Meta) IsType

func (meta *Meta) IsType() bool

IsType returns if it can be "og:type"

func (*Meta) IsURL

func (meta *Meta) IsURL() bool

IsURL returns if it can be "og:url"

func (*Meta) IsVideo

func (meta *Meta) IsVideo() bool

IsVideo returns if it can be a root of "og:video"

type OpenGraph

type OpenGraph struct {

	// Basic Metadata
	// https://ogp.me/#metadata
	Title string  `json:"title"`
	Type  string  `json:"type"`
	Image []Image `json:"image"` // could be multiple
	URL   string  `json:"url"`

	// Optional Metadata
	// https://ogp.me/#optional
	Audio       []Audio  `json:"audio"` // could be multiple
	Description string   `json:"description"`
	Determiner  string   `json:"determiner"` // TODO: enum of (a, an, the, "", auto)
	Locale      string   `json:"locale"`
	LocaleAlt   []string `json:"locale_alternate"`
	SiteName    string   `json:"site_name"`
	Video       []Video  `json:"video"`

	// Additional (unofficial)
	Favicon Favicon `json:"favicon"`

	// Intent represents how to fetch, parse, and complete properties
	// of this OpenGraph object.
	// This SHOULD NOT have any meaning for "OpenGraph Protocol".
	Intent Intent `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(url string, intent ...Intent) (*OpenGraph, error)

Fetch creates and parses OpenGraph with specified URL.

Example
ogp, err := Fetch("https://ogp.me/")
fmt.Println("title:", ogp.Title)
fmt.Println("type:", ogp.Type)
fmt.Println("error:", err)
Output:

title: Open Graph protocol
type: website
error: <nil>

func New

func New(rawurl string) *OpenGraph

New constructs new OpenGraph struct and fill nullable fields.

func (*OpenGraph) Fetch

func (og *OpenGraph) Fetch() error

Fetch ...

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 gets parsed.

func (*OpenGraph) ToAbs

func (og *OpenGraph) ToAbs() error

ToAbs makes all relative URLs to absolute URLs by applying hostname of ogp.URL or Intent.URL.

func (*OpenGraph) Walk

func (og *OpenGraph) Walk(node *html.Node) error

Walk scans HTML nodes to pick up meaningful OGP data.

type Title

type Title struct {
	Text string
}

Title represents any "<title ...>" HTML tag. <title> will NOT be used when Intent.Strict == true.

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 Video

type Video struct {
	URL       string `json:"url"`
	SecureURL string `json:"secure_url"`
	Type      string `json:"type"` // Content-Type
	Width     int    `json:"width"`
	Height    int    `json:"height"`
	// Duration in seconds
	Duration int `json:"duration"`
}

Video represents a structure of "og:video". "og:video" might have following properties:

  • og:video:url
  • og:video:secure_url
  • og:video:type
  • og:video:width
  • og:video:height

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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