mirror of
https://github.com/YGGverse/btracker.git
synced 2026-03-31 17:15:31 +00:00
implement magnet links
This commit is contained in:
parent
ec6d9a4e00
commit
7da285ca69
5 changed files with 58 additions and 31 deletions
24
src/feed.rs
24
src/feed.rs
|
|
@ -7,8 +7,7 @@ pub struct Feed {
|
|||
description: Option<String>,
|
||||
link: Option<String>,
|
||||
title: String,
|
||||
/// Valid, parsed from Url, ready-to-use address string donor
|
||||
trackers: Option<HashSet<String>>,
|
||||
trackers: Option<HashSet<Url>>,
|
||||
}
|
||||
|
||||
impl Feed {
|
||||
|
|
@ -22,7 +21,7 @@ impl Feed {
|
|||
description: description.map(escape),
|
||||
link: link.map(|s| escape(s.to_string())),
|
||||
title: escape(title),
|
||||
trackers: trackers.map(|v| v.into_iter().map(|u| u.to_string()).collect()),
|
||||
trackers,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +69,7 @@ impl Feed {
|
|||
.map(|b| b.to_string())
|
||||
.unwrap_or("?".into()) // @TODO
|
||||
),
|
||||
escape(self.magnet(&torrent.info_hash))
|
||||
escape(format::magnet(&torrent.info_hash, self.trackers.as_ref()))
|
||||
));
|
||||
|
||||
if let Some(d) = item_description(torrent.length, torrent.files) {
|
||||
|
|
@ -91,23 +90,6 @@ impl Feed {
|
|||
buffer.push_str("</channel></rss>");
|
||||
buffer
|
||||
}
|
||||
|
||||
// Tools
|
||||
|
||||
fn magnet(&self, info_hash: &str) -> String {
|
||||
let mut b = if info_hash.len() == 40 {
|
||||
format!("magnet:?xt=urn:btih:{info_hash}")
|
||||
} else {
|
||||
todo!("info-hash v2 is not supported by librqbit")
|
||||
};
|
||||
if let Some(ref trackers) = self.trackers {
|
||||
for tracker in trackers {
|
||||
b.push_str("&tr=");
|
||||
b.push_str(&urlencoding::encode(tracker))
|
||||
}
|
||||
}
|
||||
b
|
||||
}
|
||||
}
|
||||
|
||||
fn escape(subject: String) -> String {
|
||||
|
|
|
|||
|
|
@ -15,3 +15,18 @@ pub fn bytes(value: u64) -> String {
|
|||
format!("{:.2} GB", f / GB)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn magnet(info_hash: &str, trackers: Option<&std::collections::HashSet<url::Url>>) -> String {
|
||||
let mut b = if info_hash.len() == 40 {
|
||||
format!("magnet:?xt=urn:btih:{info_hash}")
|
||||
} else {
|
||||
todo!("info-hash v2 is not supported by librqbit")
|
||||
};
|
||||
if let Some(t) = trackers {
|
||||
for tracker in t {
|
||||
b.push_str("&tr=");
|
||||
b.push_str(&urlencoding::encode(tracker.as_str()))
|
||||
}
|
||||
}
|
||||
b
|
||||
}
|
||||
|
|
|
|||
23
src/main.rs
23
src/main.rs
|
|
@ -15,7 +15,8 @@ use rocket::{
|
|||
serde::Serialize,
|
||||
};
|
||||
use rocket_dyn_templates::{Template, context};
|
||||
use storage::{Order, Sort, Storage};
|
||||
use std::collections::HashSet;
|
||||
use storage::{Order, Sort, Storage, Torrent};
|
||||
use url::Url;
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
|
|
@ -25,21 +26,33 @@ pub struct Meta {
|
|||
pub description: Option<String>,
|
||||
pub stats: Option<Url>,
|
||||
pub title: String,
|
||||
pub trackers: Option<Vec<Url>>,
|
||||
pub trackers: Option<HashSet<Url>>,
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index(storage: &State<Storage>, meta: &State<Meta>) -> Result<Template, Custom<String>> {
|
||||
#[derive(Serialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
struct Row {
|
||||
torrent: Torrent,
|
||||
magnet: String,
|
||||
}
|
||||
Ok(Template::render(
|
||||
"index",
|
||||
context! {
|
||||
meta: meta.inner(),
|
||||
torrents: storage
|
||||
rows: storage
|
||||
.torrents(
|
||||
Some((Sort::Modified, Order::Asc)),
|
||||
Some(storage.default_limit),
|
||||
)
|
||||
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?
|
||||
.into_iter()
|
||||
.map(|torrent| Row {
|
||||
magnet: format::magnet(&torrent.info_hash, meta.trackers.as_ref()),
|
||||
torrent,
|
||||
})
|
||||
.collect::<Vec<Row>>()
|
||||
},
|
||||
))
|
||||
}
|
||||
|
|
@ -67,7 +80,7 @@ fn rocket() -> _ {
|
|||
config.title.clone(),
|
||||
config.description.clone(),
|
||||
config.link.clone(),
|
||||
config.tracker.clone().map(|u| u.into_iter().collect()), // make sure it's unique
|
||||
config.tracker.clone().map(|u| u.into_iter().collect()),
|
||||
);
|
||||
let storage = Storage::init(config.storage, config.limit, config.capacity).unwrap(); // @TODO handle
|
||||
rocket::build()
|
||||
|
|
@ -84,7 +97,7 @@ fn rocket() -> _ {
|
|||
description: config.description,
|
||||
stats: config.stats,
|
||||
title: config.title,
|
||||
trackers: config.tracker,
|
||||
trackers: config.tracker.map(|u| u.into_iter().collect()),
|
||||
})
|
||||
.mount("/", routes![index, rss])
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue