fixed curl calls

This commit is contained in:
milarin 2023-03-07 18:40:20 +01:00
parent 7129cecdf8
commit 799d8d5dee

View File

@ -1,12 +1,12 @@
package nuapi package nuapi
import ( import (
"bytes"
"compress/gzip" "compress/gzip"
"context" "context"
"fmt" "fmt"
"os/exec" "os/exec"
"git.milar.in/milarin/adverr"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
) )
@ -22,12 +22,29 @@ func (api *Api) GetWithCookie(ctx context.Context, url string) (*goquery.Documen
"-H", fmt.Sprintf("Accept-Encoding: %s", "gzip"), "-H", fmt.Sprintf("Accept-Encoding: %s", "gzip"),
) )
data, err := curl.Output() stdout, err := curl.StdoutPipe()
if err != nil { if err != nil {
return nil, ErrCurlRequestFailed.Wrap(err, url) return nil, ErrCurlRequestFailed.Wrap(err, url)
} }
defer stdout.Close()
r, err := gzip.NewReader(bytes.NewReader(data)) 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)
if err != nil { if err != nil {
return nil, ErrInvalidGzipData.New(err) return nil, ErrInvalidGzipData.New(err)
} }
@ -42,12 +59,29 @@ func (api *Api) Get(ctx context.Context, url string) (*goquery.Document, error)
"-H", fmt.Sprintf("Accept-Encoding: %s", "gzip"), "-H", fmt.Sprintf("Accept-Encoding: %s", "gzip"),
) )
data, err := curl.Output() stdout, err := curl.StdoutPipe()
if err != nil { if err != nil {
return nil, ErrCurlRequestFailed.Wrap(err, url) return nil, ErrCurlRequestFailed.Wrap(err, url)
} }
defer stdout.Close()
r, err := gzip.NewReader(bytes.NewReader(data)) 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)
if err != nil { if err != nil {
return nil, ErrInvalidGzipData.New(err) return nil, ErrInvalidGzipData.New(err)
} }