Functionality for switching to online from offline

This commit is contained in:
Love 2023-03-05 20:26:52 +01:00
parent 6da49ec983
commit 37bc5b11c7
No known key found for this signature in database
GPG Key ID: A3C10DC241C8FA9F
3 changed files with 38 additions and 13 deletions

View File

@ -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<dyn Parser>,
pub playlist: Option<Playlist>,
pub config: Configuration,
pub config: Rc<Configuration>,
}
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 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<Configuration>) -> Self {
let parser: Box<dyn Parser> = Box::new(OfflineParser::new(&config));
Self {
parser,

View File

@ -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<T>(reference: &T) -> &mut T {
pub async fn get_gm(
mode: Mode,
readline: &mut Readline<'_>,
config: Configuration,
config: Rc<Configuration>,
) -> Result<GrandMother, String> {
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

View File

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