-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
144 lines (125 loc) · 3.98 KB
/
main.rs
File metadata and controls
144 lines (125 loc) · 3.98 KB
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
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use std::env;
use std::thread;
use std::time::Duration;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct CreateTaskRequest {
client_key: String,
task: Task,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Task {
#[serde(rename = "type")]
task_type: String,
website_url: String,
website_key: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CreateTaskResponse {
error_id: i32,
error_code: Option<String>,
error_description: Option<String>,
task_id: Option<String>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct GetTaskResultRequest {
client_key: String,
task_id: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct GetTaskResultResponse {
error_id: i32,
error_code: Option<String>,
error_description: Option<String>,
status: Option<String>,
solution: Option<Solution>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Solution {
token: String,
}
fn main() {
// Cargar variables de entorno
dotenvy::dotenv().ok();
let api_key = env::var("CAPSOLVER_API_KEY").expect("CAPSOLVER_API_KEY debe estar configurado");
let client = Client::new();
let base_url = "https://api.capsolver.com";
// Paso 1: Crear tarea
println!("Creando tarea MtCaptchaTaskProxyLess...");
let create_request = CreateTaskRequest {
client_key: api_key.clone(),
task: Task {
task_type: "MtCaptchaTaskProxyLess".to_string(),
website_url: "https://example.com".to_string(),
website_key: "MTPublic-DemoKey9M".to_string(),
},
};
let create_response = client
.post(format!("{}/createTask", base_url))
.json(&create_request)
.send()
.expect("Error al enviar la solicitud")
.json::<CreateTaskResponse>()
.expect("Error al analizar la respuesta");
if create_response.error_id != 0 {
eprintln!(
"Error al crear la tarea: {} - {}",
create_response.error_code.unwrap_or_default(),
create_response.error_description.unwrap_or_default()
);
return;
}
let task_id = create_response.task_id.expect("No se devolvió ID de tarea");
println!("Tarea creada con éxito. ID de tarea: {}", task_id);
// Paso 2: Consultar el resultado
println!("Esperando a que se complete la tarea...");
loop {
thread::sleep(Duration::from_secs(2));
let result_request = GetTaskResultRequest {
client_key: api_key.clone(),
task_id: task_id.clone(),
};
let result_response = client
.post(format!("{}/getTaskResult", base_url))
.json(&result_request)
.send()
.expect("Error al enviar la solicitud")
.json::<GetTaskResultResponse>()
.expect("Error al analizar la respuesta");
if result_response.error_id != 0 {
eprintln!(
"Error al obtener el resultado de la tarea: {} - {}",
result_response.error_code.unwrap_or_default(),
result_response.error_description.unwrap_or_default()
);
return;
}
match result_response.status.as_deref() {
Some("ready") => {
let solution = result_response.solution.expect("No se devolvió solución");
println!("\n¡Tarea completada con éxito!");
println!("Token: {}", solution.token);
break;
}
Some("processing") => {
print!(".");
std::io::Write::flush(&mut std::io::stdout()).ok();
}
Some("failed") => {
eprintln!("\n¡La tarea falló!");
return;
}
_ => {
eprintln!("\nEstado desconocido");
return;
}
}
}
}