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 @@
55[![](https://img.shields.io/aur/version/youtube-tui)](https://aur.archlinux.org/packages/youtube-tui)
66![](https://shields.io/github/stars/siriusmart/youtube-tui?style=social)
77
8***Read more in the [user manual](https://siriusmart.github.io/youtube-tui) (including installation guide and documentation).***
9
810# Overview
911
1012Written 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 @@
11# Changelogs
22
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)
415
516### Added
617
@@ -9,7 +20,7 @@
920
1021### Fixed
1122
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.
1324
1425### v0.7.1
1526
src/config/main.rs +34
@@ -22,6 +22,7 @@ pub struct MainConfig {
2222 pub images: Images,
2323 #[serde(default = "refresh_after_modifying_search_filters_default")]
2424 pub refresh_after_modifying_search_filters: bool,
25 pub syncing: SyncConfig,
2526 #[serde(default = "image_index_default")]
2627 // The image to download from the array of images provided by the invidious api
2728 // 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 {
5253 image_index: image_index_default(),
5354 refresh_after_modifying_search_filters: refresh_after_modifying_search_filters_default(
5455 ),
56 syncing: sync_config_default(),
5557 provider: provider_default(),
5658 shell: shell_default(),
5759
@@ -64,6 +66,22 @@ impl ConfigTrait for MainConfig {
6466 const LABEL: &'static str = "main";
6567}
6668
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
6785/// how images are handled/displayed
6886#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
6987pub enum Images {
@@ -176,3 +194,19 @@ fn default_env() -> HashMap<String, String> {
176194fn shell_default() -> String {
177195 String::from("sh")
178196}
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(
361361 }
362362 };
363363
364 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
365 let image_index = mainconfig.image_index;
366 let download_thumbnails = mainconfig.images.display();
367364 let client = framework
368365 .data
369366 .global
@@ -375,8 +372,13 @@ pub fn run_single_command(
375372 Message::Message(String::from("Syncing..."));
376373 terminal.draw(|frame| framework.render(frame)).unwrap();
377374
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
378380 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) {
380382 Ok(()) => {
381383 *framework.data.global.get_mut::<Message>().unwrap() =
382384 Message::Success(String::from("Channel synced"));
@@ -447,16 +449,22 @@ pub fn run_single_command(
447449 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
448450 let image_index = mainconfig.image_index;
449451 let download_thumbnails = mainconfig.images.display();
452 let syncing = mainconfig.syncing;
450453
451 let (success, failed) = framework
454 let (success, failed, empty, cached) = framework
452455 .data
453456 .global
454457 .get_mut::<Subscriptions>()
455458 .unwrap()
456 .sync(&client, image_index, download_thumbnails);
459 .sync(&client, image_index, download_thumbnails, syncing);
457460
458461 *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 }
460468 ));
461469
462470 framework
src/global/structs/subscriptions.rs +71 −34
@@ -1,8 +1,9 @@
11use super::*;
2use crate::global::{
2use crate::{global::{
33 functions::{download_all_images, DownloadRequest},
44 traits::{Collection, CollectionItem},
5};
5}, config::SyncConfig};
6use chrono::Utc;
67use home::home_dir;
78use serde::*;
89use std::{
@@ -30,30 +31,52 @@ impl Subscriptions {
3031 client: &InvidiousClient,
3132 image_index: usize,
3233 download_thumbnails: bool,
33 ) -> (u32, u32) {
34 syncconfig: SyncConfig
35 ) -> (u32, u32, u32, u32) {
3436 let failed = Arc::new(AtomicU32::new(0));
3537 let success = Arc::new(AtomicU32::new(0));
38 let empty = Arc::new(AtomicU32::new(0));
39 let cached = Arc::new(AtomicU32::new(0));
3640
3741 let len = self.0.len();
42 let now = chrono::Utc::now().timestamp() as u64;
3843 let (tx, rx) = mpsc::channel();
3944 let mut channels = Vec::new();
4045 std::mem::swap(&mut self.0, &mut channels);
4146
4247 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 }
4353 let tx = tx.clone();
4454 let client = client.clone();
4555 let success = success.clone();
4656 let failed = failed.clone();
57 let empty = empty.clone();
4758 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);
4960 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)) => {
5165 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 }
5377 }
54 Err(_) => {
78 _ => {
5579 failed.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
56 item.videos.clear()
5780 }
5881 }
5982 tx.send(item)
@@ -71,6 +94,8 @@ impl Subscriptions {
7194 (
7295 success.load(std::sync::atomic::Ordering::Relaxed),
7396 failed.load(std::sync::atomic::Ordering::Relaxed),
97 empty.load(std::sync::atomic::Ordering::Relaxed),
98 cached.load(std::sync::atomic::Ordering::Relaxed),
7499 )
75100 }
76101
@@ -80,20 +105,28 @@ impl Subscriptions {
80105 client: &InvidiousClient,
81106 image_index: usize,
82107 download_thumbnails: bool,
108 syncconfig: &SyncConfig
83109 ) -> Result<(), Box<dyn Error>> {
84 let synced = sync_one(id, client, image_index, download_thumbnails)?;
85 for item in self.0.iter_mut() {
86 if *item == synced {
87 *item = SubItem {
88 // synced.has_new = !synced.videos.is_empty
89 has_new: synced.has_new && synced.videos.first() != item.videos.first(),
90 ..synced
91 };
92 return Ok(());
110 let now = Utc::now().timestamp() as u64;
111 match self.0.iter_mut().find(|item| &item.channel.id == id) {
112 Some(item) => {
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)?;
114
115 item.has_new = !videos.is_empty() && (item.videos.is_empty() || videos[0].id != item.videos[0].id);
116 item.videos = videos;
117 item.last_sync = now;
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
93127 }
94128 }
95129
96 self.0.push(synced);
97130 self.0.sort();
98131
99132 self.save()?;
@@ -129,9 +162,11 @@ fn sync_one(
129162 client: &InvidiousClient,
130163 image_index: usize,
131164 download_thumbnails: bool,
132) -> Result<SubItem, Box<dyn Error>> {
133 let (tx, rx) = mpsc::sync_channel(1);
165 sync_channel_info: bool,
166) -> Result<(Vec<MiniVideoItem>, Option<FullChannelItem>), Box<dyn Error>> {
167 let (tx, rx) = mpsc::channel();
134168
169 if sync_channel_info {
135170 let client2 = client.clone();
136171 let id2 = id.to_string();
137172 thread::spawn(move || {
@@ -143,6 +178,7 @@ fn sync_one(
143178 .ok(),
144179 )
145180 });
181 }
146182 let mut videos = client
147183 .0
148184 .channel_videos(id, None)?
@@ -156,10 +192,16 @@ fn sync_one(
156192 .collect::<Vec<_>>();
157193 videos.sort();
158194
159 let channel = rx
195 let channel =
196
197 if sync_channel_info {
198 Some(rx
160199 .recv()
161200 .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 };
163205
164206 if download_thumbnails {
165207 let thumbnails: Vec<Option<DownloadRequest>> = videos
@@ -170,25 +212,19 @@ fn sync_one(
170212 id: video.id.clone(),
171213 })
172214 })
173 .chain(
174 [Some(DownloadRequest {
175 url: channel.thumbnail_url.clone(),
176 id: channel.id.clone(),
177 })]
178 .into_iter(),
179 )
180215 .collect::<Vec<_>>();
181216 thread::spawn(move || {
182217 download_all_images(thumbnails);
183218 });
219 if let Some(channel) = &channel {
220 download_all_images(vec![Some(DownloadRequest { url: channel.thumbnail_url.clone(), id: channel.id.clone()})]);
221 }
184222 }
185223
186 Ok(SubItem {
187 channel,
188 has_new: !videos.is_empty(),
224 Ok((
189225 videos,
190 last_sync: chrono::Utc::now().timestamp() as u64,
191 })
226 channel
227 ))
192228}
193229
194230#[derive(Clone, Serialize, Deserialize)]
@@ -196,6 +232,7 @@ pub struct SubItem {
196232 pub channel: FullChannelItem,
197233 pub videos: Vec<MiniVideoItem>,
198234 pub last_sync: u64,
235 pub last_sync_channel: u64,
199236 pub has_new: bool,
200237}
201238