rust_anticaptcha/image_captcha.rs
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
use serde_json::Value;
use super::core::captcha_interface::CaptchaInterface;
use super::core::enums::ImageTaskType;
/// Captcha solving method - `ImageToTextTask` and `ImageToCoordinatesTask`
///
/// # Examples
///
/// With already prepared base64 string
/// ```no_run
/// use serde_json::json;
///
/// use rust_anticaptcha::core::enums::ImageTaskType;
/// use rust_anticaptcha::image_captcha::ImageCaptcha;
///
/// async fn run() {
/// let map = json!({"body": "base64_string"});
///
/// let mut image_to_text_client = ImageCaptcha::new("API_KEY".to_string());
/// image_to_text_client.captcha_handler(ImageTaskType::ImageToCoordinatesTask, map).await;
/// }
/// ```
///
/// With image as file
/// ```no_run
/// use serde_json::json;
///
/// use rust_anticaptcha::core::enums::ImageTaskType;
/// use rust_anticaptcha::image_captcha::ImageCaptcha;
/// use rust_anticaptcha::instruments::image_instrument::ImageInstrument;
///
/// async fn run() {
/// let image_instrument = ImageInstrument::new();
/// let image_file_base64 = image_instrument.read_image_file("files/captcha-image.jpg".to_string());
///
/// let map = json!({"body": image_file_base64});
///
/// let mut image_to_text_client = ImageCaptcha::new("API_KEY".to_string());
/// image_to_text_client.captcha_handler(ImageTaskType::ImageToTextTask, map).await;
/// }
/// ```
///
/// With image as link
/// ```no_run
/// use serde_json::json;
///
/// use rust_anticaptcha::core::enums::ImageTaskType;
/// use rust_anticaptcha::image_captcha::ImageCaptcha;
/// use rust_anticaptcha::instruments::image_instrument::ImageInstrument;
///
/// async fn run() {
/// let image_instrument = ImageInstrument::new();
/// let image_link_base64 = image_instrument.read_image_link("https://captcha-image.jpg".to_string()).await;
///
/// let map = json!({"body": image_link_base64});
///
/// let mut image_to_text_client = ImageCaptcha::new("API_KEY".to_string());
/// image_to_text_client.captcha_handler(ImageTaskType::ImageToTextTask, map).await;
/// }
/// ```
///
/// # Notes
/// Read more here:
///
/// <https://anti-captcha.com/apidoc/task-types/ImageToTextTask>
/// <https://anti-captcha.com/apidoc/task-types/ImageToCoordinatesTask>
///
pub struct ImageCaptcha {
pub captcha_interface: CaptchaInterface,
}
impl ImageCaptcha {
/// Method init new ImageCaptcha struct with Captcha Interface
///
/// # Arguments
/// `api_key` - Service API key
///
/// # Examples
///
/// ```
/// use rust_anticaptcha::image_captcha::ImageCaptcha;
///
/// let image_to_text_client = ImageCaptcha::new("API_KEY".to_string());
/// ```
/// # Returns
/// Method return new `ImageCaptcha` instance
///
pub fn new(api_key: String) -> Self {
ImageCaptcha {
captcha_interface: CaptchaInterface::new(api_key),
}
}
/// Method run captcha solving logic
///
/// # Arguments
/// `captcha_type` - One of image captcha types from `ImageTaskType`
/// `task_payload` - JSON with captcha task payload
///
/// # Examples
///
/// ```no_run
/// use serde_json::json;
///
/// use rust_anticaptcha::core::enums::ImageTaskType;
/// use rust_anticaptcha::image_captcha::ImageCaptcha;
///
/// async fn run() {
/// let map = json!({"body": "base64_string"});
///
/// let mut image_to_text_client = ImageCaptcha::new("API_KEY".to_string());
/// image_to_text_client.captcha_handler(ImageTaskType::ImageToTextTask, map).await;
/// }
/// ```
pub async fn captcha_handler(
&mut self,
captcha_type: ImageTaskType,
task_payload: Value,
) -> Value {
self.captcha_interface
.solve_captcha(captcha_type, task_payload)
.await
}
}