Commit 1361b33d10

1361b33d10e308e9c67165418f8dfa9761b0b2a0

parent: 238cf89517

Unsigned

Siriusmart <sirius@siri.ws> · 2025-11-08 12:30 UTC

channel caching done

Layout: unified · split

docs/src/config/commands.md +1 −1
@@ -7,7 +7,7 @@ The commands config file decides what options (that will run a certain command o
77```yaml
88launch_command: loadpage library ;; flush ;; history clear ;; key Esc 0 ;; key Up 0 ;; key Up 0 ;; key Left 0 ;; key Enter 0 # the key commands select the searchbar on launch
99video:
10- Reload updated video: run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id} # remove the cached info first, then reload the page
10- Reload updated video: rmcache ${id} ;; video ${id} # remove the cached info first, then reload the page
1111- Play video: parrun ${video-player} '${embed-url}'
1212- Play audio: mpv stop ;; resume ;; mpv sprop loop-file no ;; mpv loadfile '${embed-url}' ;; echo mpv Player started
1313- Play audio (loop): mpv stop ;; resume ;; mpv sprop loop-file inf ;; mpv loadfile '${embed-url}' ;; echo mpv Player started
src/exit.rs +3 −1
@@ -52,7 +52,9 @@ pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> {
5252 let cached_before = CACHED_BEFORE.get().unwrap();
5353
5454 let info_path = home_dir().unwrap().join(".local/share/youtube-tui/info/");
55 let thumbnail_path = home_dir().unwrap().join(".local/share/youtube-tui/thumbnails/");
55 let thumbnail_path = home_dir()
56 .unwrap()
57 .join(".local/share/youtube-tui/thumbnails/");
5658
5759 // remove cache that no longer exists
5860 for deleted in cached_before
src/global/functions/download_all_images.rs +1 −1
@@ -1,5 +1,5 @@
11use crate::global::structs::{Item, LocalStore};
2use std::{collections::HashSet, error::Error, fs, io::Cursor, path::PathBuf, sync::OnceLock, thread};
2use std::{error::Error, fs, io::Cursor, path::PathBuf, thread};
33
44pub struct DownloadRequest {
55 pub url: String,
src/global/functions/run_command.rs +27 −4
@@ -7,7 +7,12 @@ use crossterm::event::{KeyEvent, KeyModifiers};
77use home::home_dir;
88use ratatui::{backend::CrosstermBackend, Terminal};
99use std::{
10 env, error::Error, fs, io::Stdout, process::{Command, Stdio}, sync::Arc
10 env,
11 error::Error,
12 fs,
13 io::Stdout,
14 process::{Command, Stdio},
15 sync::Arc,
1116};
1217use tui_additions::framework::Framework;
1318
@@ -150,9 +155,27 @@ pub fn run_single_command(
150155 }
151156 }
152157 ["rmcache", id] => {
153 LocalStore::rm_cache(id);
154 let _ = fs::remove_file(home_dir().unwrap().join(".local/share/youtube-tui/info/").join(id).with_extension("json"));
155 let _ = fs::remove_file(home_dir().unwrap().join(".local/share/youtube-tui/thumbnails/").join(id));
158 let res = LocalStore::rm_cache(id);
159 let _ = fs::remove_file(
160 home_dir()
161 .unwrap()
162 .join(".local/share/youtube-tui/info/")
163 .join(id)
164 .with_extension("json"),
165 );
166 let _ = fs::remove_file(
167 home_dir()
168 .unwrap()
169 .join(".local/share/youtube-tui/thumbnails/")
170 .join(id),
171 );
172 if res {
173 *framework.data.global.get_mut::<Message>().unwrap() =
174 Message::Success(String::from("Cache cleared"))
175 } else {
176 *framework.data.global.get_mut::<Message>().unwrap() =
177 Message::Error(String::from("Item not in cache, nothing changed"))
178 }
156179 }
157180 ["help"] => {
158181 *framework.data.global.get_mut::<Message>().unwrap() = Message::Message(String::from(
src/global/functions/singleitem_load.rs +1 −18
@@ -2,8 +2,7 @@ use crate::{
22 config::MainConfig,
33 global::{functions::download_all_images, structs::Item, traits::SearchProviderWrapper},
44};
5use home::home_dir;
6use std::{error::Error, fs};
5use std::error::Error;
76
87pub fn load_playlist(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> {
98 let playlist =
@@ -31,25 +30,9 @@ pub fn load_video(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Err
3130}
3231
3332pub fn load_channel(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> {
34 let home_dir = home_dir().unwrap();
35 let cache_path = home_dir.join(format!(".local/share/youtube-tui/channels/{id}.json"));
36
37 if cache_path.exists() {
38 if let Ok(content) = fs::read_to_string(&cache_path) {
39 if let Ok(item) = serde_json::from_str::<Item>(&content) {
40 return Ok(item);
41 }
42 }
43 }
44
4533 let channel =
4634 Item::from_full_channel(SearchProviderWrapper::channel(id)?, mainconfig.image_index);
4735
48 if let Some(parent) = cache_path.parent() {
49 fs::create_dir_all(parent)?;
50 }
51 fs::write(&cache_path, serde_json::to_string(&channel)?)?;
52
5336 if mainconfig.images.display() {
5437 download_all_images(vec![(&channel).into()]);
5538 }
src/global/structs/history.rs +2
@@ -74,6 +74,7 @@ impl CollectionNoId<String> for CommandHistory {
7474 }
7575}
7676
77/*
7778#[derive(Clone, Default, Serialize, Deserialize)]
7879pub struct ChannelHistory(pub Vec<String>);
7980
@@ -96,3 +97,4 @@ impl CollectionNoId<String> for ChannelHistory {
9697 Self(items)
9798 }
9899}
100*/
src/global/structs/localstore.rs +18 −3
@@ -30,7 +30,10 @@ pub struct LocalStore {
3030
3131impl LocalStore {
3232 pub fn add_image(id: String) {
33 unsafe { LOCALSTORE.get_mut() }.unwrap().downloaded_images.insert(id);
33 unsafe { LOCALSTORE.get_mut() }
34 .unwrap()
35 .downloaded_images
36 .insert(id);
3437 }
3538
3639 pub fn init() {
@@ -39,8 +42,20 @@ impl LocalStore {
3942 }
4043 }
4144
42 pub fn rm_cache(id: &str) {
43 unsafe { LOCALSTORE.get_mut() }.unwrap().info.remove(id);
45 pub fn rm_cache(id: &str) -> bool {
46 let res = unsafe { LOCALSTORE.get_mut() }.unwrap().info.remove(id);
47 unsafe { LOCALSTORE.get_mut() }
48 .unwrap()
49 .downloaded_images
50 .remove(id);
51 let info_path = home_dir().unwrap().join(".local/share/youtube-tui/info/");
52 let thumbnail_path = home_dir()
53 .unwrap()
54 .join(".local/share/youtube-tui/thumbnails/");
55 let _ = fs::remove_file(info_path.join(id).with_extension("json"));
56 let _ = fs::remove_file(thumbnail_path.join(id));
57
58 res.is_some()
4459 }
4560
4661 pub fn get_info(id: &str) -> Option<Item> {
src/init.rs +2
@@ -83,10 +83,12 @@ pub fn init(
8383 .global
8484 .insert::<SearchHistory>(SearchHistory::load());
8585 framework.data.global.insert::<WatchHistory>(watchhistory);
86 /*
8687 framework
8788 .data
8889 .global
8990 .insert::<ChannelHistory>(ChannelHistory::load());
91 */
9092 framework
9193 .data
9294 .global
src/items/channeldisplay.rs +4 −3
@@ -4,7 +4,7 @@ use crate::{
44 global::{
55 functions::*,
66 structs::*,
7 traits::{Collection, CollectionNoId, SearchProviderWrapper},
7 traits::{Collection, SearchProviderWrapper},
88 },
99};
1010use ratatui::{
@@ -598,7 +598,7 @@ impl FrameworkItem for ChannelDisplay {
598598 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
599599 let page = framework.data.state.get::<Page>().unwrap().channeldisplay();
600600 let page_id = page.id.clone();
601 let is_main_page = matches!(page.r#type, ChannelDisplayPageType::Main);
601 // let is_main_page = matches!(page.r#type, ChannelDisplayPageType::Main);
602602
603603 match page.r#type {
604604 ChannelDisplayPageType::Main => {
@@ -632,7 +632,6 @@ impl FrameworkItem for ChannelDisplay {
632632
633633 let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap();
634634 watch_history.push(channel)?;
635
636635 }
637636 ChannelDisplayPageType::Videos => {
638637 let videos = SearchProviderWrapper::channel_videos(&page.id)?
@@ -696,6 +695,7 @@ impl FrameworkItem for ChannelDisplay {
696695 &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0,
697696 );
698697
698 /*
699699 if is_main_page {
700700 let channel_history = framework.data.global.get_mut::<ChannelHistory>().unwrap();
701701 if !channel_history.0.contains(&page_id) {
@@ -703,6 +703,7 @@ impl FrameworkItem for ChannelDisplay {
703703 let _ = channel_history.save();
704704 }
705705 }
706 */
706707
707708 Ok(())
708709 }