Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/keighl/metabolize
Decodes HTML meta tags into a Golang struct.
https://github.com/keighl/metabolize
Last synced: 30 days ago
JSON representation
Decodes HTML meta tags into a Golang struct.
- Host: GitHub
- URL: https://github.com/keighl/metabolize
- Owner: keighl
- License: other
- Created: 2015-09-15T20:45:28.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-09-15T21:04:14.000Z (about 9 years ago)
- Last Synced: 2024-08-03T17:21:00.909Z (4 months ago)
- Language: Go
- Size: 125 KB
- Stars: 30
- Watchers: 4
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome - Metabolize - Decodes HTML meta tags into a golang structure (Open source library / Not Categorized)
README
# Metabolize
[![Build Status](https://travis-ci.org/keighl/metabolize.png?branch=master)](https://travis-ci.org/keighl/metabolize) [![Coverage Status](https://coveralls.io/repos/keighl/metabolize/badge.svg)](https://coveralls.io/r/keighl/metabolize)
Decodes HTML values into a Golang struct. Great for quickly grabbing [open graph](http://ogp.me/) 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.
```go
type MetaData struct {
Title string `meta:"og:title"`
// If no `og:description`, will fall back to `description`
Description string `meta:"og:description,description"`
}
```Example
```go
package mainimport (
"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`