Skip to content

Commit

Permalink
Add a workaround to make yuntrack work
Browse files Browse the repository at this point in the history
Do the request twice with a cookie jar
  • Loading branch information
alufers committed Jan 24, 2024
1 parent 56cac70 commit 2114bcc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions paczkobot/track_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (t *TrackCommand) Execute(ctx context.Context) error {
msg.ParseMode = "HTML"
_, err := t.App.Bot.Send(msg)
if err != nil {
log.Printf("msgval: %v", msgText)
log.Printf("failed to edit status msg: %v", err)
return
}
Expand Down
2 changes: 2 additions & 0 deletions providers/inpost/inposttrackingapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/url"

"github.com/alufers/paczkobot/commondata"
"github.com/alufers/paczkobot/commonerrors"
"github.com/alufers/paczkobot/httphelpers"
)
Expand All @@ -19,6 +20,7 @@ func GetTrackingData(ctx context.Context, parcelNumber string) (*TrackingAPISche
}

client := httphelpers.NewClientWithLogger()
commondata.SetCommonHTTPHeaders(&request.Header)
resp, err := client.Do(request)
if err != nil {
return nil, fmt.Errorf("failed to make GET request to get tracking data (url: %v): %w", request.URL.String(), err)
Expand Down
44 changes: 39 additions & 5 deletions providers/yuntrack/yuntrack_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"time"

"github.com/alufers/paczkobot/commondata"
"github.com/alufers/paczkobot/commonerrors"
"github.com/alufers/paczkobot/httphelpers"
"golang.org/x/net/publicsuffix"
)

type YunTrack struct{}
Expand All @@ -24,8 +27,8 @@ func (p *YunTrack) MatchesNumber(trackingNumber string) bool {
return true
}

func (p *YunTrack) Track(ctx context.Context, trackingNumber string) (*commondata.TrackingData, error) {
client := httphelpers.NewClientWithLogger()
// Utility method to create a new HTTP request
func (p *YunTrack) createRequest(ctx context.Context, trackingNumber string) (*http.Request, error) {
bodyData := &YunTrackRequest{
CaptchaVerification: "",
Year: 0,
Expand All @@ -45,6 +48,7 @@ func (p *YunTrack) Track(ctx context.Context, trackingNumber string) (*commondat
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

/*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,de-DE;q=0.8,de;q=0.7,pl-PL;q=0.6,pl;q=0.5
Expand Down Expand Up @@ -79,6 +83,22 @@ func (p *YunTrack) Track(ctx context.Context, trackingNumber string) (*commondat
req.Header.Set("Sec-Fetch-Site", "same-site")
req.Header.Set("Sec-GPC", "1")
req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36")
return req, nil
}

func (p *YunTrack) Track(ctx context.Context, trackingNumber string) (*commondata.TrackingData, error) {
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, fmt.Errorf("failed to create cookie jar: %w", err)
}
client := httphelpers.NewClientWithLogger()
client.Jar = jar

req, err := p.createRequest(ctx, trackingNumber)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

res, err := client.Do(req)
if err != nil {
return nil, commonerrors.NewNetworkError(p.GetName(), req)
Expand All @@ -87,10 +107,24 @@ func (p *YunTrack) Track(ctx context.Context, trackingNumber string) (*commondat
if res.StatusCode == http.StatusNotFound {
return nil, commonerrors.NotFoundError
}
// Check if the response status is not 200 and retry with cookies
if res.StatusCode != 200 {
// body, _ := io.ReadAll(res.Body)
// log.Printf("YUNTRACK : %v", string(body))
return nil, fmt.Errorf("HTTP status code %v", res.StatusCode)
req, err = p.createRequest(ctx, trackingNumber)
if err != nil {
return nil, fmt.Errorf("failed to create second request: %w", err)
}
// Perform the retry with the same request
res, err = client.Do(req)
if err != nil {
log.Printf("XDDD Failed to retry request: %v", err)
return nil, commonerrors.NewNetworkError(p.GetName(), req)
}
defer res.Body.Close()

// Check again the status code
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP status code %v after retry", res.StatusCode)
}
}

decoder := json.NewDecoder(res.Body)
Expand Down

0 comments on commit 2114bcc

Please sign in to comment.