Skip to content

Commit

Permalink
remove retries for exceeding posts limit
Browse files Browse the repository at this point in the history
  • Loading branch information
unkmonster committed Aug 23, 2024
1 parent d611638 commit 32c934e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
11 changes: 11 additions & 0 deletions twitter/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package twitter
import (
"fmt"
"net/url"

"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
)

const HOST = "https://x.com"
Expand Down Expand Up @@ -181,3 +184,11 @@ func (l *likes) QueryParam() url.Values {
func (l *likes) SetCursor(cursor string) {
l.cursor = cursor
}

func CheckApiResp(resp *resty.Response) error {
errors := gjson.GetBytes(resp.Body(), "errors")
if errors.Exists() {
return fmt.Errorf(errors.String())
}
return nil
}
8 changes: 6 additions & 2 deletions twitter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ func Login(ctx context.Context, authToken string, ct0 string) (*resty.Client, st
return !strings.HasSuffix(r.Request.RawRequest.Host, "twimg.com") && err != nil
})
client.AddRetryCondition(func(r *resty.Response, err error) bool {
// For OverCapacity, Rate Limit Exceed
return r.Request.RawRequest.Host == "x.com" && (r.StatusCode() == 400 || r.StatusCode() == 429)
// For OverCapacity
return r.Request.RawRequest.Host == "x.com" && r.StatusCode() == 400
})
client.AddRetryCondition(func(r *resty.Response, err error) bool {
// 仅重试 429 Rate Limit Exceed
return r.Request.RawRequest.Host == "x.com" && r.StatusCode() == 429 && CheckApiResp(r) == nil
})

client.SetTransport(&http.Transport{
Expand Down
4 changes: 3 additions & 1 deletion twitter/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func getTimelineResp(ctx context.Context, api timelineApi, client *resty.Client)
if err = utils.CheckRespStatus(resp); err != nil {
return nil, err
}

if err = CheckApiResp(resp); err != nil {
return nil, err
}
return resp.Body(), nil
}

Expand Down

0 comments on commit 32c934e

Please sign in to comment.