status-go/vendor/github.com/keighl/metabolize
Frank Tang 922e785512 Add GitHub to whitelist for URL unfurling #11426 2021-01-11 11:15:09 +01:00
..
.editorconfig Add GitHub to whitelist for URL unfurling #11426 2021-01-11 11:15:09 +01:00
.gitignore Add GitHub to whitelist for URL unfurling #11426 2021-01-11 11:15:09 +01:00
.travis.yml Add GitHub to whitelist for URL unfurling #11426 2021-01-11 11:15:09 +01:00
CHANGELOG.md Add GitHub to whitelist for URL unfurling #11426 2021-01-11 11:15:09 +01:00
LICENSE Add GitHub to whitelist for URL unfurling #11426 2021-01-11 11:15:09 +01:00
README.md Add GitHub to whitelist for URL unfurling #11426 2021-01-11 11:15:09 +01:00
metabolize.go Add GitHub to whitelist for URL unfurling #11426 2021-01-11 11:15:09 +01:00

README.md

Metabolize

Build Status Coverage Status

Decodes HTML values into a Golang struct. Great for quickly grabbing open graph data.

Installation

go get -u github.com/keighl/metabolize

Usage

Use meta:"xxx" tags on your struct to tell metabolize how to decode metadata from an HTML document.

type MetaData struct {
    Title string `meta:"og:title"`
    // If no `og:description`, will fall back to `description`
    Description string `meta:"og:description,description"`
}

Example

package main

import (
    "fmt"
    m "github.com/keighl/metabolize"
    "net/http"
    "net/url"
)

type MetaData struct {
    Title       string  `meta:"og:title"`
    Description string  `meta:"og:description,description"`
    Type        string  `meta:"og:type"`
    URL         url.URL `meta:"og:url"`
    VideoWidth  int64   `meta:"og:video:width"`
    VideoHeight int64   `meta:"og:video:height"`
}

func main() {
    res, _ := http.Get("https://www.youtube.com/watch?v=FzRH3iTQPrk")

    data := new(MetaData)

    err := m.Metabolize(res.Body, data)
    if err != nil {
        panic(err)
    }

    fmt.Printf("Title: %s\n", data.Title)
    fmt.Printf("Description: %s\n", data.Description)
    fmt.Printf("Type: %s\n", data.Type)
    fmt.Printf("URL: %s\n", data.URL.String())
    fmt.Printf("VideoWidth: %d\n", data.VideoWidth)
    fmt.Printf("VideoHeight: %d\n", data.VideoHeight)
}

Outputs:

Title: The Sneezing Baby Panda
Description: A Baby Panda Sneezing Original footage taken and being used with kind permission of LJM Productions Pty. Ltd.,/Wild Candy Pty. Ltd. Authentic t-shirts http:/...
Type: video
URL: http://www.youtube.com/watch?v=FzRH3iTQPrk
VideoWidth: 480
VideoHeight: 360

Supported types

  • string
  • bool
  • float64
  • int64
  • time.Time
  • url.URL