From 2c51df93cc58164c53eb0f05ca9981ec17ffd53b Mon Sep 17 00:00:00 2001 From: loveb Date: Thu, 19 Jan 2023 01:22:29 +0100 Subject: [PATCH] download episodes --- src/downloader.rs | 4 +-- src/lib.rs | 1 + src/main.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/downloader.rs b/src/downloader.rs index c77cf56..e5f2d76 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -46,7 +46,7 @@ impl DualWriter { } }) } - pub fn new(file_name: Option) -> Result { + pub fn new(file_name: Option<&str>) -> Result { Ok(if let Some(file_name) = file_name { Self::File(File::create(&file_name)?) } else { @@ -57,7 +57,7 @@ impl DualWriter { pub async fn download_with_progress( link: &str, - file_name: Option, + file_name: Option<&str>, ) -> Result { let mut dw = DualWriter::new(file_name).or(Err("Failed to create file".to_owned()))?; diff --git a/src/lib.rs b/src/lib.rs index c6aeae5..130f74b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ pub use parser::Parser; mod config; mod downloader; use directories::ProjectDirs; +pub use downloader::download_with_progress; pub fn setup() -> String { let project_dirs = ProjectDirs::from("com", "billenius", "iptvnator_rs").unwrap(); diff --git a/src/main.rs b/src/main.rs index 71028c3..5f14717 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ use std::io::{self, stdout, Write}; +use std::num::ParseIntError; use std::process::Command; use std::rc::Rc; -use iptvnator_rs::{setup, M3u8, Parser}; +use iptvnator_rs::{download_with_progress, setup, M3u8, Parser}; + #[tokio::main] async fn main() { println!("Welcome to iptvnator_rs, the port of my iptvprogram written in python, now in rust BLAZINGLY FAST\n"); @@ -40,7 +42,7 @@ async fn main() { for (idx, m3u8_item) in search_result.as_ref().unwrap().iter().enumerate().rev() { println!(" {}: {}", idx + 1, m3u8_item); } - print!("Which one do you wish to stream? [q | s | r]: "); + print!("Which one do you wish to stream? [ q/s/r/d ]: "); stdout.flush().unwrap(); buf = String::new(); stdin.read_line(&mut buf).unwrap(); @@ -64,6 +66,40 @@ async fn main() { p.forcefully_update().await; } continue; + } else if user_wish == "d" { + print!("Download all or select in comma separated [A]: "); + stdout.flush().unwrap(); + + let mut selection = String::new(); + stdin.read_line(&mut selection).unwrap(); + + let selection = selection.trim(); + let to_download = loop { + break if selection.to_lowercase() == "a" { + println!("Downloading all"); + search_result.as_ref().unwrap().clone() + } else { + let selections = selection + .split(",") + .map(|x| x.trim().parse::()) + .collect::>>(); + + for selection in selections.iter() { + if selection.is_err() { + println!("Not a valid number"); + continue; + } + } + let selections = selections.into_iter().map(|x| x.unwrap() - 1); + let mut final_selections = Vec::new(); + for selection in selections { + final_selections.push((search_result.as_ref().unwrap())[selection]); + } + + Rc::new(final_selections) + }; + }; + download_m3u8(to_download).await; } let choosen = user_wish.parse::(); @@ -79,6 +115,33 @@ async fn main() { parser.save_watched(); } +async fn download_m3u8(files_to_download: Rc>) { + for m3u8 in files_to_download.iter() { + let file_ending_place = m3u8.link.rfind(".").unwrap(); + let potential_file_ending = &m3u8.link[file_ending_place..]; + let file_ending = if potential_file_ending.len() > 6 { + ".mkv" + } else { + potential_file_ending + }; + let file_name = format!("{}{}", m3u8.name, file_ending); + println!("Downloading {}", &file_name); + if let Err(e) = download_with_progress(&m3u8.link, Some(&file_name)).await { + eprintln!("Failed to download {}, {:?}", &file_name, e); + } + } +} + +#[tokio::test] +async fn t() { + let link = "http://clientsportals.com:2095/series/fW6Mue7z/aTGX3xaM/21179.mkv"; + let dot = link.rfind(".").unwrap(); + let last = &link[dot..link.len()]; + + println!("{}", last); + println!("{}", last.len()); +} + fn stream(m3u8item: &M3u8) { // Well I know that this is frowned upon, but it's honestly the most efficient way of doing this let ptr = m3u8item as *const M3u8;