linkinfo/linkinfo.go

54 lines
1007 B
Go

package linkinfo
import (
"net/http"
"net/url"
)
type linkHandler struct {
hosts []string
handler func(link string) (*LinkInfo, error)
}
type LinkInfoApi struct {
client *http.Client
ImgurId string
}
type LinkInfo struct {
Title string `json:"title"`
Description string `json:"description"`
ContentType string `json:"type"`
ContentLength int64 `json:"contentLength"`
Duration string `json:"duration,omitempty"`
Redirects []string `json:"redirects,omitempty"`
}
var (
linkHandlers = []*linkHandler{
{hosts: []string{"youtube.com", "youtu.be"}, handler: youtubeLinkHandler},
}
)
func New() *LinkInfoApi {
return &LinkInfoApi{}
}
func (i *LinkInfoApi) Retrieve(link string) (*LinkInfo, error) {
u, err := url.Parse(link)
if err != nil {
return nil, err
}
for _, handler := range linkHandlers {
for _, host := range handler.hosts {
if host == u.Hostname() {
return handler.handler(link)
}
}
}
return defaultLinkHandler(link)
}