From bf751c1cee9cb183531d267e240a5023f321f443 Mon Sep 17 00:00:00 2001 From: lov3b Date: Sun, 5 Mar 2023 20:26:52 +0100 Subject: [PATCH] Functionality for switching to online from offline --- src/grandmother.rs | 15 +++++++++++---- src/lib.rs | 10 +++++----- src/main.rs | 26 ++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/grandmother.rs b/src/grandmother.rs index 3dc824c..ea04f67 100644 --- a/src/grandmother.rs +++ b/src/grandmother.rs @@ -4,18 +4,18 @@ use crate::{ parser::{Parser, WatchedFind}, Configuration, OfflineParser, OnlineParser, Playlist, MAX_TRIES, }; -use std::fs; +use std::{fs, rc::Rc}; type Error = String; pub struct GrandMother { pub parser: Box, pub playlist: Option, - pub config: Configuration, + pub config: Rc, } impl GrandMother { - pub async fn new(config: Configuration) -> Result { + pub async fn new_online(config: Rc) -> Result { let playlist = Playlist::new(config.playlist_path.clone(), config.playlist_url.clone()); let seen_links = config.seen_links.iter().map(|x| x.as_str()).collect(); let playlist = playlist.await?; @@ -30,7 +30,14 @@ impl GrandMother { }) } - pub fn new_offline(config: Configuration) -> Self { + pub async fn promote_to_online(&mut self) -> Result<(), Error> { + let online_mother = GrandMother::new_online(self.config.clone()).await?; + (self.parser, self.playlist) = (online_mother.parser, online_mother.playlist); + + Ok(()) + } + + pub fn new_offline(config: Rc) -> Self { let parser: Box = Box::new(OfflineParser::new(&config)); Self { parser, diff --git a/src/lib.rs b/src/lib.rs index da785fa..4a74237 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,24 +1,24 @@ mod config; mod downloader; -pub mod parser; mod grandmother; mod m3u8; mod offlineparser; mod onlineparser; mod opt; +pub mod parser; mod playlist; -use std::io::{stdin, stdout, Stdin, StdoutLock, Write}; +use std::{io::{stdin, stdout, Stdin, StdoutLock, Write}, rc::Rc}; use async_recursion::async_recursion; pub use config::Configuration; pub use downloader::download_with_progress; -pub use parser::{GetM3u8, GetPlayPath, WatchedFind}; pub use grandmother::GrandMother; pub use m3u8::{M3u8, OfflineEntry}; pub use offlineparser::OfflineParser; pub use onlineparser::OnlineParser; pub use opt::{Mode, Opt}; +pub use parser::{GetM3u8, GetPlayPath, WatchedFind}; pub use playlist::Playlist; pub const JSON_CONFIG_FILENAME: &'static str = "config.json"; @@ -64,10 +64,10 @@ pub unsafe fn get_mut_ref(reference: &T) -> &mut T { pub async fn get_gm( mode: Mode, readline: &mut Readline<'_>, - config: Configuration, + config: Rc, ) -> Result { match mode { - Mode::Online => GrandMother::new(config).await, + Mode::Online => GrandMother::new_online(config).await, Mode::Offline => Ok(GrandMother::new_offline(config)), Mode::Ask => loop { let input = readline diff --git a/src/main.rs b/src/main.rs index d7961c6..2c517b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,11 @@ async fn main() { format!(" {} is to refresh the local iptvfile.", "r".bold()), format!(" {} is to quit and save watched fields", "q".bold()), format!(" {} is to download fields", "d".bold()), + format!( + " {} is to make entries availibe for offline use later on in the program", + "o".bold() + ), + format!(" {} is to switch to online mode", "m".bold()), format!(" {} is to perform a new search", "s".bold()), format!(" {} is to select all", "a".bold()), format!(" {} is to toggtle fullscreen for mpv", "f".bold()), @@ -50,7 +55,7 @@ async fn main() { let gm = get_gm( opt.mode, &mut readline, - Configuration::new().expect("Failed to write to configfile"), + Rc::new(Configuration::new().expect("Failed to write to configfile")), ) .await .expect("Failed to retrive online playlist"); @@ -59,7 +64,7 @@ async fn main() { // Dont't perform a search if user has just watched, instead present the previous search if search_result.is_none() { let search = readline - .input("Search by name [ r/q/f/l ]: ") + .input("Search by name [ r/q/f/l/m ]: ") .to_lowercase(); let mut search = search.trim(); @@ -99,6 +104,13 @@ async fn main() { gm.config.update_last_search_ugly(None); continue; } + "m" => { + let result = unsafe { get_mut_ref(&gm) }.promote_to_online().await; + if let Err(e) = result { + println!("Failed to switch to onlinemode {:?}", e); + } + continue; + } _ => {} } search_result = Some(Rc::new(gm.parser.find(search))); @@ -117,7 +129,7 @@ async fn main() { } let user_wish = readline - .input("Which one do you wish to stream? [ q/f/s/r/d ]: ") + .input("Which one do you wish to stream? [ q/f/s/r/d/o/m ]: ") .to_lowercase(); let user_wish = user_wish.trim(); @@ -179,6 +191,13 @@ async fn main() { ) } } + "m" => { + let result = unsafe { get_mut_ref(&gm) }.promote_to_online().await; + if let Err(e) = result { + println!("Failed to switch to onlinemode {:?}", e); + } + continue; + } _ => {} } @@ -259,7 +278,6 @@ async fn download_m3u8(file_to_download: &M3u8, path: Option<&str>) { } 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);