Compare commits

...

2 Commits

6 changed files with 406 additions and 153 deletions

View 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>

View File

@ -12,3 +12,8 @@ pretty_env_logger = "0.4.0"
tokio = { version = "1.18.2", features = ["rt-multi-thread", "macros"] }
rspotify = { version = "0.11.5", features = ["default"]}
sentry = "0.26.0"
invidious = "0.2.1"
chrono = "0.4.19"
[dev-dependencies]
tokio-test = "0.4.2"

76
src/engine.rs Normal file
View 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) => {}
}
}
}

View File

@ -9,8 +9,10 @@ use spotify::SpotifyKind::Track;
use crate::spotify::SpotifyKind::{Album, Episode, Playlist, Podcast};
use crate::utils::{human_readable_duration, truncate_with_dots};
mod engine;
mod spotify;
mod utils;
mod youtube;
static MAX_ARTISTS_CHARS: usize = 140;
@ -49,11 +51,11 @@ async fn main() {
let text = message.text().and_then(spotify::get_entry_kind);
match text {
Some(spotify) => {
let spotify_client = spotify::get_client().await;
let spotify_client = spotify::Client::new().await;
match spotify {
Track(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 {
Some(info) => {
let reply = format!(
@ -75,7 +77,7 @@ async fn main() {
}
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 {
Some(info) => {
let mut reply = format!(
@ -102,7 +104,7 @@ async fn main() {
}
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 {
Some(info) => {
let reply = format!(
@ -139,32 +141,3 @@ async fn main() {
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
}

View File

@ -2,6 +2,8 @@ use rspotify::model::PlayableItem::{Episode, Track};
use rspotify::model::{AlbumId, FullTrack, PlaylistId, TrackId};
use rspotify::prelude::*;
use rspotify::{ClientCredsSpotify, Credentials};
use std::any::Any;
use std::sync::Arc;
use std::time::Duration;
pub enum SpotifyKind {
@ -45,6 +47,125 @@ pub struct PlaylistInfo {
pub(crate) tracks: Vec<PlayableKind>,
}
#[derive(Clone, Debug)]
pub(crate) struct Client {
client: Arc<ClientCredsSpotify>,
}
impl Client {
pub(crate) async fn new() -> Self {
let spotify_creds = Credentials::from_env()
.expect("RSPOTIFY_CLIENT_ID and RSPOTIFY_CLIENT_SECRET not found.");
let mut spotify = ClientCredsSpotify::new(spotify_creds);
spotify.request_token().await.unwrap();
Client {
client: Arc::new(spotify),
}
}
pub(crate) fn new_with_dependencies(client: ClientCredsSpotify) -> Self {
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,
Err(_e) => return None,
};
match self.client.track(&track_id).await {
Ok(track) => Some(TrackInfo {
name: track.name,
artists: track.artists.iter().map(|x| x.name.clone()).collect(),
duration: track.duration,
}),
Err(_e) => None,
}
}
pub async fn get_album(&self, id: &str) -> Option<AlbumInfo> {
let album_id = match AlbumId::from_id(id) {
Ok(album) => album,
Err(_e) => return None,
};
match self.client.album(&album_id).await {
Ok(album) => Some(AlbumInfo {
name: album.name,
artists: album.artists.iter().map(|x| x.name.clone()).collect(),
genres: album.genres,
tracks: album
.tracks
.items
.iter()
.map(|t| TrackInfo {
name: t.name.clone(),
artists: t.artists.iter().map(|x| x.name.clone()).collect(),
duration: t.duration,
})
.collect(),
}),
Err(_e) => None,
}
}
pub async fn get_playlist(&self, id: &String) -> Option<PlaylistInfo> {
let playlist_id = match PlaylistId::from_id(id.as_str()) {
Ok(playlist) => playlist,
Err(_e) => return None,
};
match self.client.playlist(&playlist_id, None, None).await {
Ok(playlist) => Some(PlaylistInfo {
name: playlist.name,
artists: playlist
.tracks
.items
.iter()
.flat_map(|p| {
match &p.track {
Some(t) => match t {
Track(t) => t.artists.iter().map(|a| a.name.clone()).collect(),
Episode(e) => vec![e.show.publisher.clone()],
},
None => Vec::new(),
}
.into_iter()
})
.collect(),
tracks: playlist
.tracks
.items
.iter()
.map(|p| match &p.track {
Some(t) => match t {
Track(t) => Some(PlayableKind::Track(TrackInfo {
name: t.name.clone(),
artists: t.artists.iter().map(|a| a.name.clone()).collect(),
duration: t.duration,
})),
Episode(e) => Some(PlayableKind::Podcast(EpisodeInfo {
name: e.name.clone(),
show: e.show.name.clone(),
duration: e.duration,
description: e.description.clone(),
languages: e.languages.clone(),
release_date: e.release_date.clone(),
})),
},
None => None,
})
.filter(|i| i.is_some())
.map(|i| i.unwrap())
.collect(),
}),
Err(_e) => None,
}
}
}
fn get_id_in_url(url: &str) -> Option<&str> {
url.rsplit('/')
.next()
@ -52,44 +173,76 @@ fn get_id_in_url(url: &str) -> Option<&str> {
.and_then(|x| x.split('?').next())
}
fn extract_artists_from_tracks(tracks: Vec<FullTrack>) -> Vec<String> {
tracks
.iter()
.flat_map(|t| t.artists.iter().map(|a| a.name.clone()))
.collect()
fn get_id_in_uri(uri: &str) -> Option<&str> {
uri.rsplit(':').next()
}
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);
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 url.contains("https://open.spotify.com/album/") {
let album_id = get_id_in_url(url);
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 url.contains("https://open.spotify.com/playlist/") {
let playlist_id = get_id_in_url(url);
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 url.contains("https://open.spotify.com/show/") {
let playlist_id = get_id_in_url(url);
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 url.contains("https://open.spotify.com/episode/") {
let playlist_id = get_id_in_url(url);
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,
@ -97,107 +250,3 @@ pub fn get_entry_kind(url: &str) -> Option<SpotifyKind> {
}
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);
spotify.request_token().await.unwrap();
Box::new(spotify)
}
pub async fn get_track(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<TrackInfo> {
let track_id = match TrackId::from_id(id.as_str()) {
Ok(track) => track,
Err(_e) => return None,
};
match spotify.track(&track_id).await {
Ok(track) => Some(TrackInfo {
name: track.name,
artists: track.artists.iter().map(|x| x.name.clone()).collect(),
duration: track.duration,
}),
Err(_e) => None,
}
}
pub async fn get_album(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<AlbumInfo> {
let album_id = match AlbumId::from_id(id.as_str()) {
Ok(album) => album,
Err(_e) => return None,
};
match spotify.album(&album_id).await {
Ok(album) => Some(AlbumInfo {
name: album.name,
artists: album.artists.iter().map(|x| x.name.clone()).collect(),
genres: album.genres,
tracks: album
.tracks
.items
.iter()
.map(|t| TrackInfo {
name: t.name.clone(),
artists: t.artists.iter().map(|x| x.name.clone()).collect(),
duration: t.duration,
})
.collect(),
}),
Err(_e) => None,
}
}
pub async fn get_playlist(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<PlaylistInfo> {
let playlist_id = match PlaylistId::from_id(id.as_str()) {
Ok(playlist) => playlist,
Err(_e) => return None,
};
match spotify.playlist(&playlist_id, None, None).await {
Ok(playlist) => Some(PlaylistInfo {
name: playlist.name,
artists: playlist
.tracks
.items
.iter()
.flat_map(|p| {
match &p.track {
Some(t) => match t {
Track(t) => t.artists.iter().map(|a| a.name.clone()).collect(),
Episode(e) => vec![e.show.publisher.clone()],
},
None => Vec::new(),
}
.into_iter()
})
.collect(),
tracks: playlist
.tracks
.items
.iter()
.map(|p| match &p.track {
Some(t) => match t {
Track(t) => Some(PlayableKind::Track(TrackInfo {
name: t.name.clone(),
artists: t.artists.iter().map(|a| a.name.clone()).collect(),
duration: t.duration,
})),
Episode(e) => Some(PlayableKind::Podcast(EpisodeInfo {
name: e.name.clone(),
show: e.show.name.clone(),
duration: e.duration,
description: e.description.clone(),
languages: e.languages.clone(),
release_date: e.release_date.clone(),
})),
},
None => None,
})
.filter(|i| i.is_some())
.map(|i| i.unwrap())
.collect(),
}),
Err(_e) => None,
}
}

131
src/youtube/mod.rs Normal file
View File

@ -0,0 +1,131 @@
use std::error::Error;
use std::sync::Arc;
pub(crate) struct VideoSearch {
items: Vec<Video>,
}
pub(crate) struct Video {
title: String,
videoId: String,
author: String,
authorId: String,
authorUrl: String,
lengthSeconds: u64,
description: String,
descriptionHtml: String,
viewCount: u64,
published: u64,
publishedText: String,
live_now: bool,
paid: bool,
premium: bool,
}
type SearchSortBy = str;
const BY_RELEVANCE: &SearchSortBy = "relevance";
const BY_RATING: &SearchSortBy = "rating";
const BY_UPLOAD_DATE: &SearchSortBy = "upload_date";
const BY_VIEW_COUNT: &SearchSortBy = "view_count";
#[derive(Debug, Clone)]
pub(crate) struct Client {
client: Arc<invidious::asynchronous::Client>,
}
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,
keyword: &str,
sort_by: Option<&SearchSortBy>,
) -> Result<VideoSearch, Box<dyn Error>> {
let mut query = Vec::<String>::new();
query.push(format!("{}={}", "q", keyword));
match sort_by {
Some(sorting_type) => query.push(format!("{}={}", "sort_by", sorting_type)),
None => {}
}
query.push(format!("{}={}", "type", "video"));
let generated_query = query.join(",");
let search = self.client.search(Some(generated_query.as_str())).await?;
let videos: Vec<Video> = search
.items
.iter()
.map(|search_result| match search_result {
invidious::structs::hidden::SearchItem::Video {
title,
videoId,
author,
authorId,
authorUrl,
lengthSeconds,
videoThumbnails: _,
description,
descriptionHtml,
viewCount,
published,
publishedText,
liveNow,
paid,
premium,
} => Some(Video {
title: title.clone(),
videoId: videoId.clone(),
author: author.clone(),
authorId: authorId.clone(),
authorUrl: authorUrl.clone(),
lengthSeconds: lengthSeconds.clone(),
description: description.clone(),
descriptionHtml: descriptionHtml.clone(),
viewCount: viewCount.clone(),
published: published.clone(),
publishedText: publishedText.clone(),
live_now: liveNow.clone(),
paid: paid.clone(),
premium: premium.clone(),
}),
_ => None,
})
.filter(|x| x.is_some())
.map(|x| x.unwrap())
.filter(|x| !x.premium && !x.paid && !x.live_now)
.collect();
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())
}
}