From b9007713f593736e0f4ad0403e7fe9cff80387c3 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Fri, 3 Mar 2023 00:36:06 +0100 Subject: [PATCH] saves to downloadmode correctly --- src/config.rs | 11 +++++---- src/lib.rs | 2 +- src/m3u8.rs | 4 ++-- src/main.rs | 64 +++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 56 insertions(+), 25 deletions(-) diff --git a/src/config.rs b/src/config.rs index aa57f62..fd2f958 100644 --- a/src/config.rs +++ b/src/config.rs @@ -98,10 +98,11 @@ impl Configuration { let config_dir = project_dirs.config_dir(); let cache_dir = project_dirs.cache_dir().to_path_buf(); let data_dir = project_dirs.data_local_dir().to_path_buf(); - let _ = [&config_dir, &cache_dir.as_path(), &data_dir.as_path()] - .iter() - .filter(|x| !x.exists()) - .map(fs::create_dir_all); + for dir in [&config_dir, &cache_dir.as_path(), &data_dir.as_path()].iter() { + if !dir.exists() { + let _ = fs::create_dir_all(dir); + } + } // Config setup let config_file_path = config_dir.join(JSON_CONFIG_FILENAME).to_path_buf(); @@ -139,7 +140,7 @@ impl Configuration { } } - pub fn add_datafile_ugly(&self, data_entry: DataEntry) { + pub fn push_datafile_ugly(&self, data_entry: DataEntry) { unsafe { get_mut_ref(&self.datafile_content) }.push(data_entry); } diff --git a/src/lib.rs b/src/lib.rs index 8bbd1ed..80467b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,7 @@ mod m3u8; mod parser; use std::io::{stdin, stdout, Stdin, StdoutLock, Write}; -pub use m3u8::M3u8; +pub use m3u8::{DataEntry, M3u8}; pub use parser::Parser; mod config; pub use config::Configuration; diff --git a/src/m3u8.rs b/src/m3u8.rs index d9fa9ac..ab40a32 100644 --- a/src/m3u8.rs +++ b/src/m3u8.rs @@ -2,7 +2,7 @@ use colored::Colorize; use serde::{Deserialize, Serialize}; use std::{fmt::Display, ops::Deref}; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone, Hash)] pub struct M3u8 { pub tvg_id: String, pub tvg_name: String, @@ -25,7 +25,7 @@ impl Display for M3u8 { } } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone, Hash)] pub struct DataEntry { m3u8: M3u8, pub path: String, diff --git a/src/main.rs b/src/main.rs index 417db1c..98166b1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,9 @@ use std::process::Command; use std::rc::Rc; use colored::Colorize; -use ilovetv::{download_with_progress, get_mut_ref, Configuration, M3u8, Parser, Readline}; +use ilovetv::{ + download_with_progress, get_mut_ref, Configuration, DataEntry, M3u8, Parser, Readline, +}; #[tokio::main] async fn main() { @@ -125,9 +127,32 @@ async fn main() { } // Downloadmode "d" => { - let to_download = + let download_selections = ask_which_to_download(&mut readline, &search_result.as_ref().unwrap()); - download_m3u8(&to_download).await; + + for to_download in download_selections.iter() { + download_m3u8(to_download, None).await; + } + } + // Save to offlinemode + "o" => { + let download_selections = + ask_which_to_download(&mut readline, &search_result.as_ref().unwrap()); + + for to_download in download_selections.iter() { + let path = config.data_dir.join(&to_download.name); + let path = path.to_string_lossy().to_string(); + download_m3u8(to_download, Some(&path)).await; + let data_entry = DataEntry::new((*to_download).clone(), path); + config.push_datafile_ugly(data_entry); + } + + if let Err(e) = config.write_datafile() { + println!( + "Failed to information about downloaded entries for offline use {:?}", + e + ) + } } _ => {} } @@ -186,20 +211,25 @@ fn ask_which_to_download<'a>( ) } -async fn download_m3u8(files_to_download: &Vec<&M3u8>) { - 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); - } +async fn download_m3u8(file_to_download: &M3u8, path: Option<&str>) { + let file_ending_place = file_to_download.link.rfind(".").unwrap(); + let potential_file_ending = &file_to_download.link[file_ending_place..]; + let file_ending = if potential_file_ending.len() > 6 { + ".mkv" + } else { + potential_file_ending + }; + let file_name = format!("{}{}", file_to_download.name, file_ending); + println!("Downloading {}", &file_name); + let path = if let Some(path) = path { + format!("{}{}", path, file_ending) + } else { + file_name.clone() + }; + println!("{}", &path); + + if let Err(e) = download_with_progress(&file_to_download.link, Some(&path)).await { + eprintln!("Failed to download {}, {:?}", &file_name, e); } }