From 06fff8e972500dd15583ccb78707d4c5ba07eb83 Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Tue, 31 May 2022 23:13:28 +0200 Subject: [PATCH] feat: finish first method draft implementation --- src/engine.rs | 62 ++++++++++++++++++++++++++++++++++++++-------- src/main.rs | 4 +-- src/spotify/mod.rs | 26 +++++++++---------- 3 files changed, 67 insertions(+), 25 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 83f0cfa..08b2174 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,3 +1,4 @@ +use crate::spotify::ContentKind; use crate::{spotify, youtube}; use chrono::{Date, Utc}; use std::time::Duration; @@ -7,26 +8,27 @@ pub(crate) enum MusicData { Album(Album), } +#[derive(Debug, Clone)] pub(crate) struct Track { name: String, authors: Vec, duration: Duration, - album: Vec, - description: String, - lyrics: String, + album: Option, + description: Option, + lyrics: Option, } +#[derive(Debug, Clone)] pub(crate) struct Album { name: String, authors: Vec, - description: String, - year: Date, + description: Option, + year: Option>, } +#[derive(Debug, Clone)] pub(crate) struct Author { name: String, - surname: String, - date_of_birth: Date, } // The enum holds all the currently supported type of Id which the engine can search for @@ -64,13 +66,53 @@ impl MusicEngine { 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) { + + pub(crate) async fn search_by_id(&self, id: ServiceIdKind) -> Option { match id { ServiceIdKind::Spotify(id) => { let entry_kind = spotify::get_entry_kind(id.as_str()); + match entry_kind { + None => None, + Some(entry) => match entry { + ContentKind::Track(id) => { + self.spotify.get_track(id.as_str()).await.map(|track| { + MusicData::Track(Track { + name: track.name.clone(), + authors: track + .artists + .iter() + .map(|artist| Author { + name: artist.clone(), + }) + .collect(), + duration: track.duration.clone(), + album: None, // FIXME missing metadata + description: None, // FIXME missing metadata + lyrics: None, // FIXME missing metadata + }) + }) + } + ContentKind::Album(id) => { + self.spotify.get_album(id.as_str()).await.map(|album| { + MusicData::Album(Album { + name: album.name.clone(), + authors: album + .artists + .iter() + .map(|artist| Author { + name: artist.clone(), + }) + .collect(), + description: None, // FIXME missing metadata + year: None, // FIXME missing metadata + }) + }) + } + _ => None, + }, + } } - ServiceIdKind::Youtube(id) => {} - ServiceIdKind::Automatic(id) => {} + _ => todo!("Search type not present yet"), //TODO implement } } } diff --git a/src/main.rs b/src/main.rs index 01fa125..4932ca4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,9 @@ use std::env; use teloxide::prelude::*; use crate::spotify::{PlayableKind, TrackInfo}; -use spotify::SpotifyKind::Track; +use spotify::ContentKind::Track; -use crate::spotify::SpotifyKind::{Album, Episode, Playlist, Podcast}; +use crate::spotify::ContentKind::{Album, Episode, Playlist, Podcast}; use crate::utils::{human_readable_duration, truncate_with_dots}; mod engine; diff --git a/src/spotify/mod.rs b/src/spotify/mod.rs index 6635de9..a392137 100644 --- a/src/spotify/mod.rs +++ b/src/spotify/mod.rs @@ -6,7 +6,7 @@ use std::any::Any; use std::sync::Arc; use std::time::Duration; -pub enum SpotifyKind { +pub enum ContentKind { Track(String), Album(String), Playlist(String), @@ -81,7 +81,7 @@ impl Client { artists: track.artists.iter().map(|x| x.name.clone()).collect(), duration: track.duration, }), - Err(_e) => None, + Err(_e) => return None, } } @@ -177,74 +177,74 @@ fn get_id_in_uri(uri: &str) -> Option<&str> { uri.rsplit(':').next() } -pub fn get_entry_kind(uri: &str) -> Option { +pub fn get_entry_kind(uri: &str) -> Option { 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())), + Some(id) => Some(ContentKind::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())), + Some(id) => Some(ContentKind::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())), + Some(id) => Some(ContentKind::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())), + Some(id) => Some(ContentKind::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())), + Some(id) => Some(ContentKind::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())), + Some(id) => Some(ContentKind::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())), + Some(id) => Some(ContentKind::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())), + Some(id) => Some(ContentKind::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())), + Some(id) => Some(ContentKind::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())), + Some(id) => Some(ContentKind::Episode(id.to_string())), None => None, }; }