66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package nuapi
|
|
|
|
import (
|
|
"compress/gzip"
|
|
"context"
|
|
"fmt"
|
|
"os/exec"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
)
|
|
|
|
func (api *Api) GetWithCookie(ctx context.Context, url string) (*goquery.Document, error) {
|
|
if api.Cookie == "" {
|
|
return nil, ErrNoCookieSet.New()
|
|
}
|
|
|
|
curl := exec.Command("curl",
|
|
"-s", url,
|
|
"-H", fmt.Sprintf("User-Agent: %s", api.UserAgent),
|
|
"-H", fmt.Sprintf("Cookie: %s", api.Cookie),
|
|
"-H", fmt.Sprintf("Accept-Encoding: %s", "gzip"),
|
|
)
|
|
|
|
stdout, err := curl.StdoutPipe()
|
|
if err != nil {
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
|
}
|
|
|
|
if err := curl.Start(); err != nil {
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
|
}
|
|
defer curl.Wait()
|
|
|
|
r, err := gzip.NewReader(stdout)
|
|
if err != nil {
|
|
return nil, ErrInvalidGzipData.New(err)
|
|
}
|
|
|
|
return goquery.NewDocumentFromReader(r)
|
|
}
|
|
|
|
func (api *Api) Get(url string) (*goquery.Document, error) {
|
|
curl := exec.Command("curl",
|
|
"-s", url,
|
|
"-H", fmt.Sprintf("User-Agent: %s", api.UserAgent),
|
|
"-H", fmt.Sprintf("Accept-Encoding: %s", "gzip"),
|
|
)
|
|
|
|
stdout, err := curl.StdoutPipe()
|
|
if err != nil {
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
|
}
|
|
|
|
if err := curl.Start(); err != nil {
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
|
}
|
|
defer curl.Wait()
|
|
|
|
r, err := gzip.NewReader(stdout)
|
|
if err != nil {
|
|
return nil, ErrInvalidGzipData.Wrap(err)
|
|
}
|
|
|
|
return goquery.NewDocumentFromReader(r)
|
|
}
|