Skip to content

Commit d8c26ec

Browse files
committed
Context Support for Capsolver
1 parent 662ddd6 commit d8c26ec

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

captchatools-go/capsolver.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package captchatoolsgo
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
7+
"errors"
68
"fmt"
79
"io"
810
"net/http"
@@ -51,7 +53,11 @@ func (c Capsolver) getBalance() (float32, error) {
5153
}
5254

5355
func (c Capsolver) GetToken(additional ...*AdditionalData) (*CaptchaAnswer, error) {
54-
return c.getCaptchaAnswer(additional...)
56+
return c.getCaptchaAnswer(context.Background(), additional...)
57+
}
58+
59+
func (c Capsolver) GetTokenWithContext(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) {
60+
return c.getCaptchaAnswer(ctx, additional...)
5561
}
5662

5763
// Method to get Queue ID from the API.
@@ -87,7 +93,7 @@ func (c Capsolver) getID(data *AdditionalData) (string, error) {
8793
return "", ErrMaxAttempts
8894
}
8995

90-
func (c Capsolver) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAnswer, error) {
96+
func (c Capsolver) getCaptchaAnswer(ctx context.Context, additional ...*AdditionalData) (*CaptchaAnswer, error) {
9197
var data *AdditionalData = nil
9298
if len(additional) > 0 {
9399
data = additional[0]
@@ -110,8 +116,13 @@ func (c Capsolver) getCaptchaAnswer(additional ...*AdditionalData) (*CaptchaAnsw
110116
})
111117
response := &capmonsterTokenResponse{}
112118
for i := 0; i < 50; i++ {
113-
resp, err := http.Post("https://api.capsolver.com/getTaskResult", "application/json", bytes.NewBuffer([]byte(payload)))
119+
reqToMake, _ := http.NewRequestWithContext(ctx, "POST", "https://api.capsolver.com/getTaskResult", bytes.NewBuffer(payload))
120+
reqToMake.Header.Add("Content-Type", "application/json")
121+
resp, err := makeRequest(reqToMake)
114122
if err != nil {
123+
if errors.Is(err, context.DeadlineExceeded) {
124+
return nil, fmt.Errorf("getCaptchaAnswer error: %w", err)
125+
}
115126
time.Sleep(3 * time.Second)
116127
continue
117128
}

captchatools-go/harvester.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func NewHarvester(solving_site site, config *Config) (Harvester, error) {
7070
switch solving_site {
7171
case AnticaptchaSite:
7272
h = &Anticaptcha{config: config}
73+
case CapmonsterSite:
74+
h = &Capmonster{config: config}
75+
case TwoCaptchaSite:
76+
h = &Twocaptcha{config: config}
77+
case CapsolverSite:
78+
h = &Capsolver{config}
79+
default:
7380
return nil, ErrNoHarvester
7481
}
7582
return h, nil

0 commit comments

Comments
 (0)