Functionality for switching to online from offline
This commit is contained in:
		| @@ -4,18 +4,18 @@ use crate::{ | |||||||
|     parser::{Parser, WatchedFind}, |     parser::{Parser, WatchedFind}, | ||||||
|     Configuration, OfflineParser, OnlineParser, Playlist, MAX_TRIES, |     Configuration, OfflineParser, OnlineParser, Playlist, MAX_TRIES, | ||||||
| }; | }; | ||||||
| use std::fs; | use std::{fs, rc::Rc}; | ||||||
|  |  | ||||||
| type Error = String; | type Error = String; | ||||||
|  |  | ||||||
| pub struct GrandMother { | pub struct GrandMother { | ||||||
|     pub parser: Box<dyn Parser>, |     pub parser: Box<dyn Parser>, | ||||||
|     pub playlist: Option<Playlist>, |     pub playlist: Option<Playlist>, | ||||||
|     pub config: Configuration, |     pub config: Rc<Configuration>, | ||||||
| } | } | ||||||
|  |  | ||||||
| impl GrandMother { | impl GrandMother { | ||||||
|     pub async fn new(config: Configuration) -> Result<Self, Error> { |     pub async fn new_online(config: Rc<Configuration>) -> Result<Self, Error> { | ||||||
|         let playlist = Playlist::new(config.playlist_path.clone(), config.playlist_url.clone()); |         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 seen_links = config.seen_links.iter().map(|x| x.as_str()).collect(); | ||||||
|         let playlist = playlist.await?; |         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<Configuration>) -> Self { | ||||||
|         let parser: Box<dyn Parser> = Box::new(OfflineParser::new(&config)); |         let parser: Box<dyn Parser> = Box::new(OfflineParser::new(&config)); | ||||||
|         Self { |         Self { | ||||||
|             parser, |             parser, | ||||||
|   | |||||||
							
								
								
									
										10
									
								
								src/lib.rs
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								src/lib.rs
									
									
									
									
									
								
							| @@ -1,24 +1,24 @@ | |||||||
| mod config; | mod config; | ||||||
| mod downloader; | mod downloader; | ||||||
| pub mod parser; |  | ||||||
| mod grandmother; | mod grandmother; | ||||||
| mod m3u8; | mod m3u8; | ||||||
| mod offlineparser; | mod offlineparser; | ||||||
| mod onlineparser; | mod onlineparser; | ||||||
| mod opt; | mod opt; | ||||||
|  | pub mod parser; | ||||||
| mod playlist; | 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; | use async_recursion::async_recursion; | ||||||
| pub use config::Configuration; | pub use config::Configuration; | ||||||
| pub use downloader::download_with_progress; | pub use downloader::download_with_progress; | ||||||
| pub use parser::{GetM3u8, GetPlayPath, WatchedFind}; |  | ||||||
| pub use grandmother::GrandMother; | pub use grandmother::GrandMother; | ||||||
| pub use m3u8::{M3u8, OfflineEntry}; | pub use m3u8::{M3u8, OfflineEntry}; | ||||||
| pub use offlineparser::OfflineParser; | pub use offlineparser::OfflineParser; | ||||||
| pub use onlineparser::OnlineParser; | pub use onlineparser::OnlineParser; | ||||||
| pub use opt::{Mode, Opt}; | pub use opt::{Mode, Opt}; | ||||||
|  | pub use parser::{GetM3u8, GetPlayPath, WatchedFind}; | ||||||
| pub use playlist::Playlist; | pub use playlist::Playlist; | ||||||
|  |  | ||||||
| pub const JSON_CONFIG_FILENAME: &'static str = "config.json"; | pub const JSON_CONFIG_FILENAME: &'static str = "config.json"; | ||||||
| @@ -64,10 +64,10 @@ pub unsafe fn get_mut_ref<T>(reference: &T) -> &mut T { | |||||||
| pub async fn get_gm( | pub async fn get_gm( | ||||||
|     mode: Mode, |     mode: Mode, | ||||||
|     readline: &mut Readline<'_>, |     readline: &mut Readline<'_>, | ||||||
|     config: Configuration, |     config: Rc<Configuration>, | ||||||
| ) -> Result<GrandMother, String> { | ) -> Result<GrandMother, String> { | ||||||
|     match mode { |     match mode { | ||||||
|         Mode::Online => GrandMother::new(config).await, |         Mode::Online => GrandMother::new_online(config).await, | ||||||
|         Mode::Offline => Ok(GrandMother::new_offline(config)), |         Mode::Offline => Ok(GrandMother::new_offline(config)), | ||||||
|         Mode::Ask => loop { |         Mode::Ask => loop { | ||||||
|             let input = readline |             let input = readline | ||||||
|   | |||||||
							
								
								
									
										26
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								src/main.rs
									
									
									
									
									
								
							| @@ -27,6 +27,11 @@ async fn main() { | |||||||
|         format!(" {} is to refresh the local iptvfile.", "r".bold()), |         format!(" {} is to refresh the local iptvfile.", "r".bold()), | ||||||
|         format!(" {} is to quit and save watched fields", "q".bold()), |         format!(" {} is to quit and save watched fields", "q".bold()), | ||||||
|         format!(" {} is to download fields", "d".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 perform a new search", "s".bold()), | ||||||
|         format!(" {} is to select all", "a".bold()), |         format!(" {} is to select all", "a".bold()), | ||||||
|         format!(" {} is to toggtle fullscreen for mpv", "f".bold()), |         format!(" {} is to toggtle fullscreen for mpv", "f".bold()), | ||||||
| @@ -50,7 +55,7 @@ async fn main() { | |||||||
|     let gm = get_gm( |     let gm = get_gm( | ||||||
|         opt.mode, |         opt.mode, | ||||||
|         &mut readline, |         &mut readline, | ||||||
|         Configuration::new().expect("Failed to write to configfile"), |         Rc::new(Configuration::new().expect("Failed to write to configfile")), | ||||||
|     ) |     ) | ||||||
|     .await |     .await | ||||||
|     .expect("Failed to retrive online playlist"); |     .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 |         // Dont't perform a search if user has just watched, instead present the previous search | ||||||
|         if search_result.is_none() { |         if search_result.is_none() { | ||||||
|             let search = readline |             let search = readline | ||||||
|                 .input("Search by name [ r/q/f/l ]: ") |                 .input("Search by name [ r/q/f/l/m ]: ") | ||||||
|                 .to_lowercase(); |                 .to_lowercase(); | ||||||
|             let mut search = search.trim(); |             let mut search = search.trim(); | ||||||
|  |  | ||||||
| @@ -99,6 +104,13 @@ async fn main() { | |||||||
|                     gm.config.update_last_search_ugly(None); |                     gm.config.update_last_search_ugly(None); | ||||||
|                     continue; |                     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))); |             search_result = Some(Rc::new(gm.parser.find(search))); | ||||||
| @@ -117,7 +129,7 @@ async fn main() { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         let user_wish = readline |         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(); |             .to_lowercase(); | ||||||
|         let user_wish = user_wish.trim(); |         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 { |     } else { | ||||||
|         file_name.clone() |         file_name.clone() | ||||||
|     }; |     }; | ||||||
|     println!("{}", &path); |  | ||||||
|  |  | ||||||
|     if let Err(e) = download_with_progress(&file_to_download.link, Some(&path)).await { |     if let Err(e) = download_with_progress(&file_to_download.link, Some(&path)).await { | ||||||
|         eprintln!("Failed to download {}, {:?}", &file_name, e); |         eprintln!("Failed to download {}, {:?}", &file_name, e); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user