Add UserAgent
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
e245573361
commit
3bd0ce72a5
29
default.go
29
default.go
|
@ -64,10 +64,21 @@ func (api *LinkInfoApi) DefaultLinkHandler(link string) (*LinkInfo, error) {
|
||||||
return nil, errors.New("url is a local ip address")
|
return nil, errors.New("url is a local ip address")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var req *http.Request
|
||||||
var res *http.Response
|
var res *http.Response
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
res, err = api.Client.Head(link)
|
req, err = http.NewRequest(http.MethodHead, link, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if api.UserAgent != "" {
|
||||||
|
req.Header.Set("User-Agent", api.UserAgent)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err = api.Client.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -140,6 +151,10 @@ func (api *LinkInfoApi) detectContentType(link, defaultType string) string {
|
||||||
return defaultType
|
return defaultType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if api.UserAgent != "" {
|
||||||
|
req.Header.Set("User-Agent", api.UserAgent)
|
||||||
|
}
|
||||||
|
|
||||||
req.Header.Set("Range", "bytes=0-512")
|
req.Header.Set("Range", "bytes=0-512")
|
||||||
|
|
||||||
res, err := api.Client.Do(req)
|
res, err := api.Client.Do(req)
|
||||||
|
@ -170,7 +185,17 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (api *LinkInfoApi) retrieveHtmlLinkTitle(i *LinkInfo, link string) error {
|
func (api *LinkInfoApi) retrieveHtmlLinkTitle(i *LinkInfo, link string) error {
|
||||||
res, err := api.Client.Get(link)
|
req, err := http.NewRequest(http.MethodGet, link, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if api.UserAgent != "" {
|
||||||
|
req.Header.Set("User-Agent", api.UserAgent)
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := api.Client.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
19
linkinfo.go
19
linkinfo.go
|
@ -13,6 +13,7 @@ type LinkHandler struct {
|
||||||
|
|
||||||
type LinkInfoApi struct {
|
type LinkInfoApi struct {
|
||||||
Client *http.Client
|
Client *http.Client
|
||||||
|
UserAgent string
|
||||||
|
|
||||||
Imgur *ImgurInfoApi
|
Imgur *ImgurInfoApi
|
||||||
Youtube *YoutubeInfoApi
|
Youtube *YoutubeInfoApi
|
||||||
|
@ -29,22 +30,30 @@ type LinkInfo struct {
|
||||||
Redirects []string `json:"redirects,omitempty"`
|
Redirects []string `json:"redirects,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HttpOptions struct {
|
||||||
|
Option
|
||||||
|
|
||||||
|
UserAgent string
|
||||||
|
}
|
||||||
|
|
||||||
func New(opts ...Option) *LinkInfoApi {
|
func New(opts ...Option) *LinkInfoApi {
|
||||||
api := &LinkInfoApi{
|
api := &LinkInfoApi{
|
||||||
Client: &http.Client{
|
Client: &http.Client{
|
||||||
Timeout: 60 * time.Second,
|
Timeout: 15 * time.Second,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
switch opt.(type) {
|
switch o := opt.(type) {
|
||||||
case *ImgurOptions:
|
case *ImgurOptions:
|
||||||
api.Imgur = &ImgurInfoApi{api, opt.(*ImgurOptions)}
|
api.Imgur = &ImgurInfoApi{api, o}
|
||||||
case *YoutubeOptions:
|
case *YoutubeOptions:
|
||||||
api.Youtube = &YoutubeInfoApi{api, opt.(*YoutubeOptions)}
|
api.Youtube = &YoutubeInfoApi{api, o}
|
||||||
case *TwitterOptions:
|
case *TwitterOptions:
|
||||||
api.Twitter = &TwitterInfoApi{opts: opt.(*TwitterOptions)}
|
api.Twitter = &TwitterInfoApi{opts: o}
|
||||||
api.Twitter.Init()
|
api.Twitter.Init()
|
||||||
|
case *HttpOptions:
|
||||||
|
api.UserAgent = o.UserAgent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -60,6 +60,10 @@ func main() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opts = append(opts, &linkinfo.HttpOptions{
|
||||||
|
UserAgent: "LinkInfo/1.0",
|
||||||
|
})
|
||||||
|
|
||||||
api = linkinfo.New(opts...)
|
api = linkinfo.New(opts...)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
Loading…
Reference in New Issue