Skip to content

Commit 310ea71

Browse files
author
slundi
committed
feat: yank magnet without metadata and trackers
Ref: #37
1 parent c496c50 commit 310ea71

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

Cargo.lock

Lines changed: 17 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ base64 = { version = "0.22.1", default-features = false, features = ["alloc"] }
4646
lexopt = "0.3.0"
4747
ratatui-image = { version = "1.0.5", optional = true , default-features = false }
4848
image = { version = "0.25.1", optional = true, features = ["png"], default-features = false }
49+
url = "2.5.4"
4950

5051
[lib]
5152
name = "nyaa"

docs/keybinds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This mode is entered when the results table is focused.
1818
| P, H | First Page |
1919
| r | Reload |
2020
| o | Open in browser |
21-
| yt, ym, yp, yi, yn | Copy torrent/magnet/post link/imdb id/name |
21+
| yt, ym, yM, yp, yi, yn | Copy torrent/magnet/magnet without metadata and tracker/post link/imdb id/name |
2222
| Space | Toggle item for batch download |
2323
| v/V/Ctrl-Space | Enter visual add/remove/toggle mode |
2424
| Tab/Shift-Tab | Switch to Batches|

src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ impl App {
654654
let link = match c {
655655
't' => item.torrent_link,
656656
'm' => item.magnet_link,
657+
'M' => item.minimal_magnet_link(),
657658
'p' => item.post_link,
658659
'i' => match item.extra.get("imdb").cloned() {
659660
Some(imdb) => imdb,

src/source.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
66
use strum::{Display, VariantArray};
77
use sukebei_nyaa::SukebeiTheme;
88
use torrent_galaxy::TgxTheme;
9+
use url::Url;
910

1011
use crate::{
1112
app::{Context, LoadType, Widgets},
@@ -173,6 +174,22 @@ pub struct Item {
173174
pub extra: HashMap<String, String>,
174175
}
175176

177+
impl Item {
178+
pub fn minimal_magnet_link(self) -> String {
179+
let url = Url::parse(&self.magnet_link).ok().unwrap();
180+
181+
// Extract the query parameters into a HashMap.
182+
let query_pairs = url.query_pairs();
183+
let params: HashMap<_, _> = query_pairs.into_owned().collect();
184+
185+
let xt = params.get("xt").unwrap();
186+
let mut magnet = "magnet:?".to_string();
187+
magnet.push_str("xt=");
188+
magnet.push_str(xt);
189+
magnet
190+
}
191+
}
192+
176193
#[derive(Serialize, Deserialize, Display, Clone, Copy, VariantArray, PartialEq, Eq)]
177194
pub enum Sources {
178195
#[strum(serialize = "Nyaa")]
@@ -379,3 +396,15 @@ impl Sources {
379396
}
380397
}
381398
}
399+
400+
#[cfg(test)]
401+
mod tests {
402+
#[test]
403+
fn test_minimal_magnet_link() {
404+
let item = crate::source::Item {magnet_link: "magnet:?xt=urn:btih:691526c892951e9b41b7946524513f945e5c7c45&dn=Example.File.Name&tr=http://example.com/tracker/announce".to_owned(), ..Default::default()};
405+
assert_eq!(
406+
&item.minimal_magnet_link(),
407+
"magnet:?xt=urn:btih:691526c892951e9b41b7946524513f945e5c7c45"
408+
);
409+
}
410+
}

src/widget/results.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ impl super::Widget for ResultsWidget {
431431
("r", "Reload"),
432432
("o", "Open in browser"),
433433
(
434-
"yt, ym, yp, yi, yn",
435-
"Copy torrent/magnet/post link/imdb id/name",
434+
"yt, ym, yM, yp, yi, yn",
435+
"Copy torrent/magnet/magnet without tracker and metadata/post link/imdb id/name",
436436
),
437437
("Space", "Toggle item for batch download"),
438438
("v/V/Ctrl-Space", "Enter visual add/remove/toggle mode"),

0 commit comments

Comments
 (0)