Created basic structure

This commit is contained in:
Love 2023-03-02 23:51:37 +01:00
parent eb1e4e7ce1
commit bc6b5cba34
2 changed files with 56 additions and 9 deletions

View File

@ -9,12 +9,13 @@ use directories::ProjectDirs;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json; 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 JSON_CONFIG_FILENAME: &'static str = "config.json";
const APP_IDENTIFIER: [&'static str; 3] = ["com", "billenius", "ilovetv"]; const APP_IDENTIFIER: [&'static str; 3] = ["com", "billenius", "ilovetv"];
const STANDARD_PLAYLIST_FILENAME: &'static str = "playlist.m3u8"; const STANDARD_PLAYLIST_FILENAME: &'static str = "playlist.m3u8";
const STANDARD_SEEN_LINKS_FILENAME: &'static str = "watched_links.json"; const STANDARD_SEEN_LINKS_FILENAME: &'static str = "watched_links.json";
const STANDARD_OFFLINE_FILENAME: &'static str = "ilovetv_offline.json";
const MAX_TRIES: u8 = 4; const MAX_TRIES: u8 = 4;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -84,6 +85,8 @@ pub struct Configuration {
pub seen_links_path: PathBuf, pub seen_links_path: PathBuf,
pub seen_links: Vec<String>, pub seen_links: Vec<String>,
config_file_path: PathBuf, config_file_path: PathBuf,
pub data_dir: PathBuf,
pub datafile_content: Vec<DataEntry>,
} }
impl Configuration { impl Configuration {
@ -91,31 +94,40 @@ impl Configuration {
let project_dirs = let project_dirs =
ProjectDirs::from(APP_IDENTIFIER[0], APP_IDENTIFIER[1], APP_IDENTIFIER[2]).unwrap(); 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 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 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)?; let configuration = Conf::new(&config_file_path)?;
fs::write( fs::write(
&config_file_path, &config_file_path,
serde_json::to_string(&configuration).unwrap(), serde_json::to_string(&configuration).unwrap(),
)?; )?;
// Setup dirs for playlist // Playlist
let cache_dir = project_dirs.cache_dir().to_path_buf();
let playlist_path = cache_dir.join(&configuration.playlist_filename); let playlist_path = cache_dir.join(&configuration.playlist_filename);
let seen_links_path = cache_dir.join(&configuration.seen_links_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(); 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 { Ok(Self {
conf: configuration, conf: configuration,
playlist_path, playlist_path,
seen_links, seen_links,
seen_links_path, seen_links_path,
config_file_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<Vec<String>> { fn get_watched(path: &Path) -> Option<Vec<String>> {
let reader = BufReader::new(File::open(&path).ok()?); let reader = BufReader::new(File::open(&path).ok()?);
serde_json::from_reader(reader).ok() serde_json::from_reader(reader).ok()
@ -196,6 +217,11 @@ impl Configuration {
.await? .await?
.get_string() .get_string()
} }
fn get_datafile_content(datafile: &PathBuf) -> Option<Vec<DataEntry>> {
let reader = BufReader::new(File::open(datafile).ok()?);
serde_json::from_reader(reader).ok()
}
} }
impl Deref for Configuration { impl Deref for Configuration {

View File

@ -1,6 +1,8 @@
use colored::Colorize; use colored::Colorize;
use std::fmt::Display; use serde::{Deserialize, Serialize};
use std::{fmt::Display, ops::Deref};
#[derive(Serialize, Deserialize)]
pub struct M3u8 { pub struct M3u8 {
pub tvg_id: String, pub tvg_id: String,
pub tvg_name: String, pub tvg_name: String,
@ -22,3 +24,22 @@ impl Display for M3u8 {
Ok(()) 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
}
}