Commit caa4bca5df

caa4bca5df7f11b88adc88fa42539c31e923b091

parent: 706c897f7b

Unsigned

Siriusmart <71584876+Siriusmart@users.noreply.github.com> · 2023-05-29 21:09 UTC

channel lazy loading and fixed wrongly labeling feeds as updated

Layout: unified · split

README.md +2
@@ -5,6 +5,8 @@
5[![](https://img.shields.io/aur/version/youtube-tui)](https://aur.archlinux.org/packages/youtube-tui) 5[![](https://img.shields.io/aur/version/youtube-tui)](https://aur.archlinux.org/packages/youtube-tui)
6![](https://shields.io/github/stars/siriusmart/youtube-tui?style=social) 6![](https://shields.io/github/stars/siriusmart/youtube-tui?style=social)
7 7
8***Read more in the [user manual](https://siriusmart.github.io/youtube-tui) (including installation guide and documentation).***
9
8# Overview 10# Overview
9 11
10Written in Rust, the **YouTube TUI** is a lightweight and user friendly TUI for browsing YouTube content from the terminal. 12Written in Rust, the **YouTube TUI** is a lightweight and user friendly TUI for browsing YouTube content from the terminal.
docs/src/changelogs.md +13 −2
@@ -1,6 +1,17 @@
1# Changelogs 1# Changelogs
2 2
3## v0.7.2 3### v0.7.3 (latest unstable on git)
4
5#### Added
6
7- Lazy feed syncing (configurable), so you are not actively ddosing the Invidious instance.
8
9#### Fixed
10
11- Search bar no longer shows escaped search query: the original text string is displayed instead.
12- Fixed something about labeling channels with * although no new video is published.
13
14## v0.7.2 (latest stable crates.io)
4 15
5### Added 16### Added
6 17
@@ -9,7 +20,7 @@
9 20
10### Fixed 21### Fixed
11 22
12- Search url not escaped, so searches with symbols or spaces are not working. 23- Search url not escaped, so searches with symbols or spaces are now working.
13 24
14### v0.7.1 25### v0.7.1
15 26
src/config/main.rs +34
@@ -22,6 +22,7 @@ pub struct MainConfig {
22 pub images: Images, 22 pub images: Images,
23 #[serde(default = "refresh_after_modifying_search_filters_default")] 23 #[serde(default = "refresh_after_modifying_search_filters_default")]
24 pub refresh_after_modifying_search_filters: bool, 24 pub refresh_after_modifying_search_filters: bool,
25 pub syncing: SyncConfig,
25 #[serde(default = "image_index_default")] 26 #[serde(default = "image_index_default")]
26 // The image to download from the array of images provided by the invidious api 27 // The image to download from the array of images provided by the invidious api
27 // 0 is usually `maxres` and 3 (default) is good enough for normal uses without having huge files sizes 28 // 0 is usually `maxres` and 3 (default) is good enough for normal uses without having huge files sizes
@@ -52,6 +53,7 @@ impl Default for MainConfig {
52 image_index: image_index_default(), 53 image_index: image_index_default(),
53 refresh_after_modifying_search_filters: refresh_after_modifying_search_filters_default( 54 refresh_after_modifying_search_filters: refresh_after_modifying_search_filters_default(
54 ), 55 ),
56 syncing: sync_config_default(),
55 provider: provider_default(), 57 provider: provider_default(),
56 shell: shell_default(), 58 shell: shell_default(),
57 59
@@ -64,6 +66,22 @@ impl ConfigTrait for MainConfig {
64 const LABEL: &'static str = "main"; 66 const LABEL: &'static str = "main";
65} 67}
66 68
69#[derive(Clone, Copy, Serialize, Deserialize)]
70pub struct SyncConfig {
71 #[serde(default = "download_images_default")]
72 pub download_images: bool,
73 #[serde(default = "sync_channel_info_default")]
74 pub sync_channel_info: bool,
75 #[serde(default = "sync_channel_cooldown_secs_default")]
76 pub sync_channel_cooldown_secs: u64,
77 #[serde(default = "sync_videos_cooldown_secs_default")]
78 pub sync_videos_cooldown_secs: u64,
79}
80
81const fn sync_config_default() -> SyncConfig {
82 SyncConfig { download_images: download_images_default(), sync_channel_info: sync_channel_info_default(), sync_channel_cooldown_secs: sync_channel_cooldown_secs_default(), sync_videos_cooldown_secs: sync_videos_cooldown_secs_default() }
83}
84
67/// how images are handled/displayed 85/// how images are handled/displayed
68#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] 86#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
69pub enum Images { 87pub enum Images {
@@ -176,3 +194,19 @@ fn default_env() -> HashMap<String, String> {
176fn shell_default() -> String { 194fn shell_default() -> String {
177 String::from("sh") 195 String::from("sh")
178} 196}
197
198const fn download_images_default() -> bool {
199 true
200}
201
202const fn sync_channel_info_default() -> bool {
203 true
204}
205
206const fn sync_channel_cooldown_secs_default() -> u64 {
207 86400
208}
209
210const fn sync_videos_cooldown_secs_default() -> u64 {
211 600
212}
src/global/functions/run_command.rs +15 −7
@@ -361,9 +361,6 @@ pub fn run_single_command(
361 } 361 }
362 }; 362 };
363 363
364 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
365 let image_index = mainconfig.image_index;
366 let download_thumbnails = mainconfig.images.display();
367 let client = framework 364 let client = framework
368 .data 365 .data
369 .global 366 .global
@@ -375,8 +372,13 @@ pub fn run_single_command(
375 Message::Message(String::from("Syncing...")); 372 Message::Message(String::from("Syncing..."));
376 terminal.draw(|frame| framework.render(frame)).unwrap(); 373 terminal.draw(|frame| framework.render(frame)).unwrap();
377 374
375 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
376 let image_index = mainconfig.image_index;
377 let download_thumbnails = mainconfig.images.display();
378 let syncing = mainconfig.syncing;
379
378 let subscriptions = framework.data.global.get_mut::<Subscriptions>().unwrap(); 380 let subscriptions = framework.data.global.get_mut::<Subscriptions>().unwrap();
379 match subscriptions.sync_one(&id, &client, image_index, download_thumbnails) { 381 match subscriptions.sync_one(&id, &client, image_index, download_thumbnails, &syncing) {
380 Ok(()) => { 382 Ok(()) => {
381 *framework.data.global.get_mut::<Message>().unwrap() = 383 *framework.data.global.get_mut::<Message>().unwrap() =
382 Message::Success(String::from("Channel synced")); 384 Message::Success(String::from("Channel synced"));
@@ -447,16 +449,22 @@ pub fn run_single_command(
447 let mainconfig = framework.data.global.get::<MainConfig>().unwrap(); 449 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
448 let image_index = mainconfig.image_index; 450 let image_index = mainconfig.image_index;
449 let download_thumbnails = mainconfig.images.display(); 451 let download_thumbnails = mainconfig.images.display();
452 let syncing = mainconfig.syncing;
450 453
451 let (success, failed) = framework 454 let (success, failed, empty, cached) = framework
452 .data 455 .data
453 .global 456 .global
454 .get_mut::<Subscriptions>() 457 .get_mut::<Subscriptions>()
455 .unwrap() 458 .unwrap()
456 .sync(&client, image_index, download_thumbnails); 459 .sync(&client, image_index, download_thumbnails, syncing);
457 460
458 *framework.data.global.get_mut::<Message>().unwrap() = Message::Success(format!( 461 *framework.data.global.get_mut::<Message>().unwrap() = Message::Success(format!(
459 "Subscriptions synced: {success} success | {failed} fail" 462 "Subscriptions synced: {success} success{} | {failed} fail | {cached} cached",
463 if empty != 0 {
464 format!(" (which {empty} empty)")
465 } else {
466 String::new()
467 }
460 )); 468 ));
461 469
462 framework 470 framework
src/global/structs/subscriptions.rs +71 −34
@@ -1,8 +1,9 @@
1use super::*; 1use super::*;
2use crate::global::{ 2use crate::{global::{
3 functions::{download_all_images, DownloadRequest}, 3 functions::{download_all_images, DownloadRequest},
4 traits::{Collection, CollectionItem}, 4 traits::{Collection, CollectionItem},
5}; 5}, config::SyncConfig};
6use chrono::Utc;
6use home::home_dir; 7use home::home_dir;
7use serde::*; 8use serde::*;
8use std::{ 9use std::{
@@ -30,30 +31,52 @@ impl Subscriptions {
30 client: &InvidiousClient, 31 client: &InvidiousClient,
31 image_index: usize, 32 image_index: usize,
32 download_thumbnails: bool, 33 download_thumbnails: bool,
33 ) -> (u32, u32) { 34 syncconfig: SyncConfig
35 ) -> (u32, u32, u32, u32) {
34 let failed = Arc::new(AtomicU32::new(0)); 36 let failed = Arc::new(AtomicU32::new(0));
35 let success = Arc::new(AtomicU32::new(0)); 37 let success = Arc::new(AtomicU32::new(0));
38 let empty = Arc::new(AtomicU32::new(0));
39 let cached = Arc::new(AtomicU32::new(0));
36 40
37 let len = self.0.len(); 41 let len = self.0.len();
42 let now = chrono::Utc::now().timestamp() as u64;
38 let (tx, rx) = mpsc::channel(); 43 let (tx, rx) = mpsc::channel();
39 let mut channels = Vec::new(); 44 let mut channels = Vec::new();
40 std::mem::swap(&mut self.0, &mut channels); 45 std::mem::swap(&mut self.0, &mut channels);
41 46
42 channels.into_iter().for_each(|mut item| { 47 channels.into_iter().for_each(|mut item| {
48 if item.last_sync > now - syncconfig.sync_videos_cooldown_secs {
49 tx.send(item).unwrap();
50 cached.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
51 return;
52 }
43 let tx = tx.clone(); 53 let tx = tx.clone();
44 let client = client.clone(); 54 let client = client.clone();
45 let success = success.clone(); 55 let success = success.clone();
46 let failed = failed.clone(); 56 let failed = failed.clone();
57 let empty = empty.clone();
47 thread::spawn(move || { 58 thread::spawn(move || {
48 let res = sync_one(&item.channel.id, &client, image_index, download_thumbnails); 59 let res = sync_one(&item.channel.id, &client, image_index, download_thumbnails, syncconfig.sync_channel_info && syncconfig.sync_channel_cooldown_secs + item.last_sync_channel < now);
49 match res { 60 match res {
50 Ok(synced) => { 61 Ok((videos, _channel)) if videos.is_empty() => {
62 empty.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
63 }
64 Ok((videos, channel)) => {
51 success.fetch_add(1, std::sync::atomic::Ordering::Relaxed); 65 success.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
52 item = synced; 66 // cannot just compare video publish timestamp to sync timestamp
67 // because publish timestamp is hugely inaccurate seen here
68 // https://github.com/iv-org/invidious/issues/570
69 item.has_new = !videos.is_empty() && (item.videos.is_empty() || videos[0].id != item.videos[0].id);
70 item.videos = item.videos;
71 item.last_sync = now;
72
73 if let Some(channel) = channel {
74 item.channel = channel;
75 item.last_sync_channel = now;
76 }
53 } 77 }
54 Err(_) => { 78 _ => {
55 failed.fetch_add(1, std::sync::atomic::Ordering::Relaxed); 79 failed.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
56 item.videos.clear()
57 } 80 }
58 } 81 }
59 tx.send(item) 82 tx.send(item)
@@ -71,6 +94,8 @@ impl Subscriptions {
71 ( 94 (
72 success.load(std::sync::atomic::Ordering::Relaxed), 95 success.load(std::sync::atomic::Ordering::Relaxed),
73 failed.load(std::sync::atomic::Ordering::Relaxed), 96 failed.load(std::sync::atomic::Ordering::Relaxed),
97 empty.load(std::sync::atomic::Ordering::Relaxed),
98 cached.load(std::sync::atomic::Ordering::Relaxed),
74 ) 99 )
75 } 100 }
76 101
@@ -80,20 +105,28 @@ impl Subscriptions {
80 client: &InvidiousClient, 105 client: &InvidiousClient,
81 image_index: usize, 106 image_index: usize,
82 download_thumbnails: bool, 107 download_thumbnails: bool,
108 syncconfig: &SyncConfig
83 ) -> Result<(), Box<dyn Error>> { 109 ) -> Result<(), Box<dyn Error>> {
84 let synced = sync_one(id, client, image_index, download_thumbnails)?; 110 let now = Utc::now().timestamp() as u64;
85 for item in self.0.iter_mut() { 111 match self.0.iter_mut().find(|item| &item.channel.id == id) {
86 if *item == synced { 112 Some(item) => {
87 *item = SubItem { 113 let (videos, channel) = sync_one(&id, &client, image_index, download_thumbnails, syncconfig.sync_channel_info && syncconfig.sync_channel_cooldown_secs + item.last_sync_channel < now)?;
88 // synced.has_new = !synced.videos.is_empty 114
89 has_new: synced.has_new && synced.videos.first() != item.videos.first(), 115 item.has_new = !videos.is_empty() && (item.videos.is_empty() || videos[0].id != item.videos[0].id);
90 ..synced 116 item.videos = videos;
91 }; 117 item.last_sync = now;
92 return Ok(()); 118 if let Some(channel) = channel {
119 item.channel = channel;
120 item.last_sync_channel = now;
121 }
122 }
123 None => {
124 let (videos, channel) = sync_one(&id, &client, image_index, download_thumbnails, true)?;
125 self.0.push(SubItem { channel: channel.unwrap(), videos, last_sync: now, last_sync_channel: now, has_new: true })
126
93 } 127 }
94 } 128 }
95 129
96 self.0.push(synced);
97 self.0.sort(); 130 self.0.sort();
98 131
99 self.save()?; 132 self.save()?;
@@ -129,9 +162,11 @@ fn sync_one(
129 client: &InvidiousClient, 162 client: &InvidiousClient,
130 image_index: usize, 163 image_index: usize,
131 download_thumbnails: bool, 164 download_thumbnails: bool,
132) -> Result<SubItem, Box<dyn Error>> { 165 sync_channel_info: bool,
133 let (tx, rx) = mpsc::sync_channel(1); 166) -> Result<(Vec<MiniVideoItem>, Option<FullChannelItem>), Box<dyn Error>> {
167 let (tx, rx) = mpsc::channel();
134 168
169 if sync_channel_info {
135 let client2 = client.clone(); 170 let client2 = client.clone();
136 let id2 = id.to_string(); 171 let id2 = id.to_string();
137 thread::spawn(move || { 172 thread::spawn(move || {
@@ -143,6 +178,7 @@ fn sync_one(
143 .ok(), 178 .ok(),
144 ) 179 )
145 }); 180 });
181 }
146 let mut videos = client 182 let mut videos = client
147 .0 183 .0
148 .channel_videos(id, None)? 184 .channel_videos(id, None)?
@@ -156,10 +192,16 @@ fn sync_one(
156 .collect::<Vec<_>>(); 192 .collect::<Vec<_>>();
157 videos.sort(); 193 videos.sort();
158 194
159 let channel = rx 195 let channel =
196
197 if sync_channel_info {
198 Some(rx
160 .recv() 199 .recv()
161 .unwrap() 200 .unwrap()
162 .ok_or(Errors::StrError("failed to get channel info {id}"))??; 201 .ok_or(Errors::StrError("failed to get channel info {id}"))??)
202 } else {
203 None
204 };
163 205
164 if download_thumbnails { 206 if download_thumbnails {
165 let thumbnails: Vec<Option<DownloadRequest>> = videos 207 let thumbnails: Vec<Option<DownloadRequest>> = videos
@@ -170,25 +212,19 @@ fn sync_one(
170 id: video.id.clone(), 212 id: video.id.clone(),
171 }) 213 })
172 }) 214 })
173 .chain(
174 [Some(DownloadRequest {
175 url: channel.thumbnail_url.clone(),
176 id: channel.id.clone(),
177 })]
178 .into_iter(),
179 )
180 .collect::<Vec<_>>(); 215 .collect::<Vec<_>>();
181 thread::spawn(move || { 216 thread::spawn(move || {
182 download_all_images(thumbnails); 217 download_all_images(thumbnails);
183 }); 218 });
219 if let Some(channel) = &channel {
220 download_all_images(vec![Some(DownloadRequest { url: channel.thumbnail_url.clone(), id: channel.id.clone()})]);
221 }
184 } 222 }
185 223
186 Ok(SubItem { 224 Ok((
187 channel,
188 has_new: !videos.is_empty(),
189 videos, 225 videos,
190 last_sync: chrono::Utc::now().timestamp() as u64, 226 channel
191 }) 227 ))
192} 228}
193 229
194#[derive(Clone, Serialize, Deserialize)] 230#[derive(Clone, Serialize, Deserialize)]
@@ -196,6 +232,7 @@ pub struct SubItem {
196 pub channel: FullChannelItem, 232 pub channel: FullChannelItem,
197 pub videos: Vec<MiniVideoItem>, 233 pub videos: Vec<MiniVideoItem>,
198 pub last_sync: u64, 234 pub last_sync: u64,
235 pub last_sync_channel: u64,
199 pub has_new: bool, 236 pub has_new: bool,
200} 237}
201 238