47 lines
901 B
Go
47 lines
901 B
Go
|
package linkinfo
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
)
|
||
|
|
||
|
type linkHandler struct {
|
||
|
hosts []string
|
||
|
handler func(link string) (*LinkInfo, error)
|
||
|
}
|
||
|
|
||
|
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 (
|
||
|
Client = http.DefaultClient
|
||
|
|
||
|
linkHandlers = []*linkHandler{
|
||
|
{hosts: []string{"youtube.com", "youtu.be"}, handler: youtubeLinkHandler},
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func 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)
|
||
|
}
|