Commit 57e6337180

57e633718089527b1f7a720af055c43c9d221481

parent: c07a23d21c

Unsigned

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

added subscribtions struct

Layout: unified · split

Cargo.lock +1
@@ -2057,6 +2057,7 @@ dependencies = [
20572057 "futures",
20582058 "home",
20592059 "invidious",
2060 "rayon",
20602061 "reqwest",
20612062 "serde",
20622063 "serde_json",
docs/src/changelogs.md +1
@@ -5,6 +5,7 @@
55### Added
66
77- Options for config loading. (`write_config` in `main.yml`)
8- Parrallel/blocking commands `parrun`/`run`
89
910### Fixed
1011
docs/src/contributors.md +4
@@ -8,6 +8,10 @@ Creator and main developer of YouTube TUI.
88
99Maintainer of the <a href="https://aur.archlinux.org/packages/youtube-tui" target=_blank>`youtube-tui`</a> package on AUR.
1010
11## <a href="https://github.com/Ruixi-rebirth" target=_blank>Ruixi-rebirth</a>
12
13Maintainer of the <a href="https://search.nixos.org/packages?channel=unstable&query=youtube-tui" >`youtube-tui`</a> on the NixOS repository.
14
1115## <a href="https://github.com/zaedus" target=_blank>Zaedus</a>
1216
1317Maintainer of the <a href="https://aur.archlinux.org/packages/youtube-tui-full-bin" target=_blank>`youtube-tui-full-bin`</a>, <a href="https://aur.archlinux.org/packages/youtube-tui-nodefaults-bin" target=_blank>`youtube-tui-nodefaults-bin`</a> and <a href="https://aur.archlinux.org/packages/youtube-tui-git" target=_blank>`youtube-tui-git`</a> package on AUR.
docs/src/installation.md −2
@@ -1,7 +1,5 @@
11# Installation
22
3> Currently YouTube TUI requires `libxcb`, which should come preinstalled in most Linux system.
4
53The YouTube TUI is *not* tested for Windows.
64
75## Install from Crates.io (recommended)
src/config/commands.rs +6 −6
@@ -141,15 +141,15 @@ fn saved_video_default() -> Vec<HashMap<String, String>> {
141141 )]),
142142 HashMap::from([(
143143 String::from("[Offline] Play saved file"),
144 String::from("parrun find ${save-path} | grep ${id} | ${video-player} --playlist=- --force-window"),
144 String::from("parrun ${video-player} ${save-path}*${id}* --force-window"),
145145 )]),
146146 HashMap::from([(
147147 String::from("[Offline] Play saved file (audio)"),
148 String::from("parrun ${terminal-emulator} bash -c 'find ${save-path} | grep ${id} | ${video-player} --playlist=- --no-video'"),
148 String::from("parrun ${terminal-emulator} ${video-player} ${save-path}*${id}* --no-video"),
149149 )]),
150150 HashMap::from([(
151151 String::from("[Offline] Play saved file (audio loop)"),
152 String::from("parrun ${terminal-emulator} bash -c 'find ${save-path} | grep ${id} | ${video-player} --playlist=- --no-video --loop'"),
152 String::from("parrun ${terminal-emulator} ${video-player} ${save-path}*${id}* --no-video --loop"),
153153 )]),
154154 HashMap::from([(
155155 String::from("View channel"),
@@ -233,15 +233,15 @@ fn saved_playlist_default() -> Vec<HashMap<String, String>> {
233233 )]),
234234 HashMap::from([(
235235 String::from("[Offline] Play all (videos)"),
236 String::from("parrun find ${save-path} | grep ${id} | ${video-player} --playlist=- --force-window"),
236 String::from("parrun ${terminal-emulator} bash -c '${video-player} ${save-path}*${id}*/* --force-window'"),
237237 )]),
238238 HashMap::from([(
239239 String::from("[Offline] Play all (audio)"),
240 String::from("parrun ${terminal-emulator} bash -c 'find ${save-path} | grep ${id} | ${video-player} --playlist=- --no-video'"),
240 String::from("parrun ${terminal-emulator} bash -c '${video-player} ${save-path}*${id}*/* --no-video'"),
241241 )]),
242242 HashMap::from([(
243243 String::from("[Offline] Shuffle play all (audio loop)"),
244 String::from("parrun ${terminal-emulator} bash -c 'find ${save-path} | grep ${id} | ${video-player} --playlist=- --no-video --loop-playlist=inf --shuffle'"),
244 String::from("parrun ${terminal-emulator} bash -c '${video-player} ${save-path}*${id}*/* --no-video --loop-playlist=inf --shuffle'"),
245245 )]),
246246 HashMap::from([(
247247 String::from("View channel"),
src/global/structs/item.rs +52
@@ -37,6 +37,8 @@ pub struct MiniVideoItem {
3737 pub description: Option<String>,
3838}
3939
40unsafe impl Send for MiniVideoItem {}
41
4042/// stores information of a previewed playlist
4143#[derive(Clone, Serialize, Deserialize)]
4244pub struct MiniPlaylistItem {
@@ -196,6 +198,56 @@ impl Item {
196198 }
197199}
198200
201
202impl Item {
203 /// stript self into a FullVideoItem
204 pub fn into_fullvideo(self) -> Result<FullVideoItem, Errors> {
205 match self {
206 Self::FullVideo(fullvideo) => Ok(fullvideo),
207 _ => Err(Errors::StrError("not a full video")),
208 }
209 }
210
211 /// stript self into a MiniVideoItem
212 pub fn into_minivideo(self) -> Result<MiniVideoItem, Errors> {
213 match self {
214 Self::MiniVideo(minivideo) => Ok(minivideo),
215 _ => Err(Errors::StrError("not a mini video")),
216 }
217 }
218
219 /// stript self into a MiniPlaylistItem
220 pub fn into_miniplaylist(self) -> Result<MiniPlaylistItem, Errors> {
221 match self {
222 Self::MiniPlaylist(miniplaylist) => Ok(miniplaylist),
223 _ => Err(Errors::StrError("not a mini playlist")),
224 }
225 }
226
227 /// stript self into a FullPlaylistItem
228 pub fn into_fullplaylist(self) -> Result<FullPlaylistItem, Errors> {
229 match self {
230 Self::FullPlaylist(fullplaylist) => Ok(fullplaylist),
231 _ => Err(Errors::StrError("not a full playlist")),
232 }
233 }
234
235 /// stript self into a MiniChannelItem
236 pub fn into_minichannel(self) -> Result<MiniChannelItem, Errors> {
237 match self {
238 Self::MiniChannel(minichannel) => Ok(minichannel),
239 _ => Err(Errors::StrError("not a mini channel")),
240 }
241 }
242
243 pub fn into_fullchannel(self) -> Result<FullChannelItem, Errors> {
244 match self {
245 Self::FullChannel(fullchannel) => Ok(fullchannel),
246 _ => Err(Errors::StrError("not a full channel")),
247 }
248 }
249}
250
199251impl Item {
200252 /// returns the file save path for thumbnails (which is by their id)
201253 /// returns `invalid` if the item does not have a thumbnail
src/global/structs/mod.rs +2
@@ -10,6 +10,7 @@ mod state_env;
1010mod status;
1111mod tasks;
1212mod watchhistory;
13mod subscriptions;
1314
1415pub use errors::*;
1516pub use invidiousclient::*;
@@ -22,3 +23,4 @@ pub use state_env::*;
2223pub use status::*;
2324pub use tasks::*;
2425pub use watchhistory::*;
26pub use subscriptions::*;
src/global/structs/subscriptions.rs added +49
@@ -0,0 +1,49 @@
1use std::{collections::HashMap, error::Error, sync::mpsc, thread};
2use serde::*;
3// use rayon::prelude::*;
4use crate::config::MainConfig;
5use super::{MiniVideoItem, InvidiousClient, Item};
6
7#[derive(Serialize, Deserialize)]
8/// id, videos
9pub struct Subscriptions(pub HashMap<String, Vec<MiniVideoItem>>);
10
11impl Subscriptions {
12 /// Res<(success, failed)>
13 pub fn sync(&mut self, client: &InvidiousClient, mainconfig: &MainConfig) -> (u32, u32) {
14 let mut failed: u32 = 0;
15 let mut success: u32 = 0;
16
17 let (tx, rx) = mpsc::channel();
18 let image_index = mainconfig.image_index;
19
20 self.0.keys().for_each(|k| {
21 let tx = tx.clone();
22 let client = client.clone();
23 let k = k.clone();
24 thread::spawn(move || {
25 let res = sync_one(&k, &client, image_index).ok();
26 tx.send((k, res))
27 });
28 });
29
30 for res in rx {
31 match res {
32 (id, Some(videos)) => {
33 self.0.insert(id, videos);
34 success += 1;
35 },
36 (id, None) => {
37 self.0.get_mut(&id).unwrap().clear();
38 failed += 1;
39 }
40 }
41 }
42
43 (success, failed)
44 }
45}
46
47fn sync_one(id: &str, client: &InvidiousClient, image_index: usize) -> Result<Vec<MiniVideoItem>, Box<dyn Error>> {
48 Ok(client.0.channel_videos(id, None)?.videos.into_iter().map(|video| Item::from_channel_video(video, image_index).into_minivideo().unwrap()).collect())
49}