|
| 1 | +use tracing::{error}; |
| 2 | +use std::fs; |
| 3 | +use std::io::BufReader; |
| 4 | +use serde_json::{from_reader, to_string}; |
| 5 | +use chrono::Utc; |
| 6 | + |
| 7 | +use crate::sources::anime; |
| 8 | +use crate::sources::movie; |
| 9 | +use crate::commands::favorite::{get_recent_from_favorite}; |
| 10 | +use crate::commands::local_manifest::{get_local_manifest}; |
| 11 | +use crate::utils::configs::Configs; |
| 12 | + |
| 13 | +use crate::models::home::{Content, ContentData, HomeData}; |
| 14 | + |
| 15 | +const CACHE_DELAY: usize = 3 * 60 * 60 * 1000; // In milliseconds |
| 16 | + |
| 17 | +async fn get_cache(source: &str) -> Result<Option<HomeData>, String> { |
| 18 | + let app_config = Configs::get()?; |
| 19 | + |
| 20 | + let cache_dir = app_config.cache_dir.ok_or("Cache dir not set!")?; |
| 21 | + |
| 22 | + let home_cache_path = cache_dir.join(&format!("home_{}.json", source)); |
| 23 | + |
| 24 | + if home_cache_path.exists() { |
| 25 | + let file = fs::File::open(& home_cache_path).map_err(|e| e.to_string())?; |
| 26 | + let reader = BufReader::new(file); |
| 27 | + match from_reader::<BufReader<std::fs::File>, HomeData>(reader) { |
| 28 | + Ok(data) => return Ok(Some(data)), |
| 29 | + Err(_) => { |
| 30 | + return Ok(None) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return Ok(None); |
| 36 | +} |
| 37 | + |
| 38 | +async fn save_cache(source: &str, data: &HomeData) -> Result<(), String> { |
| 39 | + let app_config = Configs::get()?; |
| 40 | + let cache_dir = app_config.cache_dir.ok_or("Cache dir not set!")?; |
| 41 | + |
| 42 | + if !cache_dir.exists() { |
| 43 | + fs::create_dir_all(&cache_dir).map_err(|e| e.to_string())?; |
| 44 | + } |
| 45 | + |
| 46 | + let home_cache_path = cache_dir.join(&format!("home_{}.json", source)); |
| 47 | + |
| 48 | + let data_to_string = to_string(&data).map_err(|e| e.to_string())?; |
| 49 | + fs::write(&home_cache_path, data_to_string).map_err(|e| e.to_string())?; |
| 50 | + |
| 51 | + return Ok(()); |
| 52 | +} |
| 53 | + |
| 54 | +#[tauri::command] |
| 55 | +pub async fn home(source: String, force_remote: bool) -> Result<HomeData, String> { |
| 56 | + let mut home_data: HomeData = HomeData { |
| 57 | + relevant_content: vec![], |
| 58 | + content: vec![], |
| 59 | + last_save_timestamp: 0, |
| 60 | + }; |
| 61 | + |
| 62 | + let mut should_fetch_remote: bool = true; |
| 63 | + |
| 64 | + if !force_remote { |
| 65 | + if let Some(cache_home_data) = get_cache(&source).await?{ |
| 66 | + let current_timestamp: usize = Utc::now().timestamp_millis() as usize; |
| 67 | + if (current_timestamp - cache_home_data.last_save_timestamp) < CACHE_DELAY { |
| 68 | + home_data = cache_home_data; |
| 69 | + should_fetch_remote = false; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if should_fetch_remote { |
| 75 | + if source == "anime" { |
| 76 | + match anime::home::new(&source).await { |
| 77 | + Ok(data) => home_data = data, |
| 78 | + Err(e) => { |
| 79 | + error!("[HOME] Error: {}", e); |
| 80 | + } |
| 81 | + } |
| 82 | + }else if source == "movie" { |
| 83 | + match movie::home::new(&source).await { |
| 84 | + Ok(data) => home_data = data, |
| 85 | + Err(e) => { |
| 86 | + error!("[HOME] Error: {}", e); |
| 87 | + } |
| 88 | + } |
| 89 | + }else{ |
| 90 | + return Err("Unkown Source".to_string()); |
| 91 | + } |
| 92 | + |
| 93 | + save_cache(&source, &home_data).await?; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + /* Load Recent Watch */ |
| 98 | + let content_data = &mut home_data.content; |
| 99 | + |
| 100 | + let mut recent_content_data: Vec<ContentData> = vec![]; |
| 101 | + let recent_from_favorite = get_recent_from_favorite(15).await?; |
| 102 | + for item in recent_from_favorite { |
| 103 | + let local_manifest = get_local_manifest(item.source.clone(), item.id.to_string()).await?; |
| 104 | + match local_manifest.manifest_data { |
| 105 | + Some(data) => { |
| 106 | + let new_content = ContentData { |
| 107 | + source: item.source, |
| 108 | + id: item.id.to_string().clone(), |
| 109 | + title: data.title, |
| 110 | + poster: data.poster, |
| 111 | + }; |
| 112 | + recent_content_data.push(new_content); |
| 113 | + } |
| 114 | + None => { |
| 115 | + let new_content = ContentData { |
| 116 | + source: item.source, |
| 117 | + id: item.id.to_string().clone(), |
| 118 | + title: "?".to_string(), |
| 119 | + poster: "".to_string(), |
| 120 | + }; |
| 121 | + recent_content_data.push(new_content); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if recent_content_data.len() > 0 { |
| 127 | + content_data.insert( |
| 128 | + 0, |
| 129 | + Content { |
| 130 | + label: "Continue watching".to_string(), |
| 131 | + data: recent_content_data, |
| 132 | + }, |
| 133 | + ); |
| 134 | + } |
| 135 | + /* --- */ |
| 136 | + |
| 137 | + return Ok(home_data); |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | + |
0 commit comments