Documentation
¶
Overview ¶
Package opengraph implements and parses "The Open Graph Protocol" of web pages. See http://ogp.me/ for more information.
Index ¶
- Constants
- type Audio
- type Favicon
- type Image
- type Intent
- type Link
- type Meta
- func (meta *Meta) Contribute(og *OpenGraph) (err error)
- func (meta *Meta) IsAudio() bool
- func (meta *Meta) IsDescription() bool
- func (meta *Meta) IsImage() bool
- func (meta *Meta) IsOGDescription() bool
- func (meta *Meta) IsPropertyOf(name string) bool
- func (meta *Meta) IsSiteName() bool
- func (meta *Meta) IsTitle() bool
- func (meta *Meta) IsType() bool
- func (meta *Meta) IsURL() bool
- func (meta *Meta) IsVideo() bool
- type OpenGraph
- type Title
- type Video
Examples ¶
Constants ¶
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
// Headers to be sent with the HTTP request.
// If nil, default browser-like headers will be used.
Headers map[string]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 ¶
Link represents any "<link ...>" HTML tag. <link> will NOT be used when Intent.String == true.
func (*Link) Contribute ¶
Contribute contributes OpenGraph
type Meta ¶
Meta represents any "<meta ...>" HTML tag.
func (*Meta) IsDescription ¶
IsDescription returns if it can be "description" of OGP. CAUTION: This property SHOULD NOT be used when Intent.Strict == true.
func (*Meta) IsOGDescription ¶
IsOGDescription returns if it can be "description" of OGP
func (*Meta) IsPropertyOf ¶
IsPropertyOf returns if it can be a property of specified struct
func (*Meta) IsSiteName ¶
IsSiteName returns if it can be "og:site_name"
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 ¶
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 (*OpenGraph) Parse ¶
Parse parses http.Response.Body and construct OpenGraph informations. Caller should close body after it gets parsed.
type Title ¶
type Title struct {
Text string
}
Title represents any "<title ...>" HTML tag. <title> will NOT be used when Intent.Strict == true.
func (*Title) Contribute ¶
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