forked from AdminAnticaptcha/anticaptcha-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnticaptchaBase.cs
225 lines (171 loc) · 6.85 KB
/
AnticaptchaBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Anticaptcha.ApiResponse;
using Anticaptcha.Helper;
namespace Anticaptcha
{
public abstract class AnticaptchaBase
{
public enum ProxyTypeOption
{
Http,
Socks4,
Socks5
}
private const string Host = "api.anti-captcha.com";
private const SchemeType Scheme = SchemeType.Https;
public string ErrorMessage { get; private set; }
public int TaskId { get; private set; }
public string ClientKey { set; private get; }
public TaskResultResponse TaskInfo { get; protected set; }
public abstract JObject GetPostData();
public bool CreateTask()
{
var taskJson = GetPostData();
if (taskJson == null)
{
DebugHelper.Out("A task preparing error.", DebugHelper.Type.Error);
return false;
}
// if you don't need to see the raw JSON request - comment the next line out
DebugHelper.Out(taskJson.ToString(), DebugHelper.Type.Info);
var jsonPostData = new JObject();
jsonPostData["clientKey"] = ClientKey;
jsonPostData["task"] = taskJson;
DebugHelper.Out("Connecting to " + Host, DebugHelper.Type.Info);
dynamic postResult = JsonPostRequest(ApiMethod.CreateTask, jsonPostData);
if (postResult == null || postResult.Equals(false))
{
DebugHelper.Out("API error", DebugHelper.Type.Error);
return false;
}
var response = new CreateTaskResponse(postResult);
if (!response.ErrorId.Equals(0))
{
ErrorMessage = response.ErrorDescription;
DebugHelper.Out(
"API error " + response.ErrorId + ": " + response.ErrorDescription,
DebugHelper.Type.Error
);
return false;
}
if (response.TaskId == null)
{
DebugHelper.JsonFieldParseError("taskId", postResult);
return false;
}
TaskId = (int)response.TaskId;
DebugHelper.Out("Task ID: " + TaskId, DebugHelper.Type.Success);
return true;
}
public bool WaitForResult(int maxSeconds = 120, int currentSecond = 0)
{
if (currentSecond >= maxSeconds)
{
DebugHelper.Out("Time's out.", DebugHelper.Type.Error);
return false;
}
if (currentSecond.Equals(0))
{
DebugHelper.Out("Waiting for 3 seconds...", DebugHelper.Type.Info);
Thread.Sleep(3000);
}
else
{
Thread.Sleep(1000);
}
DebugHelper.Out("Requesting the task status", DebugHelper.Type.Info);
var jsonPostData = new JObject();
jsonPostData["clientKey"] = ClientKey;
jsonPostData["taskId"] = TaskId;
dynamic postResult = JsonPostRequest(ApiMethod.GetTaskResult, jsonPostData);
if (postResult == null || postResult.Equals(false))
{
DebugHelper.Out("API error", DebugHelper.Type.Error);
return false;
}
TaskInfo = new TaskResultResponse(postResult);
if (!TaskInfo.ErrorId.Equals(0))
{
ErrorMessage = TaskInfo.ErrorDescription;
DebugHelper.Out("API error " + TaskInfo.ErrorId + ": " + ErrorMessage, DebugHelper.Type.Error);
return false;
}
if (TaskInfo.Status.Equals(TaskResultResponse.StatusType.Processing))
{
DebugHelper.Out("The task is still processing...", DebugHelper.Type.Info);
return WaitForResult(maxSeconds, currentSecond + 1);
}
if (TaskInfo.Status.Equals(TaskResultResponse.StatusType.Ready))
{
if (TaskInfo.Solution.GRecaptchaResponse == null && TaskInfo.Solution.Text == null
&& TaskInfo.Solution.Answers == null && TaskInfo.Solution.Token == null &&
TaskInfo.Solution.Challenge == null && TaskInfo.Solution.Seccode == null &&
TaskInfo.Solution.Validate == null && TaskInfo.Solution.CellNumbers.Count == 0)
{
DebugHelper.Out("Got no 'solution' field from API", DebugHelper.Type.Error);
return false;
}
DebugHelper.Out("The task is complete!", DebugHelper.Type.Success);
return true;
}
ErrorMessage = "An unknown API status, please update your software";
DebugHelper.Out(ErrorMessage, DebugHelper.Type.Error);
return false;
}
private dynamic JsonPostRequest(ApiMethod methodName, JObject jsonPostData)
{
string error;
var methodNameStr = char.ToLowerInvariant(methodName.ToString()[0]) + methodName.ToString().Substring(1);
dynamic data = HttpHelper.Post(
new Uri(Scheme + "://" + Host + "/" + methodNameStr),
JsonConvert.SerializeObject(jsonPostData, Formatting.Indented),
out error
);
if (string.IsNullOrEmpty(error))
if (data == null)
error = "Got empty or invalid response from API";
else
return data;
else
error = "HTTP or JSON error: " + error;
DebugHelper.Out(error, DebugHelper.Type.Error);
return false;
}
public double? GetBalance()
{
var jsonPostData = new JObject();
jsonPostData["clientKey"] = ClientKey;
dynamic postResult = JsonPostRequest(ApiMethod.GetBalance, jsonPostData);
if (postResult == null || postResult.Equals(false))
{
DebugHelper.Out("API error", DebugHelper.Type.Error);
return null;
}
var balanceResponse = new BalanceResponse(postResult);
if (!balanceResponse.ErrorId.Equals(0))
{
ErrorMessage = balanceResponse.ErrorDescription;
DebugHelper.Out(
"API error " + balanceResponse.ErrorId + ": " + balanceResponse.ErrorDescription,
DebugHelper.Type.Error
);
return null;
}
return balanceResponse.Balance;
}
private enum ApiMethod
{
CreateTask,
GetTaskResult,
GetBalance
}
private enum SchemeType
{
Http,
Https
}
}
}