From bc6b5cba34b94fe59b97f044b2c103e6f130af64 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Thu, 2 Mar 2023 23:51:37 +0100 Subject: [PATCH] Created basic structure --- src/config.rs | 42 ++++++++++++++++++++++++++++++++++-------- src/m3u8.rs | 23 ++++++++++++++++++++++- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4793232..aa57f62 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,12 +9,13 @@ use directories::ProjectDirs; use serde::{Deserialize, Serialize}; use serde_json; -use crate::{download_with_progress, get_mut_ref, Readline}; +use crate::{download_with_progress, get_mut_ref, m3u8::DataEntry, Readline}; const JSON_CONFIG_FILENAME: &'static str = "config.json"; const APP_IDENTIFIER: [&'static str; 3] = ["com", "billenius", "ilovetv"]; const STANDARD_PLAYLIST_FILENAME: &'static str = "playlist.m3u8"; const STANDARD_SEEN_LINKS_FILENAME: &'static str = "watched_links.json"; +const STANDARD_OFFLINE_FILENAME: &'static str = "ilovetv_offline.json"; const MAX_TRIES: u8 = 4; #[derive(Serialize, Deserialize, Debug)] @@ -84,6 +85,8 @@ pub struct Configuration { pub seen_links_path: PathBuf, pub seen_links: Vec, config_file_path: PathBuf, + pub data_dir: PathBuf, + pub datafile_content: Vec, } impl Configuration { @@ -91,31 +94,40 @@ impl Configuration { let project_dirs = ProjectDirs::from(APP_IDENTIFIER[0], APP_IDENTIFIER[1], APP_IDENTIFIER[2]).unwrap(); - // Config dir + // Make sure all the dirs for the project are setup correctly let config_dir = project_dirs.config_dir(); + let cache_dir = project_dirs.cache_dir().to_path_buf(); + let data_dir = project_dirs.data_local_dir().to_path_buf(); + let _ = [&config_dir, &cache_dir.as_path(), &data_dir.as_path()] + .iter() + .filter(|x| !x.exists()) + .map(fs::create_dir_all); + + // Config setup let config_file_path = config_dir.join(JSON_CONFIG_FILENAME).to_path_buf(); - let _ = fs::create_dir_all(config_dir); - // Read/create config let configuration = Conf::new(&config_file_path)?; fs::write( &config_file_path, serde_json::to_string(&configuration).unwrap(), )?; - // Setup dirs for playlist - let cache_dir = project_dirs.cache_dir().to_path_buf(); + // Playlist let playlist_path = cache_dir.join(&configuration.playlist_filename); let seen_links_path = cache_dir.join(&configuration.seen_links_filename); - let _ = fs::create_dir_all(&cache_dir); - let seen_links = Self::get_watched(&seen_links_path).unwrap_or_default(); + // Datadir + let datafile = data_dir.join(STANDARD_OFFLINE_FILENAME); + let datafile_content = Self::get_datafile_content(&datafile).unwrap_or_default(); + Ok(Self { conf: configuration, playlist_path, seen_links, seen_links_path, config_file_path, + data_dir, + datafile_content, }) } @@ -127,6 +139,15 @@ impl Configuration { } } + pub fn add_datafile_ugly(&self, data_entry: DataEntry) { + unsafe { get_mut_ref(&self.datafile_content) }.push(data_entry); + } + + pub fn write_datafile(&self) -> Result<(), io::Error> { + let path = self.data_dir.join(STANDARD_OFFLINE_FILENAME); + fs::write(path, serde_json::to_string(&self.datafile_content)?) + } + fn get_watched(path: &Path) -> Option> { let reader = BufReader::new(File::open(&path).ok()?); serde_json::from_reader(reader).ok() @@ -196,6 +217,11 @@ impl Configuration { .await? .get_string() } + + fn get_datafile_content(datafile: &PathBuf) -> Option> { + let reader = BufReader::new(File::open(datafile).ok()?); + serde_json::from_reader(reader).ok() + } } impl Deref for Configuration { diff --git a/src/m3u8.rs b/src/m3u8.rs index 3f258d5..d9fa9ac 100644 --- a/src/m3u8.rs +++ b/src/m3u8.rs @@ -1,6 +1,8 @@ use colored::Colorize; -use std::fmt::Display; +use serde::{Deserialize, Serialize}; +use std::{fmt::Display, ops::Deref}; +#[derive(Serialize, Deserialize)] pub struct M3u8 { pub tvg_id: String, pub tvg_name: String, @@ -22,3 +24,22 @@ impl Display for M3u8 { Ok(()) } } + +#[derive(Serialize, Deserialize)] +pub struct DataEntry { + m3u8: M3u8, + pub path: String, +} +impl DataEntry { + pub fn new(m3u8: M3u8, path: String) -> Self { + Self { m3u8, path } + } +} + +impl Deref for DataEntry { + type Target = M3u8; + + fn deref(&self) -> &Self::Target { + &self.m3u8 + } +}