added better anime history

This commit is contained in:
Zastian Pretorius
2023-01-17 15:08:57 +00:00
parent 6b58d42af7
commit e7438cb664
3 changed files with 82 additions and 15 deletions

View File

@@ -1,12 +1,15 @@
#+title: Readme
#+OPTIONS: toc:2
* Table of content
1. [[#Why-use-kami][Why use kami]]
2. [[#Dependencies][Dependencies]]
3. [[#Install][Install]]
1. [[#IMPORTANT][IMPORTANT]]
2. [[#Why-use-kami][Why use kami]]
3. [[#Dependencies][Dependencies]]
4. [[#Install][Install]]
- [[#LinuxMac][Linux/mac]]
- [[#Windows][Windows]]
4. [[#Honorable-mentions][Honorable mentions]]
5. [[#Honorable-mentions][Honorable mentions]]
* IMPORTANT
remove all contents of ~$HOME/.config/kami/an_progress.json~ new version is not compatibal with old progress file.
* Why use kami
Well its a fast and easy way to watch anime and read light novels right in your terminal no need to open a browser.
Also rust is fast as fuck boiiiii.

View File

@@ -1,5 +1,5 @@
use crate::{
get_an_progress, get_anime_id, get_user_anime_progress, update_anime_progress,
get_an_history, get_an_progress, get_anime_id, get_user_anime_progress, update_anime_progress,
write_an_progress,
};
use crate::{get_anime_link, get_animes, get_image};
@@ -85,6 +85,7 @@ struct App {
/// Current value of the input box
input: String,
animes: (Vec<String>, Vec<String>, Vec<String>),
image: String,
/// Current input mode
input_mode: InputMode,
/// History of recorded messages
@@ -103,7 +104,8 @@ impl<'a> App {
fn default() -> App {
App {
input: String::new(),
animes: (Vec::new(), Vec::new(), Vec::new()),
animes: get_an_history(),
image: String::new(),
input_mode: InputMode::Normal,
messages: StatefulList::with_items(Vec::new()),
title: String::new(),
@@ -182,11 +184,15 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
let config_path = dirs::config_dir().unwrap().join("kami");
let image_path = config_path.join("tmp.jpg");
let selected = app.messages.state.selected();
let image_url = app.animes.2[selected.unwrap()].clone();
get_image(&image_url, &image_path.to_str().unwrap());
get_image(&app.image, &image_path.to_str().unwrap());
print_from_file(image_path, &config).expect("Image printing failed.");
}
app.messages.items.clear();
for anime in &app.animes.1 {
app.messages.push(anime.to_string());
}
app.input_mode = InputMode::Normal;
loop {
terminal.draw(|f| ui(f, &mut app))?;
@@ -207,6 +213,8 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
}
false => {
app.messages.next();
let selected = app.messages.state.selected();
app.image = app.animes.2[selected.unwrap()].clone();
change_image(&app);
}
},
@@ -216,6 +224,8 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
}
false => {
app.messages.next();
let selected = app.messages.state.selected();
app.image = app.animes.2[selected.unwrap()].clone();
change_image(&app);
}
},
@@ -225,6 +235,8 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
}
false => {
app.messages.previous();
let selected = app.messages.state.selected();
app.image = app.animes.2[selected.unwrap()].clone();
change_image(&app);
}
},
@@ -234,6 +246,8 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
}
false => {
app.messages.previous();
let selected = app.messages.state.selected();
app.image = app.animes.2[selected.unwrap()].clone();
change_image(&app);
}
},
@@ -265,10 +279,13 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
&app.cast.1,
)
}
let selected = app.messages.state.selected();
let image_url = app.animes.2[selected.unwrap()].clone();
if app.token == "local" || app.anime_id == 0 {
write_an_progress(&app.title, &1);
write_an_progress((&app.title, &app.link, &image_url), &1);
} else {
update_anime_progress(app.anime_id, 1, app.token.as_str());
write_an_progress((&app.title, &app.link, &image_url), &1);
}
} else {
for ep in 1..anime_info.1 + 1 {
@@ -295,15 +312,17 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
&app.cast.1,
)
}
let image_url = &app.image;
if app.ep > app.progress as u64 {
if app.token == "local" || app.anime_id == 0 {
write_an_progress(&app.title, &app.ep);
write_an_progress((&app.title, &app.link, &image_url), &app.ep);
} else {
update_anime_progress(
app.anime_id,
app.ep as usize,
app.token.as_str(),
);
write_an_progress((&app.title, &app.link, &image_url), &app.ep);
}
app.progress = app.ep as i32;
}

View File

@@ -209,7 +209,7 @@ pub fn get_an_json() -> serde_json::Value {
json
}
pub fn write_an_progress(title: &str, progress: &u64) {
pub fn write_an_progress(anime: (&str, &str, &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();
@@ -220,16 +220,61 @@ pub fn write_an_progress(title: &str, progress: &u64) {
"progress".to_string(),
serde_json::Value::from(progress.clone()),
);
title_json.insert("link".to_string(), serde_json::Value::from(anime.1));
title_json.insert("image".to_string(), serde_json::Value::from(anime.2));
title_json.insert(
"updated".to_string(),
serde_json::Value::from(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
),
);
//insert title_json into json
if json[title].is_null() {
json[title] = serde_json::Value::from(title_json);
if json[anime.0].is_null() {
json[anime.0] = serde_json::Value::from(title_json);
} else {
json[title]["progress"] = serde_json::Value::from(progress.clone());
json[anime.0]["progress"] = serde_json::Value::from(progress.clone());
json[anime.0]["link"] = serde_json::Value::from(anime.1);
json[anime.0]["image"] = serde_json::Value::from(anime.2);
json[anime.0]["updated"] = serde_json::Value::from(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
);
}
let json = serde_json::to_string_pretty(&json).unwrap();
fs::write(&json_path, json).unwrap();
}
pub fn get_an_history() -> (Vec<String>, Vec<String>, Vec<String>) {
//get the titles, links, and images from the json
let json = get_an_json();
let mut titles = vec![];
let mut links = vec![];
let mut images = vec![];
let mut last_updated = vec![];
//if the json is empty, return empty vectors
if json.is_null() {
return (titles, links, images);
}
for (key, value) in json.as_object().unwrap() {
titles.push(key.to_string());
links.push(value["link"].as_str().unwrap().to_string());
images.push(value["image"].as_str().unwrap().to_string());
println!("{}", value["updated"].as_u64().unwrap());
last_updated.push(value["updated"].as_u64().unwrap());
}
let mut indices: Vec<usize> = (0..last_updated.len()).collect();
indices.sort_by(|&a, &b| last_updated[b].cmp(&last_updated[a]));
titles = indices.iter().map(|&i| titles[i].clone()).collect();
links = indices.iter().map(|&i| links[i].clone()).collect();
images = indices.iter().map(|&i| images[i].clone()).collect();
(links, titles, images)
}
pub fn get_an_progress(title: &str) -> i32 {
let json = get_an_json();
let selected = json[title]["progress"].as_u64().unwrap_or(0);