src/global/structs/invidiousclient.rs
107 lines · 2660 bytes
1use invidious::{ClientSync, ClientSyncTrait};
2
3use crate::global::traits::SearchProviderTrait;
4
5// used in `data.global`
6/// Holds the invidious client
7#[derive(Clone)]
8pub struct InvidiousClient(pub ClientSync);
9
10impl InvidiousClient {
11 pub fn new(instance: String) -> Self {
12 Self(ClientSync::default().instance(instance))
13 }
14}
15
16impl SearchProviderTrait for InvidiousClient {
17 fn supports_video(&self) -> bool {
18 true
19 }
20
21 fn video(&self, id: &str) -> Result<invidious::video::Video, Box<dyn std::error::Error>> {
22 self.0.video(id, None).map_err(|e| e.into())
23 }
24
25 fn supports_search(&self) -> bool {
26 true
27 }
28
29 fn search(
30 &self,
31 filters: &crate::config::Search,
32 ) -> Result<Vec<invidious::hidden::SearchItem>, Box<dyn std::error::Error>> {
33 self.0
34 .search(Some(&filters.to_string()))
35 .map(|res| res.items)
36 .map_err(|e| e.into())
37 }
38
39 fn supports_channel(&self) -> bool {
40 true
41 }
42
43 fn channel(&self, id: &str) -> Result<invidious::channel::Channel, Box<dyn std::error::Error>> {
44 self.0.channel(id, None).map_err(|e| e.into())
45 }
46
47 fn supports_popular(&self) -> bool {
48 true
49 }
50
51 fn popular(&self) -> Result<Vec<invidious::hidden::PopularItem>, Box<dyn std::error::Error>> {
52 self.0
53 .popular(None)
54 .map(|res| res.items)
55 .map_err(|e| e.into())
56 }
57
58 fn supports_trending(&self) -> bool {
59 true
60 }
61
62 fn trending(&self) -> Result<Vec<invidious::CommonVideo>, Box<dyn std::error::Error>> {
63 self.0
64 .trending(None)
65 .map(|res| res.videos)
66 .map_err(|e| e.into())
67 }
68
69 fn supports_playlist(&self) -> bool {
70 true
71 }
72
73 fn playlist(
74 &self,
75 id: &str,
76 ) -> Result<invidious::universal::Playlist, Box<dyn std::error::Error>> {
77 self.0.playlist(id, None).map_err(|e| e.into())
78 }
79
80 fn supports_channel_videos(&self) -> bool {
81 true
82 }
83
84 fn channel_videos(
85 &self,
86 id: &str,
87 ) -> Result<Vec<invidious::CommonVideo>, Box<dyn std::error::Error>> {
88 self.0
89 .channel_videos(id, None)
90 .map(|res| res.videos)
91 .map_err(|e| e.into())
92 }
93
94 fn supports_channel_playlists(&self) -> bool {
95 true
96 }
97
98 fn channel_playlists(
99 &self,
100 id: &str,
101 ) -> Result<Vec<invidious::CommonPlaylist>, Box<dyn std::error::Error>> {
102 self.0
103 .channel_playlists(id, None)
104 .map(|res| res.playlists)
105 .map_err(|e| e.into())
106 }
107}