nu-api/novelupdates.go

58 lines
1.1 KiB
Go
Raw Normal View History

2023-03-03 14:37:16 +01:00
package nuapi
import (
"bytes"
"compress/gzip"
"context"
"errors"
"fmt"
"os/exec"
"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 == "" {
return nil, errors.New("no API cookie set")
}
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, err
}
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
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"),
)
data, err := curl.Output()
if err != nil {
return nil, err
}
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
return goquery.NewDocumentFromReader(r)
}