More refactors
This commit is contained in:
		@@ -28,5 +28,5 @@ pub trait GetPlayPath {
 | 
				
			|||||||
    fn get_path_to_play(&self, link: Rc<String>) -> Result<Rc<String>, String>;
 | 
					    fn get_path_to_play(&self, link: Rc<String>) -> Result<Rc<String>, String>;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub trait M3u8PlayPath: GetM3u8 + GetPlayPath {}
 | 
					pub trait Parser: GetM3u8 + GetPlayPath {}
 | 
				
			||||||
impl<T: GetM3u8 + GetPlayPath> M3u8PlayPath for T {}
 | 
					impl<T: GetM3u8 + GetPlayPath> Parser for T {}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,15 +1,15 @@
 | 
				
			|||||||
#[allow(unused_imports)]
 | 
					#[allow(unused_imports)]
 | 
				
			||||||
use crate::GetM3u8;
 | 
					use crate::GetM3u8;
 | 
				
			||||||
use crate::{
 | 
					use crate::{
 | 
				
			||||||
    getm3u8::{M3u8PlayPath, WatchedFind},
 | 
					    getm3u8::{Parser, WatchedFind},
 | 
				
			||||||
    Configuration, OfflineParser, Parser, Playlist, MAX_TRIES,
 | 
					    Configuration, OfflineParser, OnlineParser, Playlist, MAX_TRIES,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use std::fs;
 | 
					use std::fs;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Error = String;
 | 
					type Error = String;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct GrandMother {
 | 
					pub struct GrandMother {
 | 
				
			||||||
    pub parser: Box<dyn M3u8PlayPath>,
 | 
					    pub parser: Box<dyn Parser>,
 | 
				
			||||||
    pub playlist: Option<Playlist>,
 | 
					    pub playlist: Option<Playlist>,
 | 
				
			||||||
    pub config: Configuration,
 | 
					    pub config: Configuration,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -20,8 +20,8 @@ impl GrandMother {
 | 
				
			|||||||
        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?;
 | 
				
			||||||
        let playlist_content = playlist.get_saved_or_download().await?;
 | 
					        let playlist_content = playlist.get_saved_or_download().await?;
 | 
				
			||||||
        let parser: Box<dyn M3u8PlayPath> =
 | 
					        let parser: Box<dyn Parser> =
 | 
				
			||||||
            Box::new(Parser::new(&playlist_content, &seen_links).await);
 | 
					            Box::new(OnlineParser::new(&playlist_content, &seen_links).await);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(Self {
 | 
					        Ok(Self {
 | 
				
			||||||
            parser,
 | 
					            parser,
 | 
				
			||||||
@@ -31,7 +31,7 @@ impl GrandMother {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn new_offline(config: Configuration) -> Self {
 | 
					    pub fn new_offline(config: Configuration) -> Self {
 | 
				
			||||||
        let parser: Box<dyn M3u8PlayPath> = Box::new(OfflineParser::new(&config));
 | 
					        let parser: Box<dyn Parser> = Box::new(OfflineParser::new(&config));
 | 
				
			||||||
        Self {
 | 
					        Self {
 | 
				
			||||||
            parser,
 | 
					            parser,
 | 
				
			||||||
            playlist: None,
 | 
					            playlist: None,
 | 
				
			||||||
@@ -69,7 +69,7 @@ impl GrandMother {
 | 
				
			|||||||
            .iter()
 | 
					            .iter()
 | 
				
			||||||
            .map(|x| x.link.as_str())
 | 
					            .map(|x| x.link.as_str())
 | 
				
			||||||
            .collect();
 | 
					            .collect();
 | 
				
			||||||
        self.parser = Box::new(Parser::new(&content, &watched_links).await);
 | 
					        self.parser = Box::new(OnlineParser::new(&content, &watched_links).await);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,8 +3,9 @@ mod downloader;
 | 
				
			|||||||
pub mod getm3u8;
 | 
					pub mod getm3u8;
 | 
				
			||||||
mod grandmother;
 | 
					mod grandmother;
 | 
				
			||||||
mod m3u8;
 | 
					mod m3u8;
 | 
				
			||||||
 | 
					mod offlineparser;
 | 
				
			||||||
 | 
					mod onlineparser;
 | 
				
			||||||
mod opt;
 | 
					mod opt;
 | 
				
			||||||
mod parser;
 | 
					 | 
				
			||||||
mod playlist;
 | 
					mod playlist;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use std::io::{stdin, stdout, Stdin, StdoutLock, Write};
 | 
					use std::io::{stdin, stdout, Stdin, StdoutLock, Write};
 | 
				
			||||||
@@ -15,8 +16,9 @@ pub use downloader::download_with_progress;
 | 
				
			|||||||
pub use getm3u8::{GetM3u8, GetPlayPath, WatchedFind};
 | 
					pub use getm3u8::{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 onlineparser::OnlineParser;
 | 
				
			||||||
pub use opt::{Mode, Opt};
 | 
					pub use opt::{Mode, Opt};
 | 
				
			||||||
pub use parser::{OfflineParser, Parser};
 | 
					 | 
				
			||||||
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";
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										42
									
								
								src/offlineparser.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/offlineparser.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
				
			|||||||
 | 
					use std::{ops::Deref, rc::Rc};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use serde::Serialize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use crate::{m3u8::M3u8, Configuration, GetM3u8, GetPlayPath, OfflineEntry};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Serialize)]
 | 
				
			||||||
 | 
					pub struct OfflineParser {
 | 
				
			||||||
 | 
					    m3u8_items: Rc<Vec<OfflineEntry>>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					impl OfflineParser {
 | 
				
			||||||
 | 
					    pub fn new(config: &Configuration) -> Self {
 | 
				
			||||||
 | 
					        Self {
 | 
				
			||||||
 | 
					            m3u8_items: config.offlinefile_content.clone(),
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Deref for OfflineParser {
 | 
				
			||||||
 | 
					    type Target = Vec<OfflineEntry>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn deref(&self) -> &Self::Target {
 | 
				
			||||||
 | 
					        &*self.m3u8_items
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl GetPlayPath for OfflineParser {
 | 
				
			||||||
 | 
					    fn get_path_to_play(&self, link: Rc<String>) -> Result<Rc<String>, String> {
 | 
				
			||||||
 | 
					        for offline_entry in &*self.m3u8_items {
 | 
				
			||||||
 | 
					            if *offline_entry.link == *link {
 | 
				
			||||||
 | 
					                return Ok(offline_entry.path.clone());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Err("Not stored for offline use".to_owned())
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl GetM3u8 for OfflineParser {
 | 
				
			||||||
 | 
					    fn get_m3u8(&self) -> Vec<&M3u8> {
 | 
				
			||||||
 | 
					        self.m3u8_items.iter().map(|x| &**x).collect()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -4,11 +4,11 @@ use serde::Serialize;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use crate::{m3u8::M3u8, Configuration, GetM3u8, GetPlayPath, OfflineEntry};
 | 
					use crate::{m3u8::M3u8, Configuration, GetM3u8, GetPlayPath, OfflineEntry};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct Parser {
 | 
					pub struct OnlineParser {
 | 
				
			||||||
    m3u8_items: Vec<M3u8>,
 | 
					    m3u8_items: Vec<M3u8>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Parser {
 | 
					impl OnlineParser {
 | 
				
			||||||
    pub async fn new(m3u_content: &str, watched_links: &Vec<&str>) -> Self {
 | 
					    pub async fn new(m3u_content: &str, watched_links: &Vec<&str>) -> Self {
 | 
				
			||||||
        Self {
 | 
					        Self {
 | 
				
			||||||
            m3u8_items: Self::parse_m3u8(m3u_content, watched_links),
 | 
					            m3u8_items: Self::parse_m3u8(m3u_content, watched_links),
 | 
				
			||||||
@@ -23,14 +23,6 @@ impl Parser {
 | 
				
			|||||||
            .collect()
 | 
					            .collect()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /*
 | 
					 | 
				
			||||||
     * I know that this is also frowned upon, but it is perfectly safe right here,
 | 
					 | 
				
			||||||
     * even though the borrowchecker complains
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    // async fn refresh(&self) {
 | 
					 | 
				
			||||||
    //     unsafe { get_mut_ref(&self.pla) }.forcefully_update().await;
 | 
					 | 
				
			||||||
    // }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub async fn forcefully_update(&mut self, content: &str) {
 | 
					    pub async fn forcefully_update(&mut self, content: &str) {
 | 
				
			||||||
        let seen_links: &Vec<&str> = &self
 | 
					        let seen_links: &Vec<&str> = &self
 | 
				
			||||||
            .m3u8_items
 | 
					            .m3u8_items
 | 
				
			||||||
@@ -80,7 +72,7 @@ impl Parser {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Deref for Parser {
 | 
					impl Deref for OnlineParser {
 | 
				
			||||||
    type Target = Vec<M3u8>;
 | 
					    type Target = Vec<M3u8>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn deref(&self) -> &Self::Target {
 | 
					    fn deref(&self) -> &Self::Target {
 | 
				
			||||||
@@ -88,42 +80,14 @@ impl Deref for Parser {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl GetM3u8 for Parser {
 | 
					impl GetM3u8 for OnlineParser {
 | 
				
			||||||
    fn get_m3u8(&self) -> Vec<&M3u8> {
 | 
					    fn get_m3u8(&self) -> Vec<&M3u8> {
 | 
				
			||||||
        self.m3u8_items.iter().collect()
 | 
					        self.m3u8_items.iter().collect()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl GetPlayPath for Parser {
 | 
					impl GetPlayPath for OnlineParser {
 | 
				
			||||||
    fn get_path_to_play<'a>(&'a self, link: Rc<String>) -> Result<Rc<String>, String> {
 | 
					    fn get_path_to_play<'a>(&'a self, link: Rc<String>) -> Result<Rc<String>, String> {
 | 
				
			||||||
        Ok(link.clone())
 | 
					        Ok(link.clone())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#[derive(Serialize)]
 | 
					 | 
				
			||||||
pub struct OfflineParser {
 | 
					 | 
				
			||||||
    m3u8_items: Rc<Vec<OfflineEntry>>,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
impl OfflineParser {
 | 
					 | 
				
			||||||
    pub fn new(config: &Configuration) -> Self {
 | 
					 | 
				
			||||||
        Self {
 | 
					 | 
				
			||||||
            m3u8_items: config.offlinefile_content.clone(),
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl GetPlayPath for OfflineParser {
 | 
					 | 
				
			||||||
    fn get_path_to_play(&self, link: Rc<String>) -> Result<Rc<String>, String> {
 | 
					 | 
				
			||||||
        for offline_entry in &*self.m3u8_items {
 | 
					 | 
				
			||||||
            if *offline_entry.link == *link {
 | 
					 | 
				
			||||||
                return Ok(offline_entry.path.clone());
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        Err("Not stored for offline use".to_owned())
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
impl GetM3u8 for OfflineParser {
 | 
					 | 
				
			||||||
    fn get_m3u8(&self) -> Vec<&M3u8> {
 | 
					 | 
				
			||||||
        self.m3u8_items.iter().map(|x| &**x).collect()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -5,8 +5,7 @@ use structopt::StructOpt;
 | 
				
			|||||||
#[structopt(name = "ilovetv")]
 | 
					#[structopt(name = "ilovetv")]
 | 
				
			||||||
pub struct Opt {
 | 
					pub struct Opt {
 | 
				
			||||||
    #[structopt(short, long, default_value = "ask")]
 | 
					    #[structopt(short, long, default_value = "ask")]
 | 
				
			||||||
    /// Choose whether to launch in offlinemode, onlinemode or wheter to ask during startup.
 | 
					    /// Possible options: online, offline and ask
 | 
				
			||||||
    /// In offlinemode it's only possible to watch downloaded entries
 | 
					 | 
				
			||||||
    pub mode: Mode,
 | 
					    pub mode: Mode,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,9 +1,4 @@
 | 
				
			|||||||
use std::{
 | 
					use std::{fs, ops::Deref, path::PathBuf, rc::Rc};
 | 
				
			||||||
    fs,
 | 
					 | 
				
			||||||
    ops::{Deref, DerefMut},
 | 
					 | 
				
			||||||
    path::PathBuf,
 | 
					 | 
				
			||||||
    rc::Rc,
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::{download_with_progress, downloader::DualWriter, MAX_TRIES};
 | 
					use crate::{download_with_progress, downloader::DualWriter, MAX_TRIES};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -79,10 +74,10 @@ impl Playlist {
 | 
				
			|||||||
            let url = self
 | 
					            let url = self
 | 
				
			||||||
                .url
 | 
					                .url
 | 
				
			||||||
                .as_ref()
 | 
					                .as_ref()
 | 
				
			||||||
                .clone()
 | 
					                .ok_or_else(|| String::from("In offline mode"))?
 | 
				
			||||||
                .ok_or_else(|| String::from("In offline mode"))?;
 | 
					                .clone();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            let downloaded = download_with_progress(url, None)
 | 
					            let downloaded = download_with_progress(&url, None)
 | 
				
			||||||
                .await
 | 
					                .await
 | 
				
			||||||
                .and_then(DualWriter::get_string);
 | 
					                .and_then(DualWriter::get_string);
 | 
				
			||||||
            if let Ok(content) = downloaded {
 | 
					            if let Ok(content) = downloaded {
 | 
				
			||||||
@@ -102,9 +97,3 @@ impl Deref for Playlist {
 | 
				
			|||||||
        &self.content
 | 
					        &self.content
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
impl DerefMut for Playlist {
 | 
					 | 
				
			||||||
    fn deref_mut(&mut self) -> &mut Self::Target {
 | 
					 | 
				
			||||||
        &mut self.content
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user