oembed

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2019 License: MIT Imports: 15 Imported by: 0

README

oEmbed

oEmbed is a format for allowing an embedded representation of a URL on third party sites. The simple API allows a website to display embedded content (such as photos or videos) when a user posts a link to that resource, without having to parse the resource directly.

Start using oEmbed

Download and install it:
$ go get -u gitlab.com/toby3d/oembed

Import it in your code:
import "gitlab.com/toby3d/oembed"

Example

package main

import "gitlab.com/toby3d/oembed"

var targetUrl = "https://www.youtube.com/watch?v=8jPQjjsBbIc"

func main() {
  // optional: checks what url has YouTube provider
  if !oembed.HasProvider(targetUrl) {
    return
  }

  // extract oEmbed object of source url
  data, err := oembed.Extract(targetUrl)
  if err != nil {
    // provider not found / source not found / bad response...
    panic(err)
  }

  // use data as you want
}

Documentation

Index

Constants

View Source
const SourceURL string = "https://oembed.com/providers.json"

SourceURL is a official url of supported providers list

Variables

View Source
var Providers []Provider //nolint:gochecknoglobals

Providers contains all default (or new synced) providers

Functions

func HasProvider

func HasProvider(url string) bool

HasProvider checks what input url has oEmbed provider

func Sync

func Sync(sourceURL string) error

Sync try update Providers variable via request and parsing body of sourceURL

Types

type Endpoint

type Endpoint struct {
	Schemes   []string `json:"schemes,omitempty"`
	URL       string   `json:"url"`
	Discovery bool     `json:"discovery,omitempty"`
	Formats   []string `json:"formats,omitempty"`
}

Provider represent a single endpoint of Provider

func (*Endpoint) MarshalJSON

func (j *Endpoint) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*Endpoint) MarshalJSONBuf

func (j *Endpoint) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*Endpoint) UnmarshalJSON

func (j *Endpoint) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*Endpoint) UnmarshalJSONFFLexer

func (j *Endpoint) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type Error

type Error struct {
	Message string
	URL     string
	Details xerrors.Frame
}

Error represent a complex error

func (Error) Error

func (e Error) Error() string

Error returns a string formatted error

func (Error) Format

func (e Error) Format(f fmt.State, c rune)

Format implements fmt.Formatter method

func (Error) FormatError

func (e Error) FormatError(p xerrors.Printer) error

FormatError implements xerrors.Formatter method

type OEmbed

type OEmbed struct {
	// The resource type.
	Type string `json:"type"` // required

	// The oEmbed version number.
	Version string `json:"version"` // required

	// A text title, describing the resource.
	Title string `json:"title,omitempty"`

	// The name of the author/owner of the resource.
	AuthorName string `json:"author_name,omitempty"`

	// A URL for the author/owner of the resource.
	AuthorURL string `json:"author_url,omitempty"`

	// The name of the resource provider.
	ProviderName string `json:"provider_name,omitempty"`

	// The url of the resource provider.
	ProviderURL string `json:"provider_url,omitempty"`

	// The suggested cache lifetime for this resource, in seconds.
	// Consumers may choose to use this value or not.
	CacheAge int `json:"cache_age,omitempty"`

	// A URL to a thumbnail image representing the resource.
	// The thumbnail must respect any maxwidth and maxheight parameters.
	// If this parameter is present, thumbnail_width and thumbnail_height must also be present.
	ThumbnailURL string `json:"thumbnail_url,omitempty"`

	// The width of the optional thumbnail.
	// If this parameter is present, thumbnail_url and thumbnail_height must also be present.
	ThumbnailWidth int `json:"thumbnail_width,omitempty"`

	// The height of the optional thumbnail.
	// If this parameter is present, thumbnail_url and thumbnail_width must also be present.
	ThumbnailHeight int `json:"thumbnail_height,omitempty"`

	URL string `json:"url,omitempty"`
}

Response can specify a resource type, such as photo or video. Each type has specific parameters associated with it.

func Extract

func Extract(url string, params *Params) (*OEmbed, error)

Extract try fetch oEmbed object for input url with params (if represent). Return OEmbed if success.

func (*OEmbed) MarshalJSON

func (j *OEmbed) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*OEmbed) MarshalJSONBuf

func (j *OEmbed) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*OEmbed) UnmarshalJSON

func (j *OEmbed) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*OEmbed) UnmarshalJSONFFLexer

func (j *OEmbed) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type Params

type Params struct {
	MaxWidth  int
	MaxHeight int
}

Params represent a optional parameters for Extract method.

type Photo

type Photo struct {
	// The source URL of the image. Consumers should be able to insert this URL into an <img> element.
	// Only HTTP and HTTPS URLs are valid.
	URL string `json:"url"` // required

	// The width in pixels of the image specified in the url parameter.
	Width int `json:"width"` // required

	// The height in pixels of the image specified in the url parameter.
	Height int `json:"height"` // required
}

Photo is used for representing static photos.

func (*Photo) MarshalJSON

func (j *Photo) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*Photo) MarshalJSONBuf

func (j *Photo) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*Photo) UnmarshalJSON

func (j *Photo) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*Photo) UnmarshalJSONFFLexer

func (j *Photo) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type Provider

type Provider struct {
	Name      string     `json:"provider_name"`
	URL       string     `json:"provider_url"`
	Endpoints []Endpoint `json:"endpoints"`
}

Provider represent a single provider info

func (*Provider) MarshalJSON

func (j *Provider) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*Provider) MarshalJSONBuf

func (j *Provider) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*Provider) UnmarshalJSON

func (j *Provider) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*Provider) UnmarshalJSONFFLexer

func (j *Provider) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type Rich

type Rich struct {
	// The HTML required to display the resource. The HTML should have no padding or margins.
	// Consumers may wish to load the HTML in an off-domain iframe to avoid XSS vulnerabilities.
	// The markup should be valid XHTML 1.0 Basic.
	HTML string `json:"html"` // required

	// The width in pixels required to display the HTML.
	Width int `json:"width"` // required

	// The height in pixels required to display the HTML.
	Height int `json:"height"` // required
}

Rich is used for rich HTML content that does not fall under one of the other categories.

func (*Rich) MarshalJSON

func (j *Rich) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*Rich) MarshalJSONBuf

func (j *Rich) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*Rich) UnmarshalJSON

func (j *Rich) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*Rich) UnmarshalJSONFFLexer

func (j *Rich) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

type Video

type Video struct {
	// The HTML required to embed a video player. The HTML should have no padding or margins.
	// Consumers may wish to load the HTML in an off-domain iframe to avoid XSS vulnerabilities.
	HTML string `json:"html"` // required

	// The width in pixels required to display the HTML.
	Width int `json:"width"` // required

	// The height in pixels required to display the HTML.
	Height int `json:"height"` // required
}

Video is used for representing playable videos.

func (*Video) MarshalJSON

func (j *Video) MarshalJSON() ([]byte, error)

MarshalJSON marshal bytes to json - template

func (*Video) MarshalJSONBuf

func (j *Video) MarshalJSONBuf(buf fflib.EncodingBuffer) error

MarshalJSONBuf marshal buff to json - template

func (*Video) UnmarshalJSON

func (j *Video) UnmarshalJSON(input []byte) error

UnmarshalJSON umarshall json - template of ffjson

func (*Video) UnmarshalJSONFFLexer

func (j *Video) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error

UnmarshalJSONFFLexer fast json unmarshall - template ffjson

Jump to

Keyboard shortcuts

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