Skip to content

Commit

Permalink
go ta go fas
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Jul 23, 2024
1 parent 90e478d commit 219c88c
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions handlers/scraper/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var (
timeout = 10 * time.Second
timeout = 5 * time.Second
ErrNotFound = errors.New("post not found")
transport = gzhttp.Transport(http.DefaultTransport, gzhttp.TransportAlwaysDecompress(true))
transportNoProxy = &http.Transport{
Expand Down Expand Up @@ -155,17 +155,12 @@ func (i *InstaData) ScrapeData() error {
}

var body []byte
for retries := 0; retries < 3; retries++ {
res, err := client.Do(req)
if res != nil && res.StatusCode == 200 {
defer res.Body.Close()
body, err = io.ReadAll(res.Body)
if err == nil && len(body) > 0 {
break
}
} else if err != nil {
slog.Error("Failed to scrape data from embedHTML", "postID", i.PostID, "err", err)
}
res, err := client.Do(req)
if res != nil && res.StatusCode == 200 {
defer res.Body.Close()
body, err = io.ReadAll(res.Body)
} else if err != nil {
slog.Error("Failed to scrape data from embedHTML", "postID", i.PostID, "err", err)
}

var embedData gjson.Result
Expand Down Expand Up @@ -444,17 +439,15 @@ func scrapeFromGQL(postID string) ([]byte, error) {
}

client := http.Client{Transport: transport, Timeout: timeout}
for retries := 0; retries < 3; retries++ {
res, err := client.Do(req)
if res != nil && res.StatusCode == 200 {
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err == nil && len(body) > 0 {
return body, nil
}
} else if err != nil {
slog.Error("Failed to scrape data from GraphQL API", "postID", postID, "err", err)
res, err := client.Do(req)
if res != nil && res.StatusCode == 200 {
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err == nil && len(body) > 0 {
return body, nil
}
} else if err != nil {
slog.Error("Failed to scrape data from GraphQL API", "postID", postID, "err", err)
}
return nil, ErrNotFound
}

0 comments on commit 219c88c

Please sign in to comment.