Skip to main content

musicou_kit/structs/
mod.rs

1use serde::Serialize;
2
3#[derive(Debug, PartialEq, Clone, Serialize)]
4
5pub struct Album {
6    pub name: String,
7    pub id: String,
8    pub platform: Platform,
9    pub release: String, //ep, signle
10    pub items: Vec<Song>,
11    pub year: String,
12    pub thumnails: Vec<Thumbnail>,
13    pub incomplete: bool,
14    pub description: String,
15    pub artists: Vec<String>, // TODO change to Vec<Artist>
16}
17impl Album {
18    pub fn new() -> Album {
19        Album {
20            name: "".to_string(),
21            id: "".to_string(),
22            platform: Platform::Youtube,
23            release: "".to_string(),
24            description: "".to_string(),
25            items: vec![],
26            year: "".to_string(),
27            thumnails: vec![],
28            artists: vec![],
29            incomplete: true,
30        }
31    }
32    pub fn from_id(id: &str) -> Album {
33        Album {
34            name: "".to_string(),
35            id: id.to_string(),
36            platform: Platform::Youtube,
37            release: "".to_string(),
38            description: "".to_string(),
39            items: vec![],
40            year: "".to_string(),
41            thumnails: vec![],
42            artists: vec![],
43            incomplete: true,
44        }
45    }
46}
47
48#[derive(Debug, PartialEq, Clone, Serialize)]
49pub struct Playlist {
50    pub incomplete: bool,
51    pub name: String,
52    pub id: String,
53    pub platform: Platform,
54    pub thumbnail: Vec<Thumbnail>,
55    // TODO implement Artist / User instead of owner_id + owner
56    pub owner_id: String,
57    pub owner: String,
58
59    pub items: Vec<Song>,
60}
61impl Playlist {
62    //
63    pub fn new() -> Playlist {
64        //
65        Playlist {
66            name: "".to_string(),
67            incomplete: true,
68            id: "".to_string(),
69            owner: "".to_string(),
70            owner_id: "".to_string(),
71            platform: Platform::Youtube,
72            thumbnail: vec![],
73            items: vec![],
74        }
75    }
76    pub fn from_id(id: &str) -> Playlist {
77        Playlist {
78            name: "".to_string(),
79            incomplete: true,
80            id: id.to_string(),
81            owner: "".to_string(),
82            owner_id: "".to_string(),
83            platform: Platform::Youtube,
84            thumbnail: vec![],
85            items: vec![],
86        }
87    }
88    // this is implemented so if the items ever were changed to Option<Vec<Song>> then we only have to edit this fucntion
89    pub fn add_song(&mut self, song: Song) {
90        self.items.push(song);
91    }
92}
93
94#[derive(Debug, PartialEq, Clone, Serialize)]
95pub struct Song {
96    pub id: String,
97    pub title: String,
98    pub length: i64, // seconds
99    pub author: Artist,
100    pub thumbnails: Vec<Thumbnail>,
101    pub view_count: i64,
102    pub streams: Option<Vec<Stream>>,
103    pub album: Option<Album>, // album that its part of
104    // TODO add related songs Vec<Song>
105    pub platform: Platform, // TODO rename everythin to platfrom
106    pub incomplete: bool,
107    // TODO explicit
108}
109impl Song {
110    pub fn new() -> Song {
111        //crta
112        Song {
113            title: "".to_string(),
114            length: 0,
115            author: Artist::new(),
116            album: None,
117            thumbnails: vec![],
118            view_count: 0,
119            streams: Some(vec![]),
120            id: "".to_string(),
121            platform: Platform::Youtube,
122            incomplete: true,
123        }
124    }
125    pub fn from_id(id: &str) -> Song {
126        Song {
127            title: "".to_string(),
128            length: 0,
129            author: Artist::new(),
130            album: None,
131            thumbnails: vec![],
132            view_count: 0,
133            streams: Some(vec![]),
134            id: id.to_string(),
135            platform: Platform::Youtube,
136            incomplete: true,
137        }
138    }
139    pub fn add_stream(&mut self, stream: Stream) {
140        match self.streams {
141            None => self.streams = Some(vec![stream]),
142            Some(ref mut streams) => streams.push(stream),
143        }
144    }
145}
146#[derive(Debug, PartialEq, Clone, Serialize)]
147pub struct Artist {
148    pub id: String,
149    pub name: String,
150    pub platform: Platform,
151    pub thumbnails: Vec<Thumbnail>,
152    pub background: Vec<Thumbnail>,
153    pub incomplete: bool,
154    pub followers: i64,
155}
156impl Artist {
157    pub fn new() -> Artist {
158        //
159        Artist {
160            id: "".to_string(),
161            name: "".to_string(),
162            thumbnails: vec![],
163            background: vec![],
164            incomplete: true,
165            followers: 0,
166            platform: Platform::Youtube,
167        }
168    }
169    pub fn from_id(id: &str) -> Artist {
170        //
171        Artist {
172            id: id.to_string(),
173            name: "".to_string(),
174            thumbnails: vec![],
175            background: vec![],
176            incomplete: true,
177            followers: 0,
178            platform: Platform::Youtube,
179        }
180    }
181}
182
183#[derive(Debug, PartialEq, Clone, Serialize)]
184pub struct Stream {
185    pub url: String,
186    pub bitrate: String,
187    pub mime_type: String,
188    // TODO contentLength
189    // TODO audioQuality
190    // TODO audioSampleRate
191    // TODO audioChannels
192    // TODO quality
193}
194impl Stream {
195    pub fn new() -> Stream {
196        Stream {
197            url: "".to_string(),
198            bitrate: "".to_string(),
199            mime_type: "".to_string(),
200        }
201    }
202}
203#[derive(Debug, PartialEq, Clone, Serialize)]
204pub struct Thumbnail {
205    pub url: String,
206    pub width: i64,
207    pub height: i64,
208}
209impl Thumbnail {
210    pub fn new() -> Thumbnail {
211        Thumbnail {
212            url: "".to_string(),
213            height: 0,
214            width: 0,
215        }
216    }
217}
218#[derive(Debug, PartialEq, Clone, Serialize)]
219pub enum Platform {
220    Youtube,
221    Bandcamp,
222    Soundcloud,
223}
224
225#[derive(Debug, Serialize)]
226pub enum Error {
227    FailedToExtractData(String),
228    FailedToFetch,
229    CannotConvertToType(String),
230    InvalidResponseFromVendor,
231    DataNotFound,
232}
233#[derive(Debug, PartialEq, Clone, Serialize)]
234pub enum Page {
235    Song,
236    Album,
237    Playlist,
238    Artist,
239}
240
241#[derive(Debug, Clone, Serialize)]
242pub struct Search {
243    pub songs: Option<Vec<Song>>,
244    songs_continuation: Option<String>,
245
246    pub albums: Option<Vec<Album>>,
247    albums_continuation: Option<String>,
248
249    pub playlists: Option<Vec<Playlist>>,
250    playlists_continuation: Option<String>,
251
252    pub artists: Option<Vec<Artist>>,
253    artists_continuation: Option<String>,
254
255    pub platform: Platform,
256    pub query: String,
257}
258
259impl Search {
260    // TODO add add_song() and if the song already is in the list then dont add.
261
262    pub fn new(query: &str, platform: Platform) -> Search {
263        Search {
264            songs: None,
265            songs_continuation: None,
266
267            albums: None,
268            albums_continuation: None,
269
270            playlists: None,
271            playlists_continuation: None,
272
273            artists: None,
274            artists_continuation: None,
275
276            platform,
277            // Todo rewrite to str
278            query: query.to_string(),
279        }
280    }
281
282    pub fn set_continuation(&mut self, param: String, page: Page) {
283        match page {
284            Page::Song => self.songs_continuation = Some(param),
285            Page::Album => self.albums_continuation = Some(param),
286            Page::Playlist => self.playlists_continuation = Some(param),
287            Page::Artist => self.artists_continuation = Some(param),
288        }
289    }
290    pub fn get_continuation(&mut self, page: Page) -> Option<String> {
291        //
292        match page {
293            Page::Album => return self.albums_continuation.clone(),
294            Page::Artist => return self.artists_continuation.clone(),
295            Page::Song => return self.songs_continuation.clone(),
296            Page::Playlist => return self.playlists_continuation.clone(),
297        }
298    }
299    pub fn add_artist(&mut self, art: Artist) {
300        match self.artists {
301            Some(ref mut artists) => artists.push(art),
302            None => self.artists = Some(vec![art]),
303        }
304    }
305
306    pub fn add_album(&mut self, album: Album) {
307        match self.albums {
308            Some(ref mut albums) => albums.push(album),
309            None => self.albums = Some(vec![album]),
310        }
311    }
312
313    pub fn add_playlist(&mut self, playlist: Playlist) {
314        match self.playlists {
315            Some(ref mut playlists) => playlists.push(playlist),
316            None => self.playlists = Some(vec![playlist]),
317        }
318    }
319}
320pub enum YtSearchType {
321    Basic(String),
322    Continuation(String, String),
323    Final(String),
324}