More refactors

This commit is contained in:
Love 2023-03-05 19:51:21 +01:00
parent 81006d4c5c
commit 38b0e405d0
No known key found for this signature in database
GPG Key ID: A3C10DC241C8FA9F
7 changed files with 65 additions and 69 deletions

View File

@ -28,5 +28,5 @@ pub trait GetPlayPath {
fn get_path_to_play(&self, link: Rc<String>) -> Result<Rc<String>, String>;
}
pub trait M3u8PlayPath: GetM3u8 + GetPlayPath {}
impl<T: GetM3u8 + GetPlayPath> M3u8PlayPath for T {}
pub trait Parser: GetM3u8 + GetPlayPath {}
impl<T: GetM3u8 + GetPlayPath> Parser for T {}

View File

@ -1,15 +1,15 @@
#[allow(unused_imports)]
use crate::GetM3u8;
use crate::{
getm3u8::{M3u8PlayPath, WatchedFind},
Configuration, OfflineParser, Parser, Playlist, MAX_TRIES,
getm3u8::{Parser, WatchedFind},
Configuration, OfflineParser, OnlineParser, Playlist, MAX_TRIES,
};
use std::fs;
type Error = String;
pub struct GrandMother {
pub parser: Box<dyn M3u8PlayPath>,
pub parser: Box<dyn Parser>,
pub playlist: Option<Playlist>,
pub config: Configuration,
}
@ -20,8 +20,8 @@ impl GrandMother {
let seen_links = config.seen_links.iter().map(|x| x.as_str()).collect();
let playlist = playlist.await?;
let playlist_content = playlist.get_saved_or_download().await?;
let parser: Box<dyn M3u8PlayPath> =
Box::new(Parser::new(&playlist_content, &seen_links).await);
let parser: Box<dyn Parser> =
Box::new(OnlineParser::new(&playlist_content, &seen_links).await);
Ok(Self {
parser,
@ -31,7 +31,7 @@ impl GrandMother {
}
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 {
parser,
playlist: None,
@ -69,7 +69,7 @@ impl GrandMother {
.iter()
.map(|x| x.link.as_str())
.collect();
self.parser = Box::new(Parser::new(&content, &watched_links).await);
self.parser = Box::new(OnlineParser::new(&content, &watched_links).await);
Ok(())
}

View File

@ -3,8 +3,9 @@ mod downloader;
pub mod getm3u8;
mod grandmother;
mod m3u8;
mod offlineparser;
mod onlineparser;
mod opt;
mod parser;
mod playlist;
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 grandmother::GrandMother;
pub use m3u8::{M3u8, OfflineEntry};
pub use offlineparser::OfflineParser;
pub use onlineparser::OnlineParser;
pub use opt::{Mode, Opt};
pub use parser::{OfflineParser, Parser};
pub use playlist::Playlist;
pub const JSON_CONFIG_FILENAME: &'static str = "config.json";

42
src/offlineparser.rs Normal file
View 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()
}
}

View File

@ -4,11 +4,11 @@ use serde::Serialize;
use crate::{m3u8::M3u8, Configuration, GetM3u8, GetPlayPath, OfflineEntry};
pub struct Parser {
pub struct OnlineParser {
m3u8_items: Vec<M3u8>,
}
impl Parser {
impl OnlineParser {
pub async fn new(m3u_content: &str, watched_links: &Vec<&str>) -> Self {
Self {
m3u8_items: Self::parse_m3u8(m3u_content, watched_links),
@ -23,14 +23,6 @@ impl Parser {
.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) {
let seen_links: &Vec<&str> = &self
.m3u8_items
@ -80,7 +72,7 @@ impl Parser {
}
}
impl Deref for Parser {
impl Deref for OnlineParser {
type Target = Vec<M3u8>;
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> {
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> {
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()
}
}

View File

@ -5,8 +5,7 @@ use structopt::StructOpt;
#[structopt(name = "ilovetv")]
pub struct Opt {
#[structopt(short, long, default_value = "ask")]
/// Choose whether to launch in offlinemode, onlinemode or wheter to ask during startup.
/// In offlinemode it's only possible to watch downloaded entries
/// Possible options: online, offline and ask
pub mode: Mode,
}

View File

@ -1,9 +1,4 @@
use std::{
fs,
ops::{Deref, DerefMut},
path::PathBuf,
rc::Rc,
};
use std::{fs, ops::Deref, path::PathBuf, rc::Rc};
use crate::{download_with_progress, downloader::DualWriter, MAX_TRIES};
@ -79,10 +74,10 @@ impl Playlist {
let url = self
.url
.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
.and_then(DualWriter::get_string);
if let Ok(content) = downloaded {
@ -102,9 +97,3 @@ impl Deref for Playlist {
&self.content
}
}
impl DerefMut for Playlist {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.content
}
}