grandmother new file

This commit is contained in:
Love 2023-03-05 12:13:42 +01:00
parent c162450bdd
commit 92f2031dad
No known key found for this signature in database
GPG Key ID: A3C10DC241C8FA9F
3 changed files with 66 additions and 62 deletions

63
src/grandmother.rs Normal file
View File

@ -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<Self, String> {
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);
}
}
}

View File

@ -1,5 +1,6 @@
mod config; mod config;
mod downloader; mod downloader;
mod grandmother;
mod m3u8; mod m3u8;
mod parser; mod parser;
mod playlist; mod playlist;
@ -8,9 +9,10 @@ use std::io::{stdin, stdout, Stdin, StdoutLock, Write};
pub use config::Configuration; pub use config::Configuration;
pub use downloader::download_with_progress; pub use downloader::download_with_progress;
pub use grandmother::GrandMother;
pub use m3u8::{DataEntry, M3u8}; pub use m3u8::{DataEntry, M3u8};
pub use parser::Parser; pub use parser::Parser;
pub use playlist::{GrandMother, Playlist}; pub use playlist::Playlist;
pub const JSON_CONFIG_FILENAME: &'static str = "config.json"; pub const JSON_CONFIG_FILENAME: &'static str = "config.json";
pub const APP_IDENTIFIER: [&'static str; 3] = ["com", "billenius", "ilovetv"]; pub const APP_IDENTIFIER: [&'static str; 3] = ["com", "billenius", "ilovetv"];

View File

@ -1,5 +1,4 @@
use std::{ use std::{
borrow::BorrowMut,
fs, fs,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
path::PathBuf, path::PathBuf,
@ -103,63 +102,3 @@ impl DerefMut for Playlist {
&mut self.content &mut self.content
} }
} }
pub struct GrandMother {
pub parser: Parser,
pub playlist: Playlist,
pub config: Configuration,
}
impl GrandMother {
pub async fn new(config: Configuration) -> Result<Self, String> {
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);
}
}
}