Skip to content

Commit

Permalink
feat: put captcha solver behind feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Beastwick18 committed Jun 6, 2024
1 parent 975c0c7 commit 3645f92
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 30 deletions.
590 changes: 589 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ shellexpand = "3.1.0"
indexmap = "2.2.6"
color-to-tui = "0.3.0"
human_bytes = "0.4.3"
ratatui-image = { version = "1.0.1", optional = true }
image = { version = "0.25.1", optional = true }

[lib]
name = "nyaa"
path = "src/lib.rs"

[features]
captcha = ["dep:ratatui-image", "dep:image"]

[target.'cfg(unix)'.dependencies]
nix = { version = "0.29.0", features = ["signal"] }

Expand Down
18 changes: 11 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use ratatui::{
use reqwest::cookie::Jar;
use tokio::{sync::mpsc, task::AbortHandle};

#[cfg(feature = "captcha")]
use crate::widget::captcha::CaptchaPopup;
use crate::{
client::{Client, DownloadResult},
clip,
Expand Down Expand Up @@ -105,7 +107,8 @@ widgets! {
page: [Mode::Page] => PagePopup,
user: [Mode::User] => UserPopup,
help: [Mode::Help] => HelpPopup,
// captcha: [Mode::Captcha] => CaptchaPopup,
#[cfg(feature = "captcha")]
captcha: [Mode::Captcha] => CaptchaPopup,
}
}

Expand Down Expand Up @@ -403,12 +406,13 @@ impl App {
self.widgets.results.reset();
ctx.results = rt;
}
// Ok(SourceResults::Captcha(c)) => {
// ctx.results = Results::default();
// ctx.mode = Mode::Captcha;
// self.widgets.captcha.image = Some(c);
// self.widgets.captcha.input.clear();
// }
#[cfg(feature = "captcha")]
Ok(SourceResults::Captcha(c)) => {
ctx.results = Results::default();
ctx.mode = Mode::Captcha;
self.widgets.captcha.image = Some(c);
self.widgets.captcha.input.clear();
}
Err(e) => {
// Clear results on error
ctx.results = Results::default();
Expand Down
5 changes: 5 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ macro_rules! widgets {
)+
[popups]: {
$(
$(#[$docs:meta])*
$pwidget:ident:
$(
[$pmode:pat_param]
Expand All @@ -27,6 +28,7 @@ macro_rules! widgets {
pub $widget: $struc,
)+
$(
$(#[$docs])*
pub $pwidget: $pstruc,
)+
}
Expand All @@ -35,6 +37,7 @@ macro_rules! widgets {
fn draw_popups(&mut self, ctx: &$crate::app::Context, f: &mut ratatui::Frame) {
match ctx.mode {
$(
$(#[$docs])*
$($pmode => self.$pwidget.draw(f, ctx, f.size()),)?
)+
_ => {}
Expand All @@ -48,6 +51,7 @@ macro_rules! widgets {
$($mode => $struc::get_help(),)?
)+
$(
$(#[$docs])*
$($pmode => $pstruc::get_help(),)?
)+
_ => None,
Expand All @@ -60,6 +64,7 @@ macro_rules! widgets {
$($mode => self.$widget.handle_event(ctx, evt),)?
)+
$(
$(#[$docs])*
$($pmode => self.$pwidget.handle_event(ctx, evt),)?
)+
_ => {}
Expand Down
9 changes: 7 additions & 2 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use self::{
torrent_galaxy::{TgxConfig, TorrentGalaxyHtmlSource},
};

#[cfg(feature = "captcha")]
use ratatui_image::protocol::StatefulProtocol;

pub mod nyaa_html;
pub mod nyaa_rss;
pub mod sukebei_nyaa;
Expand All @@ -27,13 +30,15 @@ pub mod torrent_galaxy;
#[derive(Clone)]
pub enum SourceResults {
Results(Results),
// Captcha(Box<dyn StatefulProtocol>),
#[cfg(feature = "captcha")]
Captcha(Box<dyn StatefulProtocol>),
}

#[derive(Clone)]
pub enum SourceResponse {
Results(ResultResponse),
// Captcha(Box<dyn StatefulProtocol>),
#[cfg(feature = "captcha")]
Captcha(Box<dyn StatefulProtocol>),
}

#[derive(Serialize, Deserialize, Clone, Default)]
Expand Down
40 changes: 22 additions & 18 deletions src/source/torrent_galaxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,9 @@ impl Source for TorrentGalaxyHtmlSource {
let time = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis();

let hash = "4578678889c4b42ae37b543434c81d85";
// let hash = "ff9df5a6db0ebe6bd636296da767a587";
let base_url = Url::parse(&tgx.base_url)?;
let mut hash_url = base_url.clone().join("hub.php")?;
hash_url.set_query(Some(&format!("a=vlad&u={}", time)));
// let hash_url = format!("https://torrentgalaxy.to/hub.php?a=vlad&u={}", time);
client
.post(hash_url.clone())
.body(format!("fash={}", hash))
Expand All @@ -412,22 +410,28 @@ impl Source for TorrentGalaxyHtmlSource {
// If that doesn't work, try making the user solve a captcha
let content = try_get_content(client, tgx.timeout, &url).await?;
if Html::parse_document(&content).select(table_sel).count() == 0 {
return Err("Unable to get response, most likely due to rate limit.\nWait a bit before retrying...".into());
// let mut request = client.get("https://torrentgalaxy.to/captcha/cpt_show.pnp?v=txlight&63fd4c746843c74b53ca60277192fb48");
// if let Some(timeout) = tgx.timeout {
// request = request.timeout(Duration::from_secs(timeout));
// }
// let response = request
// .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0")
// .send()
// .await?;
// let bytes = response.bytes().await?;
// let mut picker = ratatui_image::picker::Picker::new((1, 2));
// picker.protocol_type = ratatui_image::picker::ProtocolType::Halfblocks;
// let dyn_image = image::load_from_memory(&bytes[..])?;
// let image = picker.new_resize_protocol(dyn_image);
//
// return Ok(SourceResponse::Captcha(image));
#[cfg(not(feature = "captcha"))]
{
return Err("Unable to get response, most likely due to rate limit.\nWait a bit before retrying...".into());
}
#[cfg(feature = "captcha")]
{
let mut request = client.get("https://torrentgalaxy.to/captcha/cpt_show.pnp?v=txlight&63fd4c746843c74b53ca60277192fb48");
if let Some(timeout) = tgx.timeout {
request = request.timeout(Duration::from_secs(timeout));
}
let response = request
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0")
.send()
.await?;
let bytes = response.bytes().await?;
let mut picker = ratatui_image::picker::Picker::new((1, 2));
picker.protocol_type = ratatui_image::picker::ProtocolType::Halfblocks;
let dyn_image = image::load_from_memory(&bytes[..])?;
let image = picker.new_resize_protocol(dyn_image);

return Ok(SourceResponse::Captcha(image));
}
}

// Results table found, can start parsing
Expand Down
3 changes: 2 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ impl EventSync for AppSync {
res.clone(),
src.format_table(&res.items, &search, &config, &theme),
))),
// Ok(SourceResponse::Captcha(c)) => Ok(SourceResults::Captcha(c)),
#[cfg(feature = "captcha")]
Ok(SourceResponse::Captcha(c)) => Ok(SourceResults::Captcha(c)),
Err(e) => Err(e),
};
let _ = tx_res.send(fmt).await;
Expand Down
3 changes: 2 additions & 1 deletion src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use unicode_width::UnicodeWidthStr as _;
use crate::{app::Context, style, theme::Theme};

pub mod batch;
// pub mod captcha;
#[cfg(feature = "captcha")]
pub mod captcha;
pub mod category;
pub mod clients;
pub mod filter;
Expand Down

0 comments on commit 3645f92

Please sign in to comment.