feat: add youtube search #8
							
								
								
									
										19
									
								
								.idea/runConfigurations/Run_all_tests.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								.idea/runConfigurations/Run_all_tests.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							| @ -0,0 +1,19 @@ | |||||||
|  | <component name="ProjectRunConfigurationManager"> | ||||||
|  |   <configuration default="false" name="Run all tests" type="CargoCommandRunConfiguration" factoryName="Cargo Command"> | ||||||
|  |     <option name="command" value="test" /> | ||||||
|  |     <option name="workingDirectory" value="file://$PROJECT_DIR$" /> | ||||||
|  |     <option name="channel" value="DEFAULT" /> | ||||||
|  |     <option name="requiredFeatures" value="true" /> | ||||||
|  |     <option name="allFeatures" value="false" /> | ||||||
|  |     <option name="emulateTerminal" value="false" /> | ||||||
|  |     <option name="withSudo" value="false" /> | ||||||
|  |     <option name="buildTarget" value="REMOTE" /> | ||||||
|  |     <option name="backtrace" value="SHORT" /> | ||||||
|  |     <envs /> | ||||||
|  |     <option name="isRedirectInput" value="false" /> | ||||||
|  |     <option name="redirectInputPath" value="" /> | ||||||
|  |     <method v="2"> | ||||||
|  |       <option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" /> | ||||||
|  |     </method> | ||||||
|  |   </configuration> | ||||||
|  | </component> | ||||||
| @ -13,3 +13,7 @@ tokio = { version =  "1.18.2", features = ["rt-multi-thread", "macros"] } | |||||||
| rspotify = { version = "0.11.5", features = ["default"]} | rspotify = { version = "0.11.5", features = ["default"]} | ||||||
| sentry = "0.26.0" | sentry = "0.26.0" | ||||||
| invidious = "0.2.1" | invidious = "0.2.1" | ||||||
|  | chrono = "0.4.19" | ||||||
|  | 
 | ||||||
|  | [dev-dependencies] | ||||||
|  | tokio-test = "0.4.2" | ||||||
|  | |||||||
							
								
								
									
										76
									
								
								src/engine.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								src/engine.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,76 @@ | |||||||
|  | use crate::{spotify, youtube}; | ||||||
|  | use chrono::{Date, Utc}; | ||||||
|  | use std::time::Duration; | ||||||
|  | 
 | ||||||
|  | pub(crate) enum MusicData { | ||||||
|  |     Track(Track), | ||||||
|  |     Album(Album), | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub(crate) struct Track { | ||||||
|  |     name: String, | ||||||
|  |     authors: Vec<Author>, | ||||||
|  |     duration: Duration, | ||||||
|  |     album: Vec<Album>, | ||||||
|  |     description: String, | ||||||
|  |     lyrics: String, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub(crate) struct Album { | ||||||
|  |     name: String, | ||||||
|  |     authors: Vec<Author>, | ||||||
|  |     description: String, | ||||||
|  |     year: Date<Utc>, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub(crate) struct Author { | ||||||
|  |     name: String, | ||||||
|  |     surname: String, | ||||||
|  |     date_of_birth: Date<Utc>, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // The enum holds all the currently supported type of Id which the engine can search for
 | ||||||
|  | pub(crate) enum ServiceIdKind { | ||||||
|  |     Spotify(String), | ||||||
|  |     Youtube(String), | ||||||
|  |     Automatic(String), | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // This struct will allow us in the future to search, cache and store data and metadata regarding
 | ||||||
|  | // tracks, albums and playlists
 | ||||||
|  | pub(crate) struct MusicEngine { | ||||||
|  |     spotify: spotify::Client, | ||||||
|  |     youtube: youtube::Client, | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | impl MusicEngine { | ||||||
|  |     pub(crate) async fn new() -> Self { | ||||||
|  |         MusicEngine { | ||||||
|  |             spotify: spotify::Client::new().await, | ||||||
|  |             youtube: youtube::Client::new().await, | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub(crate) fn new_with_dependencies( | ||||||
|  |         spotify_client: spotify::Client, | ||||||
|  |         youtube_client: youtube::Client, | ||||||
|  |     ) -> Self { | ||||||
|  |         MusicEngine { | ||||||
|  |             spotify: spotify_client, | ||||||
|  |             youtube: youtube_client, | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub(crate) async fn search_by_name(&self, name: &str) { | ||||||
|  |         todo!("In the future it would be possible to search for all metadata on a record from this call") | ||||||
|  |     } | ||||||
|  |     pub(crate) async fn search_by_id(&self, id: ServiceIdKind) { | ||||||
|  |         match id { | ||||||
|  |             ServiceIdKind::Spotify(id) => { | ||||||
|  |                 let entry_kind = spotify::get_entry_kind(id.as_str()); | ||||||
|  |             } | ||||||
|  |             ServiceIdKind::Youtube(id) => {} | ||||||
|  |             ServiceIdKind::Automatic(id) => {} | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										38
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -9,6 +9,7 @@ use spotify::SpotifyKind::Track; | |||||||
| use crate::spotify::SpotifyKind::{Album, Episode, Playlist, Podcast}; | use crate::spotify::SpotifyKind::{Album, Episode, Playlist, Podcast}; | ||||||
| use crate::utils::{human_readable_duration, truncate_with_dots}; | use crate::utils::{human_readable_duration, truncate_with_dots}; | ||||||
| 
 | 
 | ||||||
|  | mod engine; | ||||||
| mod spotify; | mod spotify; | ||||||
| mod utils; | mod utils; | ||||||
| mod youtube; | mod youtube; | ||||||
| @ -50,11 +51,11 @@ async fn main() { | |||||||
|         let text = message.text().and_then(spotify::get_entry_kind); |         let text = message.text().and_then(spotify::get_entry_kind); | ||||||
|         match text { |         match text { | ||||||
|             Some(spotify) => { |             Some(spotify) => { | ||||||
|                 let spotify_client = spotify::get_client().await; |                 let spotify_client = spotify::Client::new().await; | ||||||
|                 match spotify { |                 match spotify { | ||||||
|                     Track(id) => { |                     Track(id) => { | ||||||
|                         log::debug!("Parsing spotify song: {}", id); |                         log::debug!("Parsing spotify song: {}", id); | ||||||
|                         let track_info = spotify::get_track(spotify_client, &id).await; |                         let track_info = spotify_client.get_track(&id).await; | ||||||
|                         match track_info { |                         match track_info { | ||||||
|                             Some(info) => { |                             Some(info) => { | ||||||
|                                 let reply = format!( |                                 let reply = format!( | ||||||
| @ -76,7 +77,7 @@ async fn main() { | |||||||
|                     } |                     } | ||||||
|                     Album(id) => { |                     Album(id) => { | ||||||
|                         log::debug!("Parsing spotify album: {}", id); |                         log::debug!("Parsing spotify album: {}", id); | ||||||
|                         let album_info = spotify::get_album(spotify_client, &id).await; |                         let album_info = spotify_client.get_album(&id).await; | ||||||
|                         match album_info { |                         match album_info { | ||||||
|                             Some(info) => { |                             Some(info) => { | ||||||
|                                 let mut reply = format!( |                                 let mut reply = format!( | ||||||
| @ -103,7 +104,7 @@ async fn main() { | |||||||
|                     } |                     } | ||||||
|                     Playlist(id) => { |                     Playlist(id) => { | ||||||
|                         log::debug!("Parsing spotify playlist: {}", id); |                         log::debug!("Parsing spotify playlist: {}", id); | ||||||
|                         let playlist_info = spotify::get_playlist(spotify_client, &id).await; |                         let playlist_info = spotify_client.get_playlist(&id).await; | ||||||
|                         match playlist_info { |                         match playlist_info { | ||||||
|                             Some(info) => { |                             Some(info) => { | ||||||
|                                 let reply = format!( |                                 let reply = format!( | ||||||
| @ -140,32 +141,3 @@ async fn main() { | |||||||
| 
 | 
 | ||||||
|     log::info!("Exiting..."); |     log::info!("Exiting..."); | ||||||
| } | } | ||||||
| 
 |  | ||||||
| fn add_track_section_for_playlist(tracks: Vec<PlayableKind>, reply: String) -> String { |  | ||||||
|     if !tracks.is_empty() { |  | ||||||
|         let songs = tracks |  | ||||||
|             .iter() |  | ||||||
|             .map(|x| match x { |  | ||||||
|                 PlayableKind::Track(t) => t.name.clone(), |  | ||||||
|                 PlayableKind::Podcast(e) => e.name.clone() |  | ||||||
|             } + "\n") |  | ||||||
|             .collect::<String>(); |  | ||||||
|         reply |  | ||||||
|             .clone() |  | ||||||
|             .push_str(format!("\n🎶 {} Track(s): {}", tracks.len(), songs).as_str()) |  | ||||||
|     } |  | ||||||
|     reply |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| fn add_track_section(tracks: Vec<TrackInfo>, reply: String) -> String { |  | ||||||
|     if !tracks.is_empty() { |  | ||||||
|         let songs = tracks |  | ||||||
|             .iter() |  | ||||||
|             .map(|x| x.name.clone() + "\n") |  | ||||||
|             .collect::<String>(); |  | ||||||
|         reply |  | ||||||
|             .clone() |  | ||||||
|             .push_str(format!("\n🎶 {} Track(s): {}", tracks.len(), songs).as_str()) |  | ||||||
|     } |  | ||||||
|     reply |  | ||||||
| } |  | ||||||
|  | |||||||
| @ -2,6 +2,8 @@ use rspotify::model::PlayableItem::{Episode, Track}; | |||||||
| use rspotify::model::{AlbumId, FullTrack, PlaylistId, TrackId}; | use rspotify::model::{AlbumId, FullTrack, PlaylistId, TrackId}; | ||||||
| use rspotify::prelude::*; | use rspotify::prelude::*; | ||||||
| use rspotify::{ClientCredsSpotify, Credentials}; | use rspotify::{ClientCredsSpotify, Credentials}; | ||||||
|  | use std::any::Any; | ||||||
|  | use std::sync::Arc; | ||||||
| use std::time::Duration; | use std::time::Duration; | ||||||
| 
 | 
 | ||||||
| pub enum SpotifyKind { | pub enum SpotifyKind { | ||||||
| @ -45,74 +47,35 @@ pub struct PlaylistInfo { | |||||||
|     pub(crate) tracks: Vec<PlayableKind>, |     pub(crate) tracks: Vec<PlayableKind>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn get_id_in_url(url: &str) -> Option<&str> { | #[derive(Clone, Debug)] | ||||||
|     url.rsplit('/') | pub(crate) struct Client { | ||||||
|         .next() |     client: Arc<ClientCredsSpotify>, | ||||||
|         .and_then(|x| x.split(' ').next()) |  | ||||||
|         .and_then(|x| x.split('?').next()) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn extract_artists_from_tracks(tracks: Vec<FullTrack>) -> Vec<String> { | impl Client { | ||||||
|     tracks |     pub(crate) async fn new() -> Self { | ||||||
|         .iter() |         let spotify_creds = Credentials::from_env() | ||||||
|         .flat_map(|t| t.artists.iter().map(|a| a.name.clone())) |             .expect("RSPOTIFY_CLIENT_ID and RSPOTIFY_CLIENT_SECRET not found."); | ||||||
|         .collect() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub fn get_entry_kind(url: &str) -> Option<SpotifyKind> { |  | ||||||
|     if url.contains("https://open.spotify.com/track/") { |  | ||||||
|         let track_id = get_id_in_url(url); |  | ||||||
|         return match track_id { |  | ||||||
|             Some(id) => Some(SpotifyKind::Track(id.to_string())), |  | ||||||
|             None => None, |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
|     if url.contains("https://open.spotify.com/album/") { |  | ||||||
|         let album_id = get_id_in_url(url); |  | ||||||
|         return match album_id { |  | ||||||
|             Some(id) => Some(SpotifyKind::Album(id.to_string())), |  | ||||||
|             None => None, |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
|     if url.contains("https://open.spotify.com/playlist/") { |  | ||||||
|         let playlist_id = get_id_in_url(url); |  | ||||||
|         return match playlist_id { |  | ||||||
|             Some(id) => Some(SpotifyKind::Playlist(id.to_string())), |  | ||||||
|             None => None, |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
|     if url.contains("https://open.spotify.com/show/") { |  | ||||||
|         let playlist_id = get_id_in_url(url); |  | ||||||
|         return match playlist_id { |  | ||||||
|             Some(id) => Some(SpotifyKind::Podcast(id.to_string())), |  | ||||||
|             None => None, |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
|     if url.contains("https://open.spotify.com/episode/") { |  | ||||||
|         let playlist_id = get_id_in_url(url); |  | ||||||
|         return match playlist_id { |  | ||||||
|             Some(id) => Some(SpotifyKind::Episode(id.to_string())), |  | ||||||
|             None => None, |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
|     return None; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub async fn get_client() -> Box<ClientCredsSpotify> { |  | ||||||
|     let spotify_creds = |  | ||||||
|         Credentials::from_env().expect("RSPOTIFY_CLIENT_ID and RSPOTIFY_CLIENT_SECRET not found."); |  | ||||||
|         let mut spotify = ClientCredsSpotify::new(spotify_creds); |         let mut spotify = ClientCredsSpotify::new(spotify_creds); | ||||||
|         spotify.request_token().await.unwrap(); |         spotify.request_token().await.unwrap(); | ||||||
|     Box::new(spotify) |         Client { | ||||||
|  |             client: Arc::new(spotify), | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| pub async fn get_track(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<TrackInfo> { |     pub(crate) fn new_with_dependencies(client: ClientCredsSpotify) -> Self { | ||||||
|     let track_id = match TrackId::from_id(id.as_str()) { |         Client { | ||||||
|  |             client: Arc::new(client), | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub async fn get_track(&self, id: &str) -> Option<TrackInfo> { | ||||||
|  |         let track_id = match TrackId::from_id(id) { | ||||||
|             Ok(track) => track, |             Ok(track) => track, | ||||||
|             Err(_e) => return None, |             Err(_e) => return None, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|     match spotify.track(&track_id).await { |         match self.client.track(&track_id).await { | ||||||
|             Ok(track) => Some(TrackInfo { |             Ok(track) => Some(TrackInfo { | ||||||
|                 name: track.name, |                 name: track.name, | ||||||
|                 artists: track.artists.iter().map(|x| x.name.clone()).collect(), |                 artists: track.artists.iter().map(|x| x.name.clone()).collect(), | ||||||
| @ -122,13 +85,13 @@ pub async fn get_track(spotify: Box<ClientCredsSpotify>, id: &String) -> Option< | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| pub async fn get_album(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<AlbumInfo> { |     pub async fn get_album(&self, id: &str) -> Option<AlbumInfo> { | ||||||
|     let album_id = match AlbumId::from_id(id.as_str()) { |         let album_id = match AlbumId::from_id(id) { | ||||||
|             Ok(album) => album, |             Ok(album) => album, | ||||||
|             Err(_e) => return None, |             Err(_e) => return None, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|     match spotify.album(&album_id).await { |         match self.client.album(&album_id).await { | ||||||
|             Ok(album) => Some(AlbumInfo { |             Ok(album) => Some(AlbumInfo { | ||||||
|                 name: album.name, |                 name: album.name, | ||||||
|                 artists: album.artists.iter().map(|x| x.name.clone()).collect(), |                 artists: album.artists.iter().map(|x| x.name.clone()).collect(), | ||||||
| @ -148,13 +111,13 @@ pub async fn get_album(spotify: Box<ClientCredsSpotify>, id: &String) -> Option< | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| pub async fn get_playlist(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<PlaylistInfo> { |     pub async fn get_playlist(&self, id: &String) -> Option<PlaylistInfo> { | ||||||
|         let playlist_id = match PlaylistId::from_id(id.as_str()) { |         let playlist_id = match PlaylistId::from_id(id.as_str()) { | ||||||
|             Ok(playlist) => playlist, |             Ok(playlist) => playlist, | ||||||
|             Err(_e) => return None, |             Err(_e) => return None, | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|     match spotify.playlist(&playlist_id, None, None).await { |         match self.client.playlist(&playlist_id, None, None).await { | ||||||
|             Ok(playlist) => Some(PlaylistInfo { |             Ok(playlist) => Some(PlaylistInfo { | ||||||
|                 name: playlist.name, |                 name: playlist.name, | ||||||
|                 artists: playlist |                 artists: playlist | ||||||
| @ -201,3 +164,89 @@ pub async fn get_playlist(spotify: Box<ClientCredsSpotify>, id: &String) -> Opti | |||||||
|             Err(_e) => None, |             Err(_e) => None, | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn get_id_in_url(url: &str) -> Option<&str> { | ||||||
|  |     url.rsplit('/') | ||||||
|  |         .next() | ||||||
|  |         .and_then(|x| x.split(' ').next()) | ||||||
|  |         .and_then(|x| x.split('?').next()) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn get_id_in_uri(uri: &str) -> Option<&str> { | ||||||
|  |     uri.rsplit(':').next() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn get_entry_kind(uri: &str) -> Option<SpotifyKind> { | ||||||
|  |     if uri.contains("spotify:track:") { | ||||||
|  |         let track_id = get_id_in_uri(uri); | ||||||
|  |         return match track_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Track(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     if uri.contains("https://open.spotify.com/track/") { | ||||||
|  |         let track_id = get_id_in_url(uri); | ||||||
|  |         return match track_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Track(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     if uri.contains("spotify:album:") { | ||||||
|  |         let track_id = get_id_in_uri(uri); | ||||||
|  |         return match track_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Album(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     if uri.contains("https://open.spotify.com/album/") { | ||||||
|  |         let album_id = get_id_in_url(uri); | ||||||
|  |         return match album_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Album(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     if uri.contains("spotify:playlist:") { | ||||||
|  |         let track_id = get_id_in_uri(uri); | ||||||
|  |         return match track_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Album(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     if uri.contains("https://open.spotify.com/playlist/") { | ||||||
|  |         let playlist_id = get_id_in_url(uri); | ||||||
|  |         return match playlist_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Playlist(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     if uri.contains("spotify:show:") { | ||||||
|  |         let track_id = get_id_in_uri(uri); | ||||||
|  |         return match track_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Album(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     if uri.contains("https://open.spotify.com/show/") { | ||||||
|  |         let playlist_id = get_id_in_url(uri); | ||||||
|  |         return match playlist_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Podcast(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     if uri.contains("spotify:episode:") { | ||||||
|  |         let track_id = get_id_in_uri(uri); | ||||||
|  |         return match track_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Album(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     if uri.contains("https://open.spotify.com/episode/") { | ||||||
|  |         let playlist_id = get_id_in_url(uri); | ||||||
|  |         return match playlist_id { | ||||||
|  |             Some(id) => Some(SpotifyKind::Episode(id.to_string())), | ||||||
|  |             None => None, | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  |     return None; | ||||||
|  | } | ||||||
|  | |||||||
| @ -1,10 +1,5 @@ | |||||||
| use invidious::asynchronous::Client; |  | ||||||
| use std::error::Error; | use std::error::Error; | ||||||
| 
 | use std::sync::Arc; | ||||||
| #[derive(Debug, Clone)] |  | ||||||
| pub(crate) struct YoutubeClient { |  | ||||||
|     client: Client, |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| pub(crate) struct VideoSearch { | pub(crate) struct VideoSearch { | ||||||
|     items: Vec<Video>, |     items: Vec<Video>, | ||||||
| @ -22,7 +17,7 @@ pub(crate) struct Video { | |||||||
|     viewCount: u64, |     viewCount: u64, | ||||||
|     published: u64, |     published: u64, | ||||||
|     publishedText: String, |     publishedText: String, | ||||||
|     liveNow: bool, |     live_now: bool, | ||||||
|     paid: bool, |     paid: bool, | ||||||
|     premium: bool, |     premium: bool, | ||||||
| } | } | ||||||
| @ -34,15 +29,28 @@ const BY_RATING: &SearchSortBy = "rating"; | |||||||
| const BY_UPLOAD_DATE: &SearchSortBy = "upload_date"; | const BY_UPLOAD_DATE: &SearchSortBy = "upload_date"; | ||||||
| const BY_VIEW_COUNT: &SearchSortBy = "view_count"; | const BY_VIEW_COUNT: &SearchSortBy = "view_count"; | ||||||
| 
 | 
 | ||||||
| impl YoutubeClient { | #[derive(Debug, Clone)] | ||||||
|     async fn new() -> YoutubeClient { | pub(crate) struct Client { | ||||||
|         // TODO check for a stable instance
 |     client: Arc<invidious::asynchronous::Client>, | ||||||
|         let client = Client::new(String::from("https://vid.puffyan.us")); |  | ||||||
| 
 |  | ||||||
|         YoutubeClient { client } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|     async fn search_video( | impl Client { | ||||||
|  |     pub(crate) async fn new() -> Self { | ||||||
|  |         // TODO check for a stable instance
 | ||||||
|  |         let client = invidious::asynchronous::Client::new(String::from("https://vid.puffyan.us")); | ||||||
|  | 
 | ||||||
|  |         Client { | ||||||
|  |             client: Arc::new(client), | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub(crate) fn new_with_dependencies(client: invidious::asynchronous::Client) -> Self { | ||||||
|  |         Client { | ||||||
|  |             client: Arc::new(client), | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub async fn search_video( | ||||||
|         &self, |         &self, | ||||||
|         keyword: &str, |         keyword: &str, | ||||||
|         sort_by: Option<&SearchSortBy>, |         sort_by: Option<&SearchSortBy>, | ||||||
| @ -69,7 +77,7 @@ impl YoutubeClient { | |||||||
|                     authorId, |                     authorId, | ||||||
|                     authorUrl, |                     authorUrl, | ||||||
|                     lengthSeconds, |                     lengthSeconds, | ||||||
|                     videoThumbnails, |                     videoThumbnails: _, | ||||||
|                     description, |                     description, | ||||||
|                     descriptionHtml, |                     descriptionHtml, | ||||||
|                     viewCount, |                     viewCount, | ||||||
| @ -90,7 +98,7 @@ impl YoutubeClient { | |||||||
|                     viewCount: viewCount.clone(), |                     viewCount: viewCount.clone(), | ||||||
|                     published: published.clone(), |                     published: published.clone(), | ||||||
|                     publishedText: publishedText.clone(), |                     publishedText: publishedText.clone(), | ||||||
|                     liveNow: liveNow.clone(), |                     live_now: liveNow.clone(), | ||||||
|                     paid: paid.clone(), |                     paid: paid.clone(), | ||||||
|                     premium: premium.clone(), |                     premium: premium.clone(), | ||||||
|                 }), |                 }), | ||||||
| @ -98,9 +106,26 @@ impl YoutubeClient { | |||||||
|             }) |             }) | ||||||
|             .filter(|x| x.is_some()) |             .filter(|x| x.is_some()) | ||||||
|             .map(|x| x.unwrap()) |             .map(|x| x.unwrap()) | ||||||
|             .filter(|x| !x.premium && !x.paid && !x.liveNow) |             .filter(|x| !x.premium && !x.paid && !x.live_now) | ||||||
|             .collect(); |             .collect(); | ||||||
| 
 | 
 | ||||||
|         Result::Ok(VideoSearch { items: videos }) |         Result::Ok(VideoSearch { items: videos }) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | #[cfg(test)] | ||||||
|  | mod tests { | ||||||
|  |     use super::*; | ||||||
|  | 
 | ||||||
|  |     #[test] | ||||||
|  |     fn should_search_properly() { | ||||||
|  |         let client = tokio_test::block_on(Client::new()); | ||||||
|  | 
 | ||||||
|  |         let result = tokio_test::block_on(client.search_video( | ||||||
|  |             "vfdskvnfdsjklvnfdsjklvnfsdjkldsvmdlfmvkdfslvsdfmklsdvlvfdnjkvnfdsjkvnfsdjk", | ||||||
|  |             Some(BY_UPLOAD_DATE), | ||||||
|  |         )) | ||||||
|  |         .unwrap(); | ||||||
|  |         assert_eq!(30, result.items.len()) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user