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
use std::time::Duration;

use anyhow::{anyhow, Result};
use models::{BalanceResponse, CreateTaskResponse, TaskInfo, TaskResultResponse};
use reqwest::Client;
use serde_json::json;
use tasks::Task;
use tokio::time::sleep;

#[cfg(feature = "image")]
pub mod image;
pub mod models;
pub mod tasks;

pub struct Solver {
    client_key: String,
    client: Client,
    delay: Duration,
    use_sleep: bool,
}

impl Solver {
    /// Creates a new solver instance
    ///
    /// # Arguments
    ///
    /// * `key` - A string containing the CapBypass key
    ///
    /// # Example
    ///
    /// ```rust
    /// use rustycap::Solver;
    ///
    /// let solver = Solver::new("CB-XXXXXXXXX");
    /// ```
    pub fn new<T>(key: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            client_key: key.into(),
            client: Client::new(),
            use_sleep: true,
            delay: Duration::from_millis(2500),
        }
    }

    /// Configures the solver to not use a sleep in the `create_and_wait` function
    pub fn no_sleep(mut self) -> Self {
        self.use_sleep = false;

        self
    }

    /// Sets the delay for the `create_and_wait` function
    ///
    /// # Arguments
    ///
    /// * `delay` - The milliseconds to wait after each `get_task_info` request
    pub fn delay(mut self, delay: u64) -> Self {
        self.delay = Duration::from_millis(delay);

        self
    }

    /// Gets your current CapBypass balance
    pub async fn get_balance(&self) -> Result<BalanceResponse> {
        let data = json!({
            "clientKey": self.client_key,
        });
        let request = self
            .client
            .post("https://capbypass.com/api/getBalance")
            .header("content-type", "application/json")
            .json(&data)
            .send()
            .await?;
        let response = request.error_for_status()?;
        let response: BalanceResponse = response.json().await?;

        Ok(response)
    }

    /// Creates a task and returns the `task_id` and `error_id`
    ///
    /// # Arguments
    ///
    /// * `task` - A struct implementing the `Task` trait. Example: `FunCaptchaTask`
    pub async fn create_task<T>(&self, task: T) -> Result<CreateTaskResponse>
    where
        T: Task,
    {
        let task_data = task.serialize();
        let data = json!({
            "clientKey": self.client_key,
            "task": task_data,
        });
        let request = self
            .client
            .post("https://capbypass.com/api/createTask")
            .header("content-type", "application/json")
            .json(&data)
            .send()
            .await?;
        let response = request.error_for_status()?;
        let result: CreateTaskResponse = response.json().await?;

        Ok(result)
    }

    /// Retrieves the current task info for the given `task_id`
    pub async fn get_task_info<T>(&self, task_id: T) -> Result<TaskInfo>
    where
        T: Into<String>,
    {
        let task_id = task_id.into();
        let data = json!({
            "clientKey": self.client_key,
            "taskId": task_id,
        });
        let request = self
            .client
            .post("https://capbypass.com/api/getTaskResult")
            .header("content-type", "application/json")
            .json(&data)
            .send()
            .await?;
        let response = request.error_for_status()?;
        let response: TaskResultResponse = response.json().await?;

        match response.status.as_str() {
            "DONE" => {
                if let Some(solution) = response.solution {
                    Ok(TaskInfo::Done(solution))
                } else {
                    Ok(TaskInfo::Failed("Invalid response".to_string()))
                }
            }
            "FAILED" => Ok(TaskInfo::Failed(
                response
                    .error_description
                    .unwrap_or("errorDescription was null".to_string()),
            )),
            "DOES_NOT_EXIST" => Ok(TaskInfo::DoesNotExist),
            "PROCESSING" => Ok(TaskInfo::Processing),
            _ => Err(anyhow!(format!("Unknown task status: {}", response.status))),
        }
    }

    /// Creates a task and waits for the task to resolve
    /// #### ! This function loops and may use `tokio::time::sleep`
    ///
    /// # Arguments
    ///
    /// * `task` - A struct implementing the `Task` trait. Example: `FunCaptchaTask`
    pub async fn create_and_wait<T>(&self, task: T) -> Result<String>
    where
        T: Task,
    {
        let created = self.create_task(task).await?;

        if created.error_id == 0 {
            let task_id = created.task_id;

            loop {
                let status = self.get_task_info(&task_id).await?;

                match status {
                    TaskInfo::DoesNotExist => return Err(anyhow!("Task does not exist")),
                    TaskInfo::Done(solution) => return Ok(solution),
                    TaskInfo::Failed(reason) => {
                        return Err(anyhow!(format!("Task failed to solve: {}", reason)))
                    }
                    TaskInfo::Processing => {}
                };

                if self.use_sleep {
                    sleep(self.delay).await;
                }
            }
        } else {
            Err(anyhow!("Error"))
        }
    }
}