From 799d8d5deefea38d93ec88955243d0139bbb739c Mon Sep 17 00:00:00 2001 From: milarin Date: Tue, 7 Mar 2023 18:40:20 +0100 Subject: [PATCH] fixed curl calls --- novelupdates.go => curl.go | 44 +++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) rename novelupdates.go => curl.go (54%) diff --git a/novelupdates.go b/curl.go similarity index 54% rename from novelupdates.go rename to curl.go index 4641094..f3aa428 100644 --- a/novelupdates.go +++ b/curl.go @@ -1,12 +1,12 @@ package nuapi import ( - "bytes" "compress/gzip" "context" "fmt" "os/exec" + "git.milar.in/milarin/adverr" "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"), ) - data, err := curl.Output() + stdout, err := curl.StdoutPipe() if err != nil { 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 { 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"), ) - data, err := curl.Output() + stdout, err := curl.StdoutPipe() if err != nil { 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 { return nil, ErrInvalidGzipData.New(err) }