Skip to content
This repository was archived by the owner on Aug 25, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions anticaptcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -140,8 +141,18 @@ func (c *Client) createTaskImage(imgString string) (float64, error) {
// Decode response
responseBody := make(map[string]interface{})
json.NewDecoder(resp.Body).Decode(&responseBody)
// TODO treat api errors and handle them properly
return responseBody["taskId"].(float64), nil

errorDescription, ok := responseBody["errorDescription"]
if ok {
return 0, fmt.Errorf("server returned: %s", errorDescription)
}

taskId, ok := responseBody["taskId"]
if !ok {
return 0, errors.New("failed to get a response")
}

return taskId.(float64), nil
}

// SendImage Method to encapsulate the processing of the image captcha
Expand Down Expand Up @@ -170,5 +181,16 @@ func (c *Client) SendImage(imgString string) (string, error) {
break
}
}
return response["solution"].(map[string]interface{})["text"].(string), nil

errorDescription, ok := response["errorDescription"]
if ok {
return "", fmt.Errorf("server returned: %s", errorDescription)
}

solution, ok := response["solution"]
if !ok {
return "", errors.New("failed to get a response")
}

return solution.(map[string]interface{})["text"].(string), nil
}