saves to downloadmode correctly

This commit is contained in:
Love 2023-03-03 00:36:06 +01:00
parent 3c3acfe0ef
commit b9007713f5
4 changed files with 56 additions and 25 deletions

View File

@ -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);
}

View File

@ -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;

View File

@ -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,

View File

@ -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,21 +211,26 @@ 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..];
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!("{}{}", m3u8.name, file_ending);
let file_name = format!("{}{}", file_to_download.name, file_ending);
println!("Downloading {}", &file_name);
if let Err(e) = download_with_progress(&m3u8.link, Some(&file_name)).await {
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);
}
}
}
/**