nu-api/novelupdates.go
2023-03-06 11:21:19 +01:00

57 lines
1.2 KiB
Go

package nuapi
import (
"bytes"
"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.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"),
)
data, err := curl.Output()
if err != nil {
return nil, ErrCurlRequestFailed.New(err, url)
}
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, ErrInvalidGzipData.New(err)
}
return goquery.NewDocumentFromReader(r)
}
func (api *Api) Get(ctx context.Context, url string) (*goquery.Document, error) {
curl := exec.CommandContext(ctx, "curl",
"-s", url,
"-H", fmt.Sprintf("User-Agent: %s", api.UserAgent),
"-H", fmt.Sprintf("Accept-Encoding: %s", "gzip"),
)
data, err := curl.Output()
if err != nil {
return nil, ErrCurlRequestFailed.New(err, url)
}
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, ErrInvalidGzipData.New(err)
}
return goquery.NewDocumentFromReader(r)
}