| @@ -1,8 +1,9 @@ |
| 1 | 1 | use super::*; |
| 2 | | use crate::global::{ |
| 2 | use crate::{global::{ |
| 3 | 3 | functions::{download_all_images, DownloadRequest}, |
| 4 | 4 | traits::{Collection, CollectionItem}, |
| 5 | | }; |
| 5 | }, config::SyncConfig}; |
| 6 | use chrono::Utc; |
| 6 | 7 | use home::home_dir; |
| 7 | 8 | use serde::*; |
| 8 | 9 | use std::{ |
| @@ -30,30 +31,52 @@ impl Subscriptions { |
| 30 | 31 | client: &InvidiousClient, |
| 31 | 32 | image_index: usize, |
| 32 | 33 | download_thumbnails: bool, |
| 33 | | ) -> (u32, u32) { |
| 34 | syncconfig: SyncConfig |
| 35 | ) -> (u32, u32, u32, u32) { |
| 34 | 36 | let failed = Arc::new(AtomicU32::new(0)); |
| 35 | 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 | 41 | let len = self.0.len(); |
| 42 | let now = chrono::Utc::now().timestamp() as u64; |
| 38 | 43 | let (tx, rx) = mpsc::channel(); |
| 39 | 44 | let mut channels = Vec::new(); |
| 40 | 45 | std::mem::swap(&mut self.0, &mut channels); |
| 41 | 46 | |
| 42 | 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 | 53 | let tx = tx.clone(); |
| 44 | 54 | let client = client.clone(); |
| 45 | 55 | let success = success.clone(); |
| 46 | 56 | let failed = failed.clone(); |
| 57 | let empty = empty.clone(); |
| 47 | 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 | 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 | 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 | 79 | failed.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 56 | | item.videos.clear() |
| 57 | 80 | } |
| 58 | 81 | } |
| 59 | 82 | tx.send(item) |
| @@ -71,6 +94,8 @@ impl Subscriptions { |
| 71 | 94 | ( |
| 72 | 95 | success.load(std::sync::atomic::Ordering::Relaxed), |
| 73 | 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 | 105 | client: &InvidiousClient, |
| 81 | 106 | image_index: usize, |
| 82 | 107 | download_thumbnails: bool, |
| 108 | syncconfig: &SyncConfig |
| 83 | 109 | ) -> 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 | |
| 93 | 127 | } |
| 94 | 128 | } |
| 95 | 129 | |
| 96 | | self.0.push(synced); |
| 97 | 130 | self.0.sort(); |
| 98 | 131 | |
| 99 | 132 | self.save()?; |
| @@ -129,9 +162,11 @@ fn sync_one( |
| 129 | 162 | client: &InvidiousClient, |
| 130 | 163 | image_index: usize, |
| 131 | 164 | 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(); |
| 134 | 168 | |
| 169 | if sync_channel_info { |
| 135 | 170 | let client2 = client.clone(); |
| 136 | 171 | let id2 = id.to_string(); |
| 137 | 172 | thread::spawn(move || { |
| @@ -143,6 +178,7 @@ fn sync_one( |
| 143 | 178 | .ok(), |
| 144 | 179 | ) |
| 145 | 180 | }); |
| 181 | } |
| 146 | 182 | let mut videos = client |
| 147 | 183 | .0 |
| 148 | 184 | .channel_videos(id, None)? |
| @@ -156,10 +192,16 @@ fn sync_one( |
| 156 | 192 | .collect::<Vec<_>>(); |
| 157 | 193 | videos.sort(); |
| 158 | 194 | |
| 159 | | let channel = rx |
| 195 | let channel = |
| 196 | |
| 197 | if sync_channel_info { |
| 198 | Some(rx |
| 160 | 199 | .recv() |
| 161 | 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 | 206 | if download_thumbnails { |
| 165 | 207 | let thumbnails: Vec<Option<DownloadRequest>> = videos |
| @@ -170,25 +212,19 @@ fn sync_one( |
| 170 | 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 | 215 | .collect::<Vec<_>>(); |
| 181 | 216 | thread::spawn(move || { |
| 182 | 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 { |
| 187 | | channel, |
| 188 | | has_new: !videos.is_empty(), |
| 224 | Ok(( |
| 189 | 225 | videos, |
| 190 | | last_sync: chrono::Utc::now().timestamp() as u64, |
| 191 | | }) |
| 226 | channel |
| 227 | )) |
| 192 | 228 | } |
| 193 | 229 | |
| 194 | 230 | #[derive(Clone, Serialize, Deserialize)] |
| @@ -196,6 +232,7 @@ pub struct SubItem { |
| 196 | 232 | pub channel: FullChannelItem, |
| 197 | 233 | pub videos: Vec<MiniVideoItem>, |
| 198 | 234 | pub last_sync: u64, |
| 235 | pub last_sync_channel: u64, |
| 199 | 236 | pub has_new: bool, |
| 200 | 237 | } |
| 201 | 238 | |