Documentation
¶
Overview ¶
Package googp is a OGP (Open Graph protocol) parser library for Golang.
This library is fully compliant with the reference, highly customizable, and supports type conversion.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportedPage is an unsupported page errror. ErrUnsupportedPage = 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.
Example ¶
var ogp OGP
if err := Fetch(endpoint()+"/5.html", &ogp); err != nil {
return
}
fmt.Printf("og:title = \"%s\"\n", ogp.Title)
fmt.Printf("og:type = \"%s\"\n", ogp.Type)
fmt.Printf("og:url = \"%s\"\n", ogp.URL)
Output: og:title = "Open Graph protocol" og:type = "website" og:url = "https://ogp.me/"
Example (CustomizeModel) ¶
// URL is embedded url.URL and is added TextUnmarshaler implementation.
var _ encoding.TextUnmarshaler = &URL{}
type MyOGP struct {
Title string `googp:"og:title"`
URL URL `googp:"og:url"`
ImageURL *URL `googp:"og:image"`
AppID int `googp:"fb:app_id"`
}
var ogp MyOGP
if err := Fetch(endpoint()+"/5.html", &ogp); err != nil {
return
}
fmt.Printf("og:title = \"%s\"\n", ogp.Title)
fmt.Printf("og:url = \"%s\"\n", ogp.URL.String())
fmt.Printf("og:image = \"%s\"\n", ogp.ImageURL.String())
fmt.Printf("fb:app_id = %d\n", ogp.AppID)
Output: og:title = "Open Graph protocol" og:url = "https://ogp.me/" og:image = "https://ogp.me/logo.png" fb:app_id = 115190258555800
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"`
}
Audio is a model that structure contents of og:audio.
type BadStatusCodeError ¶ added in v0.4.0
type BadStatusCodeError struct {
StatusCode int
}
BadStatusCodeError is an error returned when the status code is not 200 in Fetch.
func (BadStatusCodeError) Error ¶ added in v0.4.0
func (err BadStatusCodeError) Error() string
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"`
}
Image is a model that structure contents of og:image.
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"`
}
OGP is a model that have Basic Metadata and Optional Metadata defined in the reference. ref: https://ogp.me/
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser is an OGP parser.
func (*Parser) Parse ¶
Parse OGPs from the HTML.
Example ¶
reader := strings.NewReader(`
<html>
<head>
<meta property="og:title" content="title" />
<meta property="og:type" content="website" />
<meta property="og:url" content="http://example.com" />
<meta property="og:image" content="http://example.com/image.png" />
</head>
<body>
</body>
</html>
`)
var ogp OGP
if err := NewParser().Parse(reader, &ogp); err != nil {
return
}
fmt.Printf("og:title = \"%s\"\n", ogp.Title)
fmt.Printf("og:type = \"%s\"\n", ogp.Type)
fmt.Printf("og:url = \"%s\"\n", ogp.URL)
fmt.Printf("og:image = \"%s\"\n", ogp.Images[0].URL)
Output: og:title = "title" og:type = "website" og:url = "http://example.com" og:image = "http://example.com/image.png"
type ParserOpts ¶
type ParserOpts struct {
// You can add processing when you need to regard another Nodes 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 an 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"`
}
Video is a model that structure contents of og:video.