2019-10-03 23:59:20 +00:00
|
|
|
package linkinfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-10-04 01:32:00 +00:00
|
|
|
"time"
|
2019-10-03 23:59:20 +00:00
|
|
|
)
|
|
|
|
|
2019-10-04 01:25:57 +00:00
|
|
|
type LinkHandler struct {
|
|
|
|
Hosts []string
|
|
|
|
Handler func(link string) (*LinkInfo, error)
|
2019-10-03 23:59:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 00:50:58 +00:00
|
|
|
type LinkInfoApi struct {
|
2019-10-04 03:31:24 +00:00
|
|
|
Client *http.Client
|
|
|
|
|
|
|
|
Imgur *ImgurInfoApi
|
2019-10-04 04:08:03 +00:00
|
|
|
Youtube *YoutubeInfoApi
|
2019-10-06 04:52:26 +00:00
|
|
|
Twitter *TwitterInfoApi
|
2019-10-04 01:25:57 +00:00
|
|
|
linkHandlers []*LinkHandler
|
2019-10-04 00:50:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 23:59:20 +00:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2019-10-04 03:31:24 +00:00
|
|
|
func New(opts ...Option) *LinkInfoApi {
|
2019-10-04 01:32:00 +00:00
|
|
|
api := &LinkInfoApi{
|
|
|
|
Client: &http.Client{
|
|
|
|
Timeout: 60 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
2019-10-04 01:25:57 +00:00
|
|
|
|
2019-10-04 03:31:24 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.(type) {
|
|
|
|
case *ImgurOptions:
|
|
|
|
api.Imgur = &ImgurInfoApi{api, opt.(*ImgurOptions)}
|
2019-10-04 04:08:03 +00:00
|
|
|
case *YoutubeOptions:
|
|
|
|
api.Youtube = &YoutubeInfoApi{api, opt.(*YoutubeOptions)}
|
2019-10-06 04:52:26 +00:00
|
|
|
case *TwitterOptions:
|
|
|
|
api.Twitter = &TwitterInfoApi{opts: opt.(*TwitterOptions)}
|
|
|
|
api.Twitter.Init()
|
2019-10-04 03:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 01:25:57 +00:00
|
|
|
api.registerDefaultHandlers()
|
|
|
|
|
|
|
|
return api
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *LinkInfoApi) registerDefaultHandlers() {
|
2019-10-04 03:31:24 +00:00
|
|
|
i.linkHandlers = make([]*LinkHandler, 0)
|
|
|
|
|
|
|
|
if i.Imgur != nil {
|
|
|
|
i.linkHandlers = append(i.linkHandlers, &LinkHandler{
|
|
|
|
Hosts: imgurHosts,
|
|
|
|
Handler: i.Imgur.Handler,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-04 04:08:03 +00:00
|
|
|
if i.Youtube != nil {
|
|
|
|
i.linkHandlers = append(i.linkHandlers, &LinkHandler{
|
|
|
|
Hosts: youtubeHosts,
|
|
|
|
Handler: i.Youtube.Handler,
|
|
|
|
})
|
2019-10-04 01:25:57 +00:00
|
|
|
}
|
2019-10-06 04:52:26 +00:00
|
|
|
|
|
|
|
if i.Twitter != nil {
|
|
|
|
i.linkHandlers = append(i.linkHandlers, &LinkHandler{
|
|
|
|
Hosts: twitterHosts,
|
|
|
|
Handler: i.Twitter.Handler,
|
|
|
|
})
|
|
|
|
}
|
2019-10-04 00:50:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 03:31:24 +00:00
|
|
|
func (i *LinkInfoApi) AddHandler(handler *LinkHandler) {
|
|
|
|
i.linkHandlers = append(i.linkHandlers, handler)
|
|
|
|
}
|
|
|
|
|
2019-10-04 00:50:58 +00:00
|
|
|
func (i *LinkInfoApi) Retrieve(link string) (*LinkInfo, error) {
|
2019-10-03 23:59:20 +00:00
|
|
|
u, err := url.Parse(link)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-04 01:25:57 +00:00
|
|
|
for _, handler := range i.linkHandlers {
|
|
|
|
for _, host := range handler.Hosts {
|
2019-10-03 23:59:20 +00:00
|
|
|
if host == u.Hostname() {
|
2019-10-04 01:25:57 +00:00
|
|
|
return handler.Handler(link)
|
2019-10-03 23:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 01:25:57 +00:00
|
|
|
return i.DefaultLinkHandler(link)
|
2019-10-03 23:59:20 +00:00
|
|
|
}
|