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")
|
||||
}
|
||||
|
||||
var req *http.Request
|
||||
var res *http.Response
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
|
@ -140,6 +151,10 @@ func (api *LinkInfoApi) detectContentType(link, defaultType string) string {
|
|||
return defaultType
|
||||
}
|
||||
|
||||
if api.UserAgent != "" {
|
||||
req.Header.Set("User-Agent", api.UserAgent)
|
||||
}
|
||||
|
||||
req.Header.Set("Range", "bytes=0-512")
|
||||
|
||||
res, err := api.Client.Do(req)
|
||||
|
@ -170,7 +185,17 @@ var (
|
|||
)
|
||||
|
||||
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 {
|
||||
return err
|
||||
|
|
21
linkinfo.go
21
linkinfo.go
|
@ -12,7 +12,8 @@ type LinkHandler struct {
|
|||
}
|
||||
|
||||
type LinkInfoApi struct {
|
||||
Client *http.Client
|
||||
Client *http.Client
|
||||
UserAgent string
|
||||
|
||||
Imgur *ImgurInfoApi
|
||||
Youtube *YoutubeInfoApi
|
||||
|
@ -29,22 +30,30 @@ type LinkInfo struct {
|
|||
Redirects []string `json:"redirects,omitempty"`
|
||||
}
|
||||
|
||||
type HttpOptions struct {
|
||||
Option
|
||||
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
func New(opts ...Option) *LinkInfoApi {
|
||||
api := &LinkInfoApi{
|
||||
Client: &http.Client{
|
||||
Timeout: 60 * time.Second,
|
||||
Timeout: 15 * time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
switch opt.(type) {
|
||||
switch o := opt.(type) {
|
||||
case *ImgurOptions:
|
||||
api.Imgur = &ImgurInfoApi{api, opt.(*ImgurOptions)}
|
||||
api.Imgur = &ImgurInfoApi{api, o}
|
||||
case *YoutubeOptions:
|
||||
api.Youtube = &YoutubeInfoApi{api, opt.(*YoutubeOptions)}
|
||||
api.Youtube = &YoutubeInfoApi{api, o}
|
||||
case *TwitterOptions:
|
||||
api.Twitter = &TwitterInfoApi{opts: opt.(*TwitterOptions)}
|
||||
api.Twitter = &TwitterInfoApi{opts: o}
|
||||
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...)
|
||||
|
||||
var err error
|
||||
|
|
Loading…
Reference in New Issue