102 lines
2.0 KiB
Go
102 lines
2.0 KiB
Go
package linkinfo
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
type LinkHandler struct {
|
|
Hosts []string
|
|
Handler func(link string) (*LinkInfo, error)
|
|
}
|
|
|
|
type LinkInfoApi struct {
|
|
Client *http.Client
|
|
|
|
Imgur *ImgurInfoApi
|
|
Youtube *YoutubeInfoApi
|
|
Twitter *TwitterInfoApi
|
|
linkHandlers []*LinkHandler
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
func New(opts ...Option) *LinkInfoApi {
|
|
api := &LinkInfoApi{
|
|
Client: &http.Client{
|
|
Timeout: 60 * time.Second,
|
|
},
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
switch opt.(type) {
|
|
case *ImgurOptions:
|
|
api.Imgur = &ImgurInfoApi{api, opt.(*ImgurOptions)}
|
|
case *YoutubeOptions:
|
|
api.Youtube = &YoutubeInfoApi{api, opt.(*YoutubeOptions)}
|
|
case *TwitterOptions:
|
|
api.Twitter = &TwitterInfoApi{opts: opt.(*TwitterOptions)}
|
|
api.Twitter.Init()
|
|
}
|
|
}
|
|
|
|
api.registerDefaultHandlers()
|
|
|
|
return api
|
|
}
|
|
|
|
func (i *LinkInfoApi) registerDefaultHandlers() {
|
|
i.linkHandlers = make([]*LinkHandler, 0)
|
|
|
|
if i.Imgur != nil {
|
|
i.linkHandlers = append(i.linkHandlers, &LinkHandler{
|
|
Hosts: imgurHosts,
|
|
Handler: i.Imgur.Handler,
|
|
})
|
|
}
|
|
|
|
if i.Youtube != nil {
|
|
i.linkHandlers = append(i.linkHandlers, &LinkHandler{
|
|
Hosts: youtubeHosts,
|
|
Handler: i.Youtube.Handler,
|
|
})
|
|
}
|
|
|
|
if i.Twitter != nil {
|
|
i.linkHandlers = append(i.linkHandlers, &LinkHandler{
|
|
Hosts: twitterHosts,
|
|
Handler: i.Twitter.Handler,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (i *LinkInfoApi) AddHandler(handler *LinkHandler) {
|
|
i.linkHandlers = append(i.linkHandlers, handler)
|
|
}
|
|
|
|
func (i *LinkInfoApi) Retrieve(link string) (*LinkInfo, error) {
|
|
u, err := url.Parse(link)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, handler := range i.linkHandlers {
|
|
for _, host := range handler.Hosts {
|
|
if host == u.Hostname() {
|
|
return handler.Handler(link)
|
|
}
|
|
}
|
|
}
|
|
|
|
return i.DefaultLinkHandler(link)
|
|
}
|