Document extractor service (#15665)
* Document extractor service * Fixing vendor modules * Addressing PR Review comments * Some small simplifications * Fixing a linter complain * simplifying a bit the code using package variables Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
04ef5c682e
Коммит
8d5be2d657
47
vendor/github.com/advancedlogic/GoOse/html.go
сгенерированный
поставляемый
Обычный файл
47
vendor/github.com/advancedlogic/GoOse/html.go
сгенерированный
поставляемый
Обычный файл
@@ -0,0 +1,47 @@
|
||||
package goose
|
||||
|
||||
import (
|
||||
resty "github.com/go-resty/resty/v2"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type HtmlRequester interface {
|
||||
fetchHTML(string) (string, error)
|
||||
}
|
||||
|
||||
// Crawler can fetch the target HTML page
|
||||
type htmlrequester struct {
|
||||
config Configuration
|
||||
}
|
||||
|
||||
// NewCrawler returns a crawler object initialised with the URL and the [optional] raw HTML body
|
||||
func NewHtmlRequester(config Configuration) HtmlRequester {
|
||||
return htmlrequester{
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
func (hr htmlrequester) fetchHTML(url string) (string, error) {
|
||||
client := resty.New()
|
||||
client.SetTimeout(hr.config.timeout)
|
||||
resp, err := client.R().
|
||||
SetHeader("Content-Type", "text/html").
|
||||
SetHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30").
|
||||
Get(url)
|
||||
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "could not perform request on "+url)
|
||||
}
|
||||
if resp.IsError() {
|
||||
return "", &badRequest{Message: "could not perform request with " + url + " status code " + string(resp.StatusCode())}
|
||||
}
|
||||
return resp.String(), nil
|
||||
}
|
||||
|
||||
type badRequest struct {
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func (BadRequest *badRequest) Error() string {
|
||||
return "Required request fields are not filled"
|
||||
}
|
||||
Ссылка в новой задаче
Block a user