Save watched right after something is watched, and use match

This commit is contained in:
Love 2023-02-27 16:24:16 +01:00
parent f69a040287
commit 7d6aa90dad
5 changed files with 69 additions and 93 deletions

29
Cargo.lock generated
View File

@ -123,16 +123,6 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "ctrlc"
version = "3.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1631ca6e3c59112501a9d87fd86f21591ff77acd31331e8a73f8d80a65bbdd71"
dependencies = [
"nix",
"windows-sys",
]
[[package]] [[package]]
name = "directories" name = "directories"
version = "4.0.1" version = "4.0.1"
@ -470,7 +460,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bytes", "bytes",
"colored", "colored",
"ctrlc",
"directories", "directories",
"futures-util", "futures-util",
"indicatif", "indicatif",
@ -576,18 +565,6 @@ dependencies = [
"tempfile", "tempfile",
] ]
[[package]]
name = "nix"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a"
dependencies = [
"bitflags",
"cfg-if",
"libc",
"static_assertions",
]
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "1.15.0" version = "1.15.0"
@ -959,12 +936,6 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.107" version = "1.0.107"

View File

@ -8,7 +8,6 @@ edition = "2021"
[dependencies] [dependencies]
bytes = "1.3.0" bytes = "1.3.0"
colored = "2.0.0" colored = "2.0.0"
ctrlc = "3.2.4"
directories = "4.0.1" directories = "4.0.1"
futures-util = "0.3.25" futures-util = "0.3.25"
indicatif = { version = "0.17.3", features = ["tokio"] } indicatif = { version = "0.17.3", features = ["tokio"] }

View File

@ -9,6 +9,5 @@ You will need to install mpv, and have it in your path, otherwise it wont work
## Left to do ## Left to do
- Implement the ctrlc handler so that the program saves watched links before exiting.
- Create a GUI - Create a GUI
- Would be nice to bundle mpv in some form - Would be nice to bundle mpv in some form

View File

@ -29,22 +29,27 @@ async fn main() {
let search = readline.input("Search by name [ r/q/f ]: ").to_lowercase(); let search = readline.input("Search by name [ r/q/f ]: ").to_lowercase();
let search = search.trim(); let search = search.trim();
// If they want to quit, let them- // Special commands
if search == "q" { match search {
break; // Quit
} else if search == "r" { "q" => break,
search_result = None; // Refresh playlist
refresh(&parser).await; "r" => {
continue; search_result = None;
} else if search == "f" { refresh(&parser).await;
mpv_fs = !mpv_fs; continue;
println!( }
"Toggled mpv to {}launch in fullscreen", // Toggle fullscreen for mpv
if mpv_fs { "" } else { "not " } "f" => {
); mpv_fs = !mpv_fs;
continue; println!(
"Toggled mpv to {}launch in fullscreen",
if mpv_fs { "" } else { "not " }
);
continue;
}
_ => {}
} }
search_result = Some(Rc::new(parser.find(search))); search_result = Some(Rc::new(parser.find(search)));
if search_result.as_ref().unwrap().len() == 0 { if search_result.as_ref().unwrap().len() == 0 {
@ -64,61 +69,68 @@ async fn main() {
let user_wish = user_wish.trim(); let user_wish = user_wish.trim();
// If they want to quit, let them- // If they want to quit, let them-
if user_wish == "q" { match user_wish {
break; // Quit
} else if user_wish == "s" { "q" => break,
search_result = None; // Go inte search-mode
continue; "s" => {
} else if user_wish == "r" { search_result = None;
println!("Refreshing local m3u8-file"); continue;
search_result = None; }
refresh(&parser).await; // Refresh playlist
continue; "r" => {
} else if user_wish == "d" { println!("Refreshing local m3u8-file");
let selection = readline search_result = None;
.input("Download all or select in comma separated [a | 1,2,3,4]: ") refresh(&parser).await;
.to_lowercase(); continue;
let selection = selection.trim(); }
// Downloadmode
"d" => {
let selection = readline
.input("Download all or select in comma separated [a | 1,2,3,4]: ")
.to_lowercase();
let selection = selection.trim();
let to_download = loop { let to_download = loop {
break if selection == "a" { break if selection == "a" {
println!("Downloading all"); println!("Downloading all");
search_result.as_ref().unwrap().clone() search_result.as_ref().unwrap().clone()
} else { } else {
let selections = selection let selections = selection
.split(",") .split(",")
.map(|x| x.trim().parse::<usize>()) .map(|x| x.trim().parse::<usize>())
.collect::<Vec<Result<usize, ParseIntError>>>(); .collect::<Vec<Result<usize, ParseIntError>>>();
for selection in selections.iter() { for selection in selections.iter() {
if selection.is_err() { if selection.is_err() {
println!("Not a valid number or the option {}", "a".bold()); println!("Not a valid number or the option {}", "a".bold());
continue; 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]);
} }
}
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) Rc::new(final_selections)
};
}; };
}; download_m3u8(to_download).await;
download_m3u8(to_download).await; }
_ => {}
} }
let choosen = user_wish.parse::<usize>(); let choosen = user_wish.parse::<usize>();
match choosen { match choosen {
Ok(k) => { Ok(k) => {
let search_result = search_result.as_ref().unwrap().clone(); let search_result = search_result.as_ref().unwrap().clone();
stream(&(search_result[k - 1]), mpv_fs) stream(&(search_result[k - 1]), mpv_fs);
parser.save_watched();
} }
Err(e) => println!("Have to be a valid number! {:?}", e), Err(e) => println!("Have to be a valid number! {:?}", e),
} }
} }
parser.save_watched();
} }
/* /*

View File

@ -90,13 +90,8 @@ impl Parser {
let _ = fs::create_dir_all(&*self.watched_name.parent().unwrap()); let _ = fs::create_dir_all(&*self.watched_name.parent().unwrap());
match fs::write(&*self.watched_name, watched_items.join("\n")) { if let Err(e) = fs::write(&*self.watched_name, watched_items.join("\n")) {
Ok(_) => { eprintln!("Failed to write watched links {:?}", e);
println!("Saved watched")
}
Err(e) => {
eprintln!("Failed to write downloaded m3u8file {:?}", e);
}
} }
} }