oembed

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2023 License: MIT Imports: 11 Imported by: 2

README

Package oembed implements functions to discover oEmbed providers for given URL and parse providers' response.

https://godoc.org/github.com/artyom/oembed

Documentation

Overview

Package oembed implements functions to discover oEmbed providers for given URL and parse providers' response.

Index

Constants

View Source
const (
	TypePhoto Type = "photo"
	TypeVideo      = "video"
	TypeLink       = "link"
	TypeRich       = "rich"
)

Predefined oEmbed types (http://oembed.com 2.3.4.1-2.3.4.4):

Variables

This section is empty.

Functions

func Discover

func Discover(htmlBodyReader io.Reader) (endpointURL string, found bool, err error)

Discover parses htmlBodyReader as utf-8 encoded html text and tries to discover oEmbed endpoint as described in http://oembed.com/#section4

Use NewReader from golang.org/x/net/html/charset to ensure data is properly encoded.

Types

type LookupFunc

type LookupFunc func(url string) (endpointURL string, found bool)

LookupFunc checks whether provided url matches any known oEmbed provider and returns url of oEmbed endpoint that should return metadata about original url.

func Providers

func Providers(r io.Reader) (LookupFunc, error)

Providers decodes r as content of http://oembed.com/providers.json and returns LookupFunc matching url against providers list.

type Metadata

type Metadata struct {
	Type            Type   // resource type
	Provider        string // provider name
	Title           string // resource title
	AuthorName      string // name of the author/owner
	AuthorURL       string // URL for the author/owner of the resource
	Thumbnail       string // URL of the thumbnail image
	ThumbnailWidth  int    // width of the optional thumbnail
	ThumbnailHeight int    // height of the optional thumbnail

	// HTML snippet required to display the embedded resource — as returned
	// by provider, may be unsafe. Set for video and rich types.
	HTML          string
	URL           string // source URL of the original image, set for photo types
	Width, Height int    // width/height in pixels of the image/snippet specified by the URL/HTML
}

Metadata describes resource as returned by oEmbed provider (http://oembed.com, section 2.3.4)

func FromJSON

func FromJSON(r io.Reader) (*Metadata, error)

FromJSON decodes json-encoded oEmbed metadata read from r. It ensures that result contains only valid http/https urls and metadata satisfies one of the predefined types.

func FromResponse

func FromResponse(r *http.Response) (*Metadata, error)

FromResponse verifies status code of r, and calls FromJSON or FromXML depending on Content-Type of r. Only responses with status code 200 and Content-Type of either application/json or text/xml are allowed, otherwise error is returned.

func FromXML

func FromXML(r io.Reader) (*Metadata, error)

FromXML decodes xml-encoded oEmbed metadata read from r. It ensures that result contains only valid http/https urls and metadata satisfies one of the predefined types.

type Type

type Type string

Type describes resource type as returned by oEmbed provider

Jump to

Keyboard shortcuts

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