googp

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2020 License: Apache-2.0 Imports: 11 Imported by: 1

README

googp

CircleCI Go Report Card GoDoc

googp is a OGP (Open Graph protocol) parser library for Golang.

Overviews

Usage

import (
    "fmt"
    "github.com/soranoba/googp"
)

type CustomOGP struct {
    Title       string   `googp:"og:title"`
    Description string   `googp:"-"`        // ignored
    images      []string                    // private field ignored
    Videos      []string `googp:"og:video,og:video:url"`
    Musics      Music    `googp:"music"`    // object type
}

func main() {
    ogp1 := new(googp.OGP)
    if err := googp.Fetch("https://soranoba.net", ogp1); err != nil {
        return
    }
    fmt.Println(ogp1)

    ogp2 := new(CustomOGP)
    if err := googp.Fetch("https://soranoba.net", ogp2); err != nil {
        return
    }
    fmt.Println(ogp2)
}

Object Mappings

Structured Properties
type OGP struct {
    Image struct {
        URL       `googp:"og:image,og:image:url"`
        SecureURL `googp:"og:image:secure_url"`
    } `googp:"og:image"`
}

You may collect in a struct by specifying the root tag.
In case of specifying og:image, googp collect values which property is og:image:*.

Arrays
type OGP struct {
    Image []string `googp:"og:image"`
}

googp collects values which the same properties.

Object Types

In googp, it same as Structured Properties.
You may define your own type yourself.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BadStatusCodeErr   = errors.New("Bad status code")
	UnsupportedPageErr = errors.New("Unsupported page")
)

Functions

func Fetch

func Fetch(rawurl string, i interface{}, opts ...ParserOpts) error

Fetch the content from the URL and parse OGP information.

Types

type Audio

type Audio struct {
	URL       string `googp:"og:audio,og:audio:url" json:"url,omitempty"`
	SecureURL string `googp:"og:audio:secure_url"   json:"secure_url,omitempty"`
	Type      string `googp:"og:audio:type"         json:"type,omitempty"`
}

type Image

type Image struct {
	URL       string `googp:"og:image,og:image:url" json:"url,omitempty"`
	SecureURL string `googp:"og:image:secure_url"   json:"secure_url,omitempty"`
	Type      string `googp:"og:image:type"         json:"type,omitempty"`
	Width     int    `googp:"og:image:width"        json:"width,omitempty"`
	Height    int    `googp:"og:image:height"       json:"height,omitempty"`
	Alt       string `googp:"og:image:alt"          json:"alt,omitempty"`
}

type Meta

type Meta struct {
	Property string
	Content  string
}

type OGP

type OGP struct {
	Title  string  `googp:"og:title" json:"title,omitempty"`
	Type   string  `googp:"og:type"  json:"type,omitempty"`
	URL    string  `googp:"og:url"   json:"url,omitempty"`
	Images []Image `googp:"og:image" json:"images,omitempty"`

	Audios          []Audio  `googp:"og:audio"            json:"audios,omitempty"`
	Description     string   `googp:"og:description"      json:"description,omitempty"`
	Determiner      string   `googp:"og:determiner"       json:"determiner,omitempty"`
	Locale          string   `googp:"og:locale"           json:"locale,omitempty"`
	LocaleAlternate []string `googp:"og:locale:alternate" json:"locale_alternate,omitempty"`
	SiteName        string   `googp:"og:site_name"        json:"site_name,omitempty"`
	Videos          []Video  `googp:"og:video"            json:"videos,omitempty"`
}

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is an OGP parser.

func NewParser

func NewParser(opts ...ParserOpts) *Parser

NewParser create a `Parser`

func (*Parser) Parse

func (parser *Parser) Parse(reader io.Reader, i interface{}) error

Parse OGPs from the HTML.

func (*Parser) ParseNode

func (parser *Parser) ParseNode(n *html.Node, i interface{}) error

Parse OGPs from the HTML node.

type ParserOpts

type ParserOpts struct {
	// You can add processing when you need to regard the Node in the `<head>` as `<meta>`.
	// For example, you can use it when you want to get the `<title>`.
	PreNodeFunc func(*html.Node) *Meta
	// You can add body to parse target.
	// If html have some meta tags in the body, you should set to true.
	IncludeBody bool
}

ParserOpts is option of `Parser`.

type Video

type Video struct {
	URL       string `googp:"og:video,og:video:url" json:"url,omitempty"`
	SecureURL string `googp:"og:video:secure_url"   json:"secure_url,omitempty"`
	Type      string `googp:"og:video:type"         json:"type,omitempty"`
	Width     int    `googp:"og:video:width"        json:"width,omitempty"`
	Height    int    `googp:"og:video:height"       json:"height,omitempty"`
}

Jump to

Keyboard shortcuts

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