2023-03-03 14:37:16 +01:00
|
|
|
package nuapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
|
2023-03-07 18:40:20 +01:00
|
|
|
"git.milar.in/milarin/adverr"
|
2023-03-03 14:37:16 +01:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2023-03-03 15:10:00 +01:00
|
|
|
func (api *Api) GetWithCookie(ctx context.Context, url string) (*goquery.Document, error) {
|
2023-03-03 14:37:16 +01:00
|
|
|
if api.Cookie == "" {
|
2023-03-06 11:21:19 +01:00
|
|
|
return nil, ErrNoCookieSet.New()
|
2023-03-03 14:37:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
curl := exec.CommandContext(ctx, "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"),
|
|
|
|
)
|
|
|
|
|
2023-03-07 18:40:20 +01:00
|
|
|
stdout, err := curl.StdoutPipe()
|
2023-03-03 14:37:16 +01:00
|
|
|
if err != nil {
|
2023-03-07 18:28:45 +01:00
|
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
2023-03-03 14:37:16 +01:00
|
|
|
}
|
2023-03-07 18:40:20 +01:00
|
|
|
defer stdout.Close()
|
2023-03-03 14:37:16 +01:00
|
|
|
|
2023-03-07 18:40:20 +01:00
|
|
|
stderr, err := curl.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
|
|
|
}
|
|
|
|
defer stderr.Close()
|
|
|
|
|
|
|
|
if err := curl.Start(); err != nil {
|
|
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := curl.Wait(); err != nil {
|
|
|
|
adverr.Println(ErrCurlRequestFailed.Wrap(err, url))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
r, err := gzip.NewReader(stdout)
|
2023-03-03 14:37:16 +01:00
|
|
|
if err != nil {
|
2023-03-06 11:21:19 +01:00
|
|
|
return nil, ErrInvalidGzipData.New(err)
|
2023-03-03 14:37:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return goquery.NewDocumentFromReader(r)
|
|
|
|
}
|
|
|
|
|
2023-03-03 15:10:00 +01:00
|
|
|
func (api *Api) Get(ctx context.Context, url string) (*goquery.Document, error) {
|
2023-03-03 14:37:16 +01:00
|
|
|
curl := exec.CommandContext(ctx, "curl",
|
|
|
|
"-s", url,
|
|
|
|
"-H", fmt.Sprintf("User-Agent: %s", api.UserAgent),
|
|
|
|
"-H", fmt.Sprintf("Accept-Encoding: %s", "gzip"),
|
|
|
|
)
|
|
|
|
|
2023-03-07 18:40:20 +01:00
|
|
|
stdout, err := curl.StdoutPipe()
|
2023-03-03 14:37:16 +01:00
|
|
|
if err != nil {
|
2023-03-07 18:28:45 +01:00
|
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
2023-03-03 14:37:16 +01:00
|
|
|
}
|
2023-03-07 18:40:20 +01:00
|
|
|
defer stdout.Close()
|
|
|
|
|
|
|
|
stderr, err := curl.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
|
|
|
}
|
|
|
|
defer stderr.Close()
|
|
|
|
|
|
|
|
if err := curl.Start(); err != nil {
|
|
|
|
return nil, ErrCurlRequestFailed.Wrap(err, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := curl.Wait(); err != nil {
|
|
|
|
adverr.Println(ErrCurlRequestFailed.Wrap(err, url))
|
|
|
|
}
|
|
|
|
}()
|
2023-03-03 14:37:16 +01:00
|
|
|
|
2023-03-07 18:40:20 +01:00
|
|
|
r, err := gzip.NewReader(stdout)
|
2023-03-03 14:37:16 +01:00
|
|
|
if err != nil {
|
2023-03-06 11:21:19 +01:00
|
|
|
return nil, ErrInvalidGzipData.New(err)
|
2023-03-03 14:37:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return goquery.NewDocumentFromReader(r)
|
|
|
|
}
|