Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Xewdy444 committed Jan 7, 2024
1 parent 3f7403f commit 1b55360
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod proxy_utilities;

use anyhow::{bail, Context, Result};
#[cfg(windows)]
use anyhow::bail;
use anyhow::{Context, Result};
use clap::{command, Parser};
use futures::future;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
Expand Down
42 changes: 17 additions & 25 deletions src/proxy_utilities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum Proxy {
impl fmt::Display for Proxy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Proxy::Http(address) | Proxy::Socks5(address) => write!(f, "{address}"),
Proxy::Http(host) | Proxy::Socks5(host) => write!(f, "{host}"),
}
}
}
Expand Down Expand Up @@ -69,8 +69,8 @@ impl Proxy {
/// Returns the URL of the proxy.
fn url(&self) -> String {
match self {
Proxy::Http(_) => format!("http://{self}"),
Proxy::Socks5(_) => format!("socks5://{self}"),
Proxy::Http(host) => format!("http://{host}"),
Proxy::Socks5(host) => format!("socks5://{host}"),
}
}

Expand All @@ -87,9 +87,9 @@ impl Proxy {
bail!("Invalid proxy: {host}");
}

let address = IpAddr::from_str(parts[0])?;
let port: u16 = parts[1].parse()?;
Ok((address, port))
let addr = IpAddr::from_str(parts[0]).context("Invalid IP address")?;
let port: u16 = parts[1].parse().context("Invalid port")?;
Ok((addr, port))
}
}

Expand Down Expand Up @@ -172,8 +172,7 @@ impl ProxyScraper {
///
/// ## Errors
///
/// Returns an error if the request to the <https://checkerproxy.net/getAllProxy> page fails
/// or if the HTML could not be parsed.
/// Returns an error if the request to the <https://checkerproxy.net/getAllProxy> page fails.
pub async fn scrape_archive_urls(&self) -> Result<Vec<String>> {
let response = self
.client
Expand All @@ -184,11 +183,7 @@ impl ProxyScraper {

let html = response.text().await?;
let parser = Html::parse_document(&html);

let selector = match Selector::parse("li > a") {
Ok(selector) => selector,
Err(_) => bail!("Failed to parse HTML"),
};
let selector = Selector::parse("li > a").unwrap();

let mut archive_urls = Vec::new();

Expand Down Expand Up @@ -231,20 +226,17 @@ impl ProxyScraper {
) -> Result<Vec<Proxy>> {
let response = self
.client
.get(&archive_url)
.get(archive_url)
.send()
.await
.with_context(|| {
format!("Failed to get proxies from archive API URL: {archive_url}")
})?;
.context("Request failed")?
.error_for_status()
.context("Request returned an error status code")?;

let json: Value = serde_json::from_str(&response.text().await?)?;
let mut proxies = Vec::new();

for proxy_dict in json
.as_array()
.context("Invalid JSON received from archive API URL")?
{
for proxy_dict in json.as_array().context("Invalid JSON received")? {
if anonymous_only {
let kind = match proxy_dict.get("kind") {
Some(value) => match value.as_u64() {
Expand All @@ -259,7 +251,7 @@ impl ProxyScraper {
}
}

let address = match proxy_dict.get("addr") {
let host = match proxy_dict.get("addr") {
Some(value) => match value.as_str() {
Some(str_value) => str_value.to_string(),
None => continue,
Expand All @@ -269,9 +261,9 @@ impl ProxyScraper {

let proxy = match proxy_dict.get("type") {
Some(value) => match value.as_u64() {
Some(1) => Proxy::Http(address),
Some(2) => Proxy::Http(address),
Some(4) => Proxy::Socks5(address),
Some(1) => Proxy::Http(host),
Some(2) => Proxy::Http(host),
Some(4) => Proxy::Socks5(host),
_ => continue,
},
None => continue,
Expand Down

0 comments on commit 1b55360

Please sign in to comment.