Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
convert to async
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbasPL committed Nov 23, 2023
1 parent 1ae0db2 commit aae811b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 43 deletions.
23 changes: 13 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ edition = "2021"
[dependencies]
darknet = "0.4"
anyhow = "1.0"
reqwest = { version = "0.11", default-features = false, features = ["blocking"] }
reqwest = { version = "0.11", default-features = false }
tl = "0.7"
image = "0.24"
clap = { version = "4.4.7", features = ["derive", "string", "env"] }
clap = { version = "4.4.7", features = ["derive", "string", "env"] }
tokio = { version = "1", features = ["macros"]}
13 changes: 7 additions & 6 deletions examples/get-test-data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use aero2solver::{
};
use anyhow::{anyhow, Result};

fn run(solver: &mut Aero2Solver) -> Result<()> {
async fn run(solver: &mut Aero2Solver) -> Result<()> {
let client = PortalClient::new(BASE_URL, USER_AGENT)?;

let state = client.get_state()?;
let state = client.get_state().await?;

let mut tries = 0;
let (solution, captcha) = loop {
tries += 1;
println!("Trying to solve captcha (try {})", tries);
let captcha = client.get_captcha(&state.session_id)?;
let captcha = client.get_captcha(&state.session_id).await?;
match solver.solve(&captcha, 0.9, 8) {
Ok(solution) => {
println!("Captcha solved as {} after {}", solution, tries);
Expand All @@ -29,7 +29,7 @@ fn run(solver: &mut Aero2Solver) -> Result<()> {
}
};

let state = client.submit_captcha(&state.session_id, &solution)?;
let state = client.submit_captcha(&state.session_id, &solution).await?;
match state.message {
Some(ref message) if message.eq("Rozłącz i ponownie połącz się z Internetem.") => {
println!("Captcha solved, code: {}", solution);
Expand All @@ -46,7 +46,8 @@ fn run(solver: &mut Aero2Solver) -> Result<()> {
Ok(())
}

fn main() -> Result<()> {
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let mut solver = Aero2Solver::new(
"./model/captcha.names",
"./model/captcha.cfg",
Expand All @@ -62,7 +63,7 @@ fn main() -> Result<()> {
break;
}

match run(&mut solver) {
match run(&mut solver).await {
Ok(_) => success_count += 1,
Err(e) => println!("Error: {}", e),
}
Expand Down
13 changes: 7 additions & 6 deletions examples/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use aero2solver::{
};
use anyhow::Result;

fn run(solver: &mut Aero2Solver) -> Result<()> {
async fn run(solver: &mut Aero2Solver) -> Result<()> {
let client = PortalClient::new(BASE_URL, USER_AGENT)?;

let state = client.get_state()?;
let state = client.get_state().await?;

let mut tries = 0;
let solution: String = loop {
tries += 1;
println!("Trying to solve captcha (try {})", tries);
let captcha = client.get_captcha(&state.session_id)?;
let captcha = client.get_captcha(&state.session_id).await?;
match solver.solve(&captcha, 0.8, 8) {
Ok(solution) => {
println!("Captcha solved as {} after {}", solution, tries);
Expand All @@ -24,7 +24,7 @@ fn run(solver: &mut Aero2Solver) -> Result<()> {
}
};

let state = client.submit_captcha(&state.session_id, &solution)?;
let state = client.submit_captcha(&state.session_id, &solution).await?;
println!(
"Captcha solved with message: {}",
state.message.unwrap_or_default()
Expand All @@ -33,14 +33,15 @@ fn run(solver: &mut Aero2Solver) -> Result<()> {
Ok(())
}

fn main() -> Result<()> {
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let mut solver = Aero2Solver::new(
"./model/captcha.names",
"./model/captcha.cfg",
"./model/captcha.weights",
)?;

loop {
run(&mut solver)?;
run(&mut solver).await?;
}
}
25 changes: 16 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ struct Args {
solved_delay: f32,
}

fn main() -> Result<()> {
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let Args {
model_cfg,
weights,
Expand All @@ -68,10 +69,12 @@ fn main() -> Result<()> {
let solved_sleep_time = Duration::from_secs_f32(solved_delay);

loop {
let was_solved = run(&mut solver, threshold, error_sleep_time).unwrap_or_else(|x| {
println!("Error: {}", x);
false
});
let was_solved = run(&mut solver, threshold, error_sleep_time)
.await
.unwrap_or_else(|x| {
println!("Error: {}", x);
false
});

let sleep_time = match was_solved {
true => {
Expand All @@ -85,12 +88,16 @@ fn main() -> Result<()> {
}
}

fn run(solver: &mut Aero2Solver, min_threshold: f32, fail_sleep_time: Duration) -> Result<bool> {
async fn run(
solver: &mut Aero2Solver,
min_threshold: f32,
fail_sleep_time: Duration,
) -> Result<bool> {
let client = PortalClient::new(BASE_URL, USER_AGENT)?;

let mut was_required = false;

let mut state = client.get_state()?;
let mut state = client.get_state().await?;
loop {
if !state.captcha_present {
let status = match was_required {
Expand Down Expand Up @@ -125,7 +132,7 @@ fn run(solver: &mut Aero2Solver, min_threshold: f32, fail_sleep_time: Duration)
break Err(anyhow!("Too many tries"));
}
println!("Trying to solve captcha (try {})", tries);
let captcha = client.get_captcha(&state.session_id)?;
let captcha = client.get_captcha(&state.session_id).await?;
match solver.solve(&captcha, min_threshold, 8) {
Ok(solution) => {
println!("Captcha solved as {} after {}", solution, tries);
Expand All @@ -135,6 +142,6 @@ fn run(solver: &mut Aero2Solver, min_threshold: f32, fail_sleep_time: Duration)
}
}?;

state = client.submit_captcha(&state.session_id, &solution)?;
state = client.submit_captcha(&state.session_id, &solution).await?;
}
}
26 changes: 16 additions & 10 deletions src/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use ::reqwest::redirect::Policy;
use anyhow::{anyhow, Ok, Result};
use reqwest::blocking as reqwest;
use reqwest;

pub struct CaptchaState {
pub captcha_present: bool,
Expand Down Expand Up @@ -62,31 +62,35 @@ impl PortalClient {
})
}

pub fn get_state(&self) -> Result<CaptchaState> {
pub async fn get_state(&self) -> Result<CaptchaState> {
let page = self
.client
.post(self.base_url)
.form(&[("viewForm", "true")])
.send()?
.text()?;
.send()
.await?
.text()
.await?;

self.parse_page(&page)
}

pub fn get_captcha(&self, session_id: &str) -> Result<Vec<u8>> {
pub async fn get_captcha(&self, session_id: &str) -> Result<Vec<u8>> {
let bytes = self
.client
.get(format!(
"{}getCaptcha.html?PHPSESSID={}",
self.base_url, session_id
))
.send()?
.bytes()?;
.send()
.await?
.bytes()
.await?;

Ok(bytes.to_vec())
}

pub fn submit_captcha(&self, session_id: &str, solution: &str) -> Result<CaptchaState> {
pub async fn submit_captcha(&self, session_id: &str, solution: &str) -> Result<CaptchaState> {
let page = self
.client
.post(self.base_url)
Expand All @@ -95,8 +99,10 @@ impl PortalClient {
("viewForm", "true"),
("captcha", solution),
])
.send()?
.text()?;
.send()
.await?
.text()
.await?;

self.parse_page(&page)
}
Expand Down

0 comments on commit aae811b

Please sign in to comment.