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 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<Author>,
duration: Duration,
album: Vec<Album>,
description: String,
lyrics: String,
album: Option<Album>,
description: Option<String>,
lyrics: Option<String>,
}
#[derive(Debug, Clone)]
pub(crate) struct Album {
name: String,
authors: Vec<Author>,
description: String,
year: Date<Utc>,
description: Option<String>,
year: Option<Date<Utc>>,
}
#[derive(Debug, Clone)]
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
@ -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<MusicData> {
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
})
})
}
ServiceIdKind::Youtube(id) => {}
ServiceIdKind::Automatic(id) => {}
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,
},
}
}
_ => todo!("Search type not present yet"), //TODO implement
}
}
}

View File

@ -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;

View File

@ -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<SpotifyKind> {
pub fn get_entry_kind(uri: &str) -> Option<ContentKind> {
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,
};
}