diff --git a/src/anime/anime.rs b/src/anime/anime.rs index 88d36be..e680f7d 100644 --- a/src/anime/anime.rs +++ b/src/anime/anime.rs @@ -1,6 +1,9 @@ use crate::open_video; use crate::{anime_link, anime_names}; -use crate::{get_anime_id, get_user_anime_progress, update_anime_progress}; +use crate::{ + get_an_progress, get_anime_id, get_user_anime_progress, update_anime_progress, + write_an_progress, +}; use crossterm::{ event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, @@ -158,18 +161,28 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( //if KeyCode::Enter => { KeyCode::Enter => { if ep_select == false { + app.progress = 0; let selected = app.messages.state.selected(); app.title = app.messages.items[selected.unwrap()].clone(); let anime_info = get_anime_info(&app.title); app.anime_id = get_anime_id(anime_info.0); app.messages.items.clear(); - app.progress = - get_user_anime_progress(app.anime_id, app.token.as_str()); - app.messages.state.select(Some(app.progress as usize)); + if app.token == "local" || app.anime_id == 0 { + app.progress = get_an_progress(&app.title) as i32; + app.messages.state.select(Some(app.progress as usize)); + } else { + app.progress = + get_user_anime_progress(app.anime_id, app.token.as_str()); + app.messages.state.select(Some(app.progress as usize)); + } if anime_info.1 == 1 { let link = anime_link(&app.title, 1); open_video((link.0, link.1)); - update_anime_progress(app.anime_id, 1, app.token.as_str()); + if app.token == "local" || app.anime_id == 0 { + write_an_progress(&app.title, &1); + } else { + update_anime_progress(app.anime_id, 1, app.token.as_str()); + } } else { for ep in 1..anime_info.1 + 1 { app.messages.push(format!("Episode {}", ep)); @@ -189,11 +202,15 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( let link = anime_link(&app.title, app.ep); open_video((link.0, link.1)); if app.ep > app.progress as u64 { - update_anime_progress( - app.anime_id, - app.ep as usize, - app.token.as_str(), - ); + if app.token == "local" || app.anime_id == 0 { + write_an_progress(&app.title, &app.ep); + } else { + update_anime_progress( + app.anime_id, + app.ep as usize, + app.token.as_str(), + ); + } app.progress = app.ep as i32; } } diff --git a/src/anime/trackers.rs b/src/anime/trackers.rs index 68b779d..47fe8d0 100644 --- a/src/anime/trackers.rs +++ b/src/anime/trackers.rs @@ -17,10 +17,24 @@ pub fn get_token() -> String { //read token from file let token = fs::read_to_string(&token_path).unwrap(); if token.is_empty() { - println!("please go to the below link and copy and past the token below"); - println!("https://anilist.co/api/v2/oauth/authorize?client_id=9121&response_type=token"); - let token = string_input("token: "); - fs::write(&token_path, token).unwrap(); + //ask user if they want to add a token or track locally + let input = string_input( + "would you want to link anilist(sellecting no will track anime localy)? (y/n)", + ); + if input == "y" { + println!("please go to the below link and copy and past the token below"); + println!( + "https://anilist.co/api/v2/oauth/authorize?client_id=9121&response_type=token" + ); + let token = string_input("token: "); + fs::write(&token_path, token).unwrap(); + } else if input == "n" { + let token = "local"; + fs::write(&token_path, token).unwrap(); + } else { + println!("invalid input"); + std::process::exit(1); + } } let token = fs::read_to_string(&token_path).unwrap(); token @@ -57,14 +71,20 @@ query ($id: Int, $search: Int) { .text(); let regex = regex::Regex::new(r#"id":(.*?),"#).unwrap(); let resp: String = resp.as_ref().unwrap().to_string(); - let id = regex - .captures(&resp) - .unwrap() - .get(1) - .unwrap() - .as_str() - .parse::() - .unwrap(); + //if error let id = 0 + let id = match regex.captures(&resp) { + Some(captures) => captures[1].parse::().unwrap(), + None => 0, + }; + + // let id = regex + // .captures(&resp) + // .unwrap() + // .get(1) + // .unwrap() + // .as_str() + // .parse::() + // .unwrap(); id } @@ -173,3 +193,45 @@ mutation ($mediaId: Int, $status: MediaListStatus, $progress: Int) { .unwrap() .text(); } + +// local tracking +pub fn get_an_json() -> serde_json::Value { + let config_path = dirs::config_dir().unwrap().join("kami"); + if !config_path.exists() { + fs::create_dir_all(&config_path).unwrap(); + } + let json_path = config_path.join("an_progress.json"); + if !json_path.exists() { + fs::File::create(&json_path).unwrap(); + } + let json = fs::read_to_string(&json_path).unwrap(); + let json: serde_json::Value = serde_json::from_str(&json).unwrap_or(serde_json::Value::Null); + json +} + +pub fn write_an_progress(title: &str, progress: &u64) { + let config_path = dirs::config_dir().unwrap().join("kami"); + let json_path = config_path.join("an_progress.json"); + let json = fs::read_to_string(&json_path).unwrap(); + let mut json: serde_json::Value = + serde_json::from_str(&json).unwrap_or(serde_json::Value::Null); + let mut title_json = serde_json::Map::new(); + title_json.insert( + "progress".to_string(), + serde_json::Value::from(progress.clone()), + ); + //insert title_json into json + if json[title].is_null() { + json[title] = serde_json::Value::from(title_json); + } else { + json[title]["progress"] = serde_json::Value::from(progress.clone()); + } + let json = serde_json::to_string_pretty(&json).unwrap(); + fs::write(&json_path, json).unwrap(); +} + +pub fn get_an_progress(title: &str) -> i32 { + let json = get_an_json(); + let selected = json[title]["progress"].as_u64().unwrap_or(0); + selected as i32 +} diff --git a/src/ln/ln.rs b/src/ln/ln.rs index e445bb8..bf46174 100644 --- a/src/ln/ln.rs +++ b/src/ln/ln.rs @@ -115,7 +115,7 @@ impl<'a> App { pub fn ln_ui(chapter: u32) -> Result<(), Box> { // setup terminal - let _ = get_json(); + let _ = get_ln_json(); enable_raw_mode()?; let mut stdout = io::stdout(); execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; diff --git a/src/ln/tracker.rs b/src/ln/tracker.rs index b379559..80d7152 100644 --- a/src/ln/tracker.rs +++ b/src/ln/tracker.rs @@ -2,7 +2,7 @@ use serde_json; use std::fs; // // -pub fn get_json() -> serde_json::Value { +pub fn get_ln_json() -> serde_json::Value { let config_path = dirs::config_dir().unwrap().join("kami"); if !config_path.exists() { fs::create_dir_all(&config_path).unwrap(); @@ -43,7 +43,7 @@ pub fn write_ln_progress(title: &str, current_page: &u32, selected: &usize) { } pub fn get_ln_progress(title: &str) -> (u32, usize) { - let json = get_json(); + let json = get_ln_json(); let current_page = json[title]["current_page"].as_u64().unwrap_or(1) as u32; let selected = json[title]["selected"].as_u64().unwrap_or(0) as usize; (current_page, selected)