feat: finish first method draft implementation

pull/8/head
Davide Polonio 2022-05-31 23:13:28 +02:00
parent 469991d20b
commit 06fff8e972
3 changed files with 67 additions and 25 deletions

View File

@ -1,3 +1,4 @@
use crate::spotify::ContentKind;
use crate::{spotify, youtube}; use crate::{spotify, youtube};
use chrono::{Date, Utc}; use chrono::{Date, Utc};
use std::time::Duration; use std::time::Duration;
@ -7,26 +8,27 @@ pub(crate) enum MusicData {
Album(Album), Album(Album),
} }
#[derive(Debug, Clone)]
pub(crate) struct Track { pub(crate) struct Track {
name: String, name: String,
authors: Vec<Author>, authors: Vec<Author>,
duration: Duration, duration: Duration,
album: Vec<Album>, album: Option<Album>,
description: String, description: Option<String>,
lyrics: String, lyrics: Option<String>,
} }
#[derive(Debug, Clone)]
pub(crate) struct Album { pub(crate) struct Album {
name: String, name: String,
authors: Vec<Author>, authors: Vec<Author>,
description: String, description: Option<String>,
year: Date<Utc>, year: Option<Date<Utc>>,
} }
#[derive(Debug, Clone)]
pub(crate) struct Author { pub(crate) struct Author {
name: String, 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 // 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) { 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") 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<MusicData> {
match id { match id {
ServiceIdKind::Spotify(id) => { ServiceIdKind::Spotify(id) => {
let entry_kind = spotify::get_entry_kind(id.as_str()); 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) => {} _ => todo!("Search type not present yet"), //TODO implement
ServiceIdKind::Automatic(id) => {}
} }
} }
} }

View File

@ -4,9 +4,9 @@ use std::env;
use teloxide::prelude::*; use teloxide::prelude::*;
use crate::spotify::{PlayableKind, TrackInfo}; 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}; use crate::utils::{human_readable_duration, truncate_with_dots};
mod engine; mod engine;

View File

@ -6,7 +6,7 @@ use std::any::Any;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
pub enum SpotifyKind { pub enum ContentKind {
Track(String), Track(String),
Album(String), Album(String),
Playlist(String), Playlist(String),
@ -81,7 +81,7 @@ impl Client {
artists: track.artists.iter().map(|x| x.name.clone()).collect(), artists: track.artists.iter().map(|x| x.name.clone()).collect(),
duration: track.duration, 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() uri.rsplit(':').next()
} }
pub fn get_entry_kind(uri: &str) -> Option<SpotifyKind> { pub fn get_entry_kind(uri: &str) -> Option<ContentKind> {
if uri.contains("spotify:track:") { if uri.contains("spotify:track:") {
let track_id = get_id_in_uri(uri); let track_id = get_id_in_uri(uri);
return match track_id { return match track_id {
Some(id) => Some(SpotifyKind::Track(id.to_string())), Some(id) => Some(ContentKind::Track(id.to_string())),
None => None, None => None,
}; };
} }
if uri.contains("https://open.spotify.com/track/") { if uri.contains("https://open.spotify.com/track/") {
let track_id = get_id_in_url(uri); let track_id = get_id_in_url(uri);
return match track_id { return match track_id {
Some(id) => Some(SpotifyKind::Track(id.to_string())), Some(id) => Some(ContentKind::Track(id.to_string())),
None => None, None => None,
}; };
} }
if uri.contains("spotify:album:") { if uri.contains("spotify:album:") {
let track_id = get_id_in_uri(uri); let track_id = get_id_in_uri(uri);
return match track_id { return match track_id {
Some(id) => Some(SpotifyKind::Album(id.to_string())), Some(id) => Some(ContentKind::Album(id.to_string())),
None => None, None => None,
}; };
} }
if uri.contains("https://open.spotify.com/album/") { if uri.contains("https://open.spotify.com/album/") {
let album_id = get_id_in_url(uri); let album_id = get_id_in_url(uri);
return match album_id { return match album_id {
Some(id) => Some(SpotifyKind::Album(id.to_string())), Some(id) => Some(ContentKind::Album(id.to_string())),
None => None, None => None,
}; };
} }
if uri.contains("spotify:playlist:") { if uri.contains("spotify:playlist:") {
let track_id = get_id_in_uri(uri); let track_id = get_id_in_uri(uri);
return match track_id { return match track_id {
Some(id) => Some(SpotifyKind::Album(id.to_string())), Some(id) => Some(ContentKind::Album(id.to_string())),
None => None, None => None,
}; };
} }
if uri.contains("https://open.spotify.com/playlist/") { if uri.contains("https://open.spotify.com/playlist/") {
let playlist_id = get_id_in_url(uri); let playlist_id = get_id_in_url(uri);
return match playlist_id { return match playlist_id {
Some(id) => Some(SpotifyKind::Playlist(id.to_string())), Some(id) => Some(ContentKind::Playlist(id.to_string())),
None => None, None => None,
}; };
} }
if uri.contains("spotify:show:") { if uri.contains("spotify:show:") {
let track_id = get_id_in_uri(uri); let track_id = get_id_in_uri(uri);
return match track_id { return match track_id {
Some(id) => Some(SpotifyKind::Album(id.to_string())), Some(id) => Some(ContentKind::Album(id.to_string())),
None => None, None => None,
}; };
} }
if uri.contains("https://open.spotify.com/show/") { if uri.contains("https://open.spotify.com/show/") {
let playlist_id = get_id_in_url(uri); let playlist_id = get_id_in_url(uri);
return match playlist_id { return match playlist_id {
Some(id) => Some(SpotifyKind::Podcast(id.to_string())), Some(id) => Some(ContentKind::Podcast(id.to_string())),
None => None, None => None,
}; };
} }
if uri.contains("spotify:episode:") { if uri.contains("spotify:episode:") {
let track_id = get_id_in_uri(uri); let track_id = get_id_in_uri(uri);
return match track_id { return match track_id {
Some(id) => Some(SpotifyKind::Album(id.to_string())), Some(id) => Some(ContentKind::Album(id.to_string())),
None => None, None => None,
}; };
} }
if uri.contains("https://open.spotify.com/episode/") { if uri.contains("https://open.spotify.com/episode/") {
let playlist_id = get_id_in_url(uri); let playlist_id = get_id_in_url(uri);
return match playlist_id { return match playlist_id {
Some(id) => Some(SpotifyKind::Episode(id.to_string())), Some(id) => Some(ContentKind::Episode(id.to_string())),
None => None, None => None,
}; };
} }