-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: structure the project better (#87)
- Loading branch information
Showing
37 changed files
with
282 additions
and
312 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::{fs::File, io::Read}; | ||
|
||
use anyhow::Result; | ||
use base64::Engine; | ||
use transmission_rpc::types::TorrentAddArgs; | ||
|
||
use crate::transmission; | ||
|
||
pub(super) async fn add_torrent(torrent: String) -> Result<()> { | ||
let mut transclient = transmission::utils::new_client(); | ||
let args = { | ||
if torrent.starts_with("magnet:") | ||
|| torrent.starts_with("http:") | ||
|| torrent.starts_with("https:") | ||
{ | ||
TorrentAddArgs { | ||
filename: Some(torrent), | ||
..Default::default() | ||
} | ||
} else if torrent.starts_with("www") { | ||
TorrentAddArgs { | ||
filename: Some(format!("https://{torrent}")), | ||
..Default::default() | ||
} | ||
} else { | ||
let mut torrent_file = File::open(torrent)?; | ||
let mut buf = vec![]; | ||
torrent_file.read_to_end(&mut buf).unwrap(); | ||
let metainfo = base64::engine::general_purpose::STANDARD.encode(buf); | ||
TorrentAddArgs { | ||
metainfo: Some(metainfo), | ||
..Default::default() | ||
} | ||
} | ||
}; | ||
|
||
if let Err(e) = transclient.torrent_add(args).await { | ||
eprintln!("error while adding a torrent: {e}"); | ||
if e.to_string().contains("expected value at line") { | ||
eprintln!("Check whether your arguments are valid."); | ||
} | ||
|
||
std::process::exit(1); | ||
}; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use anyhow::{bail, Result}; | ||
use regex::Regex; | ||
use transmission_rpc::types::TorrentAddArgs; | ||
|
||
use crate::transmission; | ||
|
||
pub async fn fetch_rss(url: &str, filter: Option<&str>) -> Result<()> { | ||
let mut transclient = transmission::utils::new_client(); | ||
let content = reqwest::get(url).await?.bytes().await?; | ||
let channel = rss::Channel::read_from(&content[..])?; | ||
let re: Option<Regex> = { | ||
if let Some(filter_str) = filter { | ||
let res = Regex::new(filter_str)?; | ||
Some(res) | ||
} else { | ||
None | ||
} | ||
}; | ||
let items = channel.items().iter().filter_map(|item| { | ||
if let (Some(title), Some(url)) = (item.title(), item.link()) { | ||
if let Some(re) = &re { | ||
if re.is_match(title) { | ||
return Some((title, url)); | ||
} | ||
} else { | ||
return Some((title, url)); | ||
} | ||
} | ||
None | ||
}); | ||
for (title, url) in items { | ||
println!("downloading {title}"); | ||
let args = TorrentAddArgs { | ||
filename: Some(url.to_string()), | ||
..Default::default() | ||
}; | ||
if let Err(e) = transclient.torrent_add(args).await { | ||
bail!("error while adding a torrent: {e}") | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
mod add_torrent; | ||
mod fetch_rss; | ||
|
||
use anyhow::Result; | ||
use clap::{Parser, Subcommand}; | ||
|
||
use add_torrent::add_torrent; | ||
use fetch_rss::fetch_rss; | ||
|
||
#[derive(Parser)] | ||
#[command(version, about)] | ||
pub struct Args { | ||
#[command(subcommand)] | ||
pub command: Option<Commands>, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
pub enum Commands { | ||
AddTorrent { torrent: String }, | ||
FetchRss { url: String, filter: Option<String> }, | ||
} | ||
|
||
pub async fn handle_command(command: Commands) -> Result<()> { | ||
match command { | ||
Commands::AddTorrent { torrent } => add_torrent(torrent).await?, | ||
Commands::FetchRss { url, filter } => fetch_rss(&url, filter.as_deref()).await?, | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
rm-main/src/ui/components/mod.rs → rm-main/src/tui/components/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
3 changes: 2 additions & 1 deletion
3
rm-main/src/ui/components/tabs.rs → rm-main/src/tui/components/tabs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.