Add name and peers to magnet links

Add additional data to magnet links in `imdl torrent link`.

- Set the `dn` query parameter with `metinfo.info.name`
- Add peers to the `x.pe` query parameter:

    imdl torrent link --peer HOST:PORT

type: added
This commit is contained in:
Casey Rodarmor 2020-03-17 03:49:39 -07:00
parent 57a358e458
commit aeb9d8b317
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0

View File

@ -15,6 +15,13 @@ pub(crate) struct Link {
parse(from_os_str)
)]
input: PathBuf,
#[structopt(
long = "peer",
short = "p",
value_name = "PEER",
help = "Add `PEER` to magnet link."
)]
peers: Vec<HostPort>,
}
impl Link {
@ -25,6 +32,8 @@ impl Link {
let mut link = MagnetLink::with_infohash(infohash);
link.set_name(&metainfo.info.name);
let mut trackers = HashSet::new();
for result in metainfo.trackers() {
let tracker = result?;
@ -34,6 +43,10 @@ impl Link {
}
}
for peer in self.peers {
link.add_peer(peer);
}
outln!(env, "{}", link.to_url())?;
Ok(())
@ -67,7 +80,10 @@ mod tests {
let infohash = Sha1Digest::from_data(INFO.as_bytes());
assert_eq!(env.out(), format!("magnet:?xt=urn:btih:{}\n", infohash),);
assert_eq!(
env.out(),
format!("magnet:?xt=urn:btih:{}&dn=foo\n", infohash),
);
}
#[test]
@ -96,7 +112,73 @@ mod tests {
assert_eq!(
env.out(),
format!(
"magnet:?xt=urn:btih:{}&tr=https://foo.com/announce\n",
"magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce\n",
infohash
),
);
}
#[test]
fn unique_trackers() {
let mut env = test_env! {
args: [
"torrent",
"link",
"--input",
"foo.torrent",
],
tree: {
"foo.torrent": "d\
8:announce24:https://foo.com/announce\
13:announce-listll24:https://foo.com/announceel24:https://bar.com/announceee\
4:infod6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e\
e",
}
};
assert_ok!(env.run());
const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e";
let infohash = Sha1Digest::from_data(INFO.as_bytes());
assert_eq!(
env.out(),
format!(
"magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce&tr=https://bar.com/announce\n",
infohash
),
);
}
#[test]
fn with_peer() {
let mut env = test_env! {
args: [
"torrent",
"link",
"--input",
"foo.torrent",
"--peer",
"foo.com:1337",
],
tree: {
"foo.torrent": "d\
8:announce24:https://foo.com/announce\
4:infod6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e\
e",
}
};
assert_ok!(env.run());
const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e";
let infohash = Sha1Digest::from_data(INFO.as_bytes());
assert_eq!(
env.out(),
format!(
"magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce&x.pe=foo.com:1337\n",
infohash
),
);
@ -122,7 +204,10 @@ mod tests {
let infohash = Sha1Digest::from_data(INFO.as_bytes());
assert_eq!(env.out(), format!("magnet:?xt=urn:btih:{}\n", infohash),);
assert_eq!(
env.out(),
format!("magnet:?xt=urn:btih:{}&dn=foo\n", infohash),
);
}
#[test]