Skip to content

Commit

Permalink
Share remote zstd decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Aug 10, 2024
1 parent f9bc02a commit 7f84f0c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions handlers/scraper/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
transport http.RoundTripper
transportNoProxy *http.Transport
sflightScraper singleflight.Group
remoteZSTDReader *zstd.Decoder
)

//go:embed dictionary.bin
Expand All @@ -51,9 +52,15 @@ type InstaData struct {
}

func init() {
var err error
transport = gzhttp.Transport(http.DefaultTransport, gzhttp.TransportAlwaysDecompress(true))
transportNoProxy = http.DefaultTransport.(*http.Transport).Clone()
transportNoProxy.Proxy = nil // Skip any proxy

remoteZSTDReader, err = zstd.NewReader(nil, zstd.WithDecoderLowmem(true), zstd.WithDecoderDicts(zstdDict))
if err != nil {
panic(err)
}
}

func GetData(postID string) (*InstaData, error) {
Expand Down Expand Up @@ -152,13 +159,12 @@ func (i *InstaData) ScrapeData() error {
res, err := remoteClient.Do(req)
if res != nil && res.StatusCode == 200 {
defer res.Body.Close()
zstdReader, err := zstd.NewReader(nil, zstd.WithDecoderLowmem(true), zstd.WithDecoderDicts(zstdDict))
if err != nil {
return err
}
remoteData, err := io.ReadAll(res.Body)
if err == nil {
remoteDecomp, err := zstdReader.DecodeAll(remoteData, nil)
remoteDecomp, err := remoteZSTDReader.DecodeAll(remoteData, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit 7f84f0c

Please sign in to comment.