From be3839ae679e65362fba90a2bc69fd2bbe7e102e Mon Sep 17 00:00:00 2001 From: lov3b Date: Sun, 5 Mar 2023 12:13:42 +0100 Subject: [PATCH] grandmother new file --- src/grandmother.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++- src/playlist.rs | 61 -------------------------------------------- 3 files changed, 66 insertions(+), 62 deletions(-) create mode 100644 src/grandmother.rs diff --git a/src/grandmother.rs b/src/grandmother.rs new file mode 100644 index 0000000..93c95ce --- /dev/null +++ b/src/grandmother.rs @@ -0,0 +1,63 @@ +use std::fs; + +use crate::{Configuration, Parser, Playlist, MAX_TRIES}; + +pub struct GrandMother { + pub parser: Parser, + pub playlist: Playlist, + pub config: Configuration, +} + +impl GrandMother { + pub async fn new(config: Configuration) -> 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?; + let playlist_content = playlist.get_saved_or_download().await?; + + let parser = Parser::new(&playlist_content, &seen_links).await; + + Ok(Self { + parser, + playlist, + config, + }) + } + + pub async fn refresh_dirty(&self) { + let ptr = self as *const Self as *mut Self; + unsafe { &mut *ptr }.refresh().await; + } + + pub async fn refresh(&mut self) { + let mut counter = 0; + let content = loop { + counter += 1; + let content = self.playlist.download().await; + if counter > MAX_TRIES { + return; + } + if let Ok(content) = content { + break content; + } + println!("Retrying {}/{}", counter, MAX_TRIES); + }; + + let watched_links = self.parser.get_watched(); + let watched_links = watched_links.iter().map(|x| x.as_str()).collect(); + self.parser = Parser::new(&content, &watched_links).await; + } + + pub fn save_watched(&self) { + let watched_items = self.parser.get_watched(); + + let resp = fs::write( + &self.config.seen_links_path, + serde_json::to_string(&watched_items).unwrap(), + ); + + if let Err(e) = resp { + eprintln!("Failed to write watched links {:?}", e); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 6487e8d..90f97f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ mod config; mod downloader; +mod grandmother; mod m3u8; mod parser; mod playlist; @@ -8,9 +9,10 @@ use std::io::{stdin, stdout, Stdin, StdoutLock, Write}; pub use config::Configuration; pub use downloader::download_with_progress; +pub use grandmother::GrandMother; pub use m3u8::{DataEntry, M3u8}; pub use parser::Parser; -pub use playlist::{GrandMother, Playlist}; +pub use playlist::Playlist; pub const JSON_CONFIG_FILENAME: &'static str = "config.json"; pub const APP_IDENTIFIER: [&'static str; 3] = ["com", "billenius", "ilovetv"]; diff --git a/src/playlist.rs b/src/playlist.rs index 320031a..d2ec2cd 100644 --- a/src/playlist.rs +++ b/src/playlist.rs @@ -1,5 +1,4 @@ use std::{ - borrow::BorrowMut, fs, ops::{Deref, DerefMut}, path::PathBuf, @@ -103,63 +102,3 @@ impl DerefMut for Playlist { &mut self.content } } - -pub struct GrandMother { - pub parser: Parser, - pub playlist: Playlist, - pub config: Configuration, -} - -impl GrandMother { - pub async fn new(config: Configuration) -> 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?; - let playlist_content = playlist.get_saved_or_download().await?; - - let parser = Parser::new(&playlist_content, &seen_links).await; - - Ok(Self { - parser, - playlist, - config, - }) - } - - pub async fn refresh_dirty(&self) { - let ptr = self as *const Self as *mut Self; - unsafe { &mut *ptr }.refresh().await; - } - - pub async fn refresh(&mut self) { - let mut counter = 0; - let content = loop { - counter += 1; - let content = self.playlist.download().await; - if counter > MAX_TRIES { - return; - } - if let Ok(content) = content { - break content; - } - println!("Retrying {}/{}", counter, MAX_TRIES); - }; - - let watched_links = self.parser.get_watched(); - let watched_links = watched_links.iter().map(|x| x.as_str()).collect(); - self.parser = Parser::new(&content, &watched_links).await; - } - - pub fn save_watched(&self) { - let watched_items = self.parser.get_watched(); - - let resp = fs::write( - &self.config.seen_links_path, - serde_json::to_string(&watched_items).unwrap(), - ); - - if let Err(e) = resp { - eprintln!("Failed to write watched links {:?}", e); - } - } -}