Merge pull request 'feat: add youtube search' (#8) from issue#2 into 0.3.x

Reviewed-on: #8
0.3.x
Davide Polonio 2023-11-03 10:10:39 +01:00
commit 571de470d9
11 changed files with 1011 additions and 333 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

@ -1,14 +1,22 @@
[package] [package]
name = "songlify" name = "songlify"
version = "0.3.3" version = "0.3.4"
edition = "2018" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
teloxide = { version = "0.9.1", features = ["auto-send", "macros"] } teloxide = { version = "0.9.2", features = ["auto-send", "macros"] }
log = "0.4.17" log = "0.4.17"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
tokio = { version = "1.18.2", features = ["rt-multi-thread", "macros"] } tokio = { version = "1.20.0", 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.27.0"
invidious = "0.2.1"
chrono = "0.4.19"
itertools = "0.10.3"
async-trait = "0.1.56"
[dev-dependencies]
tokio-test = "0.4.2"
mockall = "0.11.1"

View File

@ -1,9 +1,9 @@
FROM rust:1.59.0-slim-bullseye as builder FROM rust:1.62.0-slim-bullseye as builder
WORKDIR /build WORKDIR /build
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
libssl-dev=1.1.1k-1+deb11u2 \ libssl-dev=1.1.1n-0+deb11u3 \
pkg-config=0.29.2-1 pkg-config=0.29.2-1
COPY ./ /build COPY ./ /build

View File

@ -1,18 +1,17 @@
use log::LevelFilter; use log::{debug, info, LevelFilter};
use search::spotify;
use sentry::ClientInitGuard; use sentry::ClientInitGuard;
use std::env; use std::env;
use teloxide::prelude::*; use teloxide::prelude::*;
use crate::spotify::{PlayableKind, TrackInfo}; use search::spotify::ContentKind::Track;
use spotify::SpotifyKind::Track; use search::spotify::TrackInfo;
use crate::spotify::SpotifyKind::{Album, Episode, Playlist, Podcast}; use crate::search::get_spotify_kind;
use crate::utils::{human_readable_duration, truncate_with_dots}; use search::spotify::ContentKind::{Album, Playlist};
mod spotify; mod search;
mod utils; mod tgformatter;
static MAX_ARTISTS_CHARS: usize = 140;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -46,125 +45,51 @@ async fn main() {
let bot = Bot::from_env().auto_send(); let bot = Bot::from_env().auto_send();
teloxide::repl(bot, |message: Message, bot: AutoSend<Bot>| async move { teloxide::repl(bot, |message: Message, bot: AutoSend<Bot>| async move {
let text = message.text().and_then(spotify::get_entry_kind); let music_engine = search::Engine::new().await;
match text { let opt_text_message = message.text();
Some(spotify) => { if opt_text_message.is_none() {
let spotify_client = spotify::get_client().await; return respond(());
match spotify { }
let text_message = opt_text_message.unwrap();
let content_kind = opt_text_message.and_then(|x| get_spotify_kind(x));
let option_reply = match content_kind {
None => return respond(()),
Some(content) => match content {
Track(id) => { Track(id) => {
log::debug!("Parsing spotify song: {}", id); info!("Processing song with spotify id: {}", id);
let track_info = spotify::get_track(spotify_client, &id).await; let track_item = music_engine.get_song_from_spotify_id(text_message).await;
match track_info { tgformatter::format_track_message(track_item)
Some(info) => {
let reply = format!(
"Track information:\n\
🎵 Track name: {}\n\
🧑🎤 Artist(s): {}\n\
Duration: {}",
info.name,
truncate_with_dots(info.artists.join(", "), MAX_ARTISTS_CHARS),
human_readable_duration(info.duration)
);
bot.send_message(message.chat.id, reply)
.reply_to_message_id(message.id)
.await?;
Some(respond(()))
}
None => None,
}
} }
Album(id) => { Album(id) => {
log::debug!("Parsing spotify album: {}", id); info!("Processing album with spotify id: {}", id);
let album_info = spotify::get_album(spotify_client, &id).await; let album_item = music_engine.get_album_from_spotify_id(text_message).await;
match album_info { tgformatter::format_album_message(album_item)
Some(info) => {
let mut reply = format!(
"Album information:\n\
🎵 Album name: {}\n\
🧑🎤 {} artist(s): {}",
info.name,
info.artists.len(),
truncate_with_dots(info.artists.join(", "), MAX_ARTISTS_CHARS)
);
if !info.genres.is_empty() {
reply.push_str(
format!("\n💿 Genre(s): {}", info.genres.join(", "))
.as_str(),
);
}
bot.send_message(message.chat.id, reply)
.reply_to_message_id(message.id)
.await?;
Some(respond(()))
}
None => None,
}
} }
Playlist(id) => { Playlist(id) => {
log::debug!("Parsing spotify playlist: {}", id); info!("Processing playlist with spotify id: {}", id);
let playlist_info = spotify::get_playlist(spotify_client, &id).await; let playlist_item = music_engine
match playlist_info { .get_playlist_from_spotify_id(text_message)
Some(info) => { .await;
let reply = format!( tgformatter::format_playlist_message(playlist_item)
"Playlist information:\n\ }
Playlist name: {}\n\ _ => {
🧑🎤 {} artist(s): {}", log::warn!("This kind of media has been not supported yet");
info.name, None
info.artists.len(), }
truncate_with_dots(info.artists.join(", "), MAX_ARTISTS_CHARS) },
); };
if option_reply.is_some() {
debug!("Got reply to send back");
let reply = option_reply.unwrap();
bot.send_message(message.chat.id, reply) bot.send_message(message.chat.id, reply)
.reply_to_message_id(message.id) .reply_to_message_id(message.id)
.await?; .await?;
Some(respond(()))
} }
None => None,
} return respond(());
}
Episode(id) => {
log::warn!("Support for episodes ({}) has not be implemented yet!", id);
None
}
Podcast(id) => {
log::warn!("Support for podcasts ({}) has not be implemented yet!", id);
None
}
}
}
None => None,
};
respond(())
}) })
.await; .await;
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
}

204
src/search/mod.rs Normal file
View File

@ -0,0 +1,204 @@
use crate::spotify::{get_entry_kind, AlbumInfo, PlaylistInfo};
use crate::TrackInfo;
use spotify::ContentKind;
use std::collections::HashSet;
use youtube::Video;
pub mod spotify;
mod youtube;
#[cfg(test)]
mod tests;
pub(crate) trait ArtistComposed {
fn get_artists_name(&self) -> HashSet<String>;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct TrackItem {
pub(crate) spotify_track: Option<TrackInfo>,
pub(crate) youtube_track: Option<Vec<Video>>,
}
impl ArtistComposed for TrackItem {
fn get_artists_name(&self) -> HashSet<String> {
if self.spotify_track.is_some() {
return self
.spotify_track
.clone()
.unwrap()
.artists
.into_iter()
.collect();
} else {
self.youtube_track
.clone()
.and_then(|youtube_tracks| {
youtube_tracks.get(0).map(|t| {
let mut hash = HashSet::new();
hash.insert(t.author.clone());
return hash;
})
})
.unwrap_or_else(|| {
let mut hash = HashSet::new();
hash.insert("Unknown artist".to_string());
return hash;
})
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct AlbumItem {
pub(crate) spotify_album: Option<AlbumInfo>,
}
impl ArtistComposed for AlbumItem {
fn get_artists_name(&self) -> HashSet<String> {
if self.spotify_album.is_some() {
return self
.spotify_album
.clone()
.unwrap()
.artists
.into_iter()
.collect();
}
return HashSet::new();
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct PlaylistItem {
pub(crate) spotify_playlist: Option<PlaylistInfo>,
}
impl ArtistComposed for PlaylistItem {
fn get_artists_name(&self) -> HashSet<String> {
if self.spotify_playlist.is_some() {
return self
.spotify_playlist
.clone()
.unwrap()
.artists
.into_iter()
.collect();
}
return HashSet::new();
}
}
// This struct will allow us in the future to search, cache and store data and metadata regarding
// tracks, albums and playlists
pub(crate) struct Engine {
spotify: Box<dyn spotify::SearchableClient + Send + Sync>,
youtube: Box<dyn youtube::SearchableClient + Send + Sync>,
}
impl Engine {
pub(crate) async fn new() -> Self {
Engine {
spotify: Box::new(spotify::Client::new().await),
youtube: Box::new(youtube::Client::new().await),
}
}
#[allow(dead_code)]
#[allow(unused_variables)]
#[cfg(test)]
pub(crate) fn new_with_dependencies(
spotify_client: Box<dyn spotify::SearchableClient + Send + Sync>,
youtube_client: Box<dyn youtube::SearchableClient + Send + Sync>,
) -> Self {
Engine {
spotify: spotify_client,
youtube: youtube_client,
}
}
pub(crate) async fn get_song_by_name(&self, name: &str) -> TrackItem {
todo!("In the future it would be possible to search for all metadata on a record from this call")
}
pub(crate) async fn get_song_from_spotify_id(&self, message: &str) -> TrackItem {
let entry_kind = spotify::get_entry_kind(message);
let track_info = match entry_kind {
Some(entry) => match entry {
ContentKind::Track(id) => self.spotify.get_track(id.as_str()).await,
_ => None,
},
None => None,
};
if track_info.is_some() {
let ti = track_info.unwrap();
let youtube_search = match self
.youtube
.search_video(
format!(
"{}{}",
ti.artists
.get(0)
.map(|artist| format!("{} - ", artist))
.unwrap_or("".to_string()),
ti.name
)
.as_str(),
None,
)
.await
{
Err(_) => None,
Ok(search) => Some(search),
};
return TrackItem {
spotify_track: Some(ti),
youtube_track: youtube_search.map(|search| search.items),
};
}
return TrackItem {
spotify_track: None,
youtube_track: None,
};
}
pub(crate) async fn get_album_from_spotify_id(&self, message: &str) -> AlbumItem {
let entry_kind = spotify::get_entry_kind(message);
let album_info = match entry_kind {
Some(entry) => match entry {
ContentKind::Album(id) => self.spotify.get_album(id.as_str()).await,
_ => None,
},
None => None,
};
AlbumItem {
spotify_album: album_info,
}
}
pub(crate) async fn get_playlist_from_spotify_id(&self, message: &str) -> PlaylistItem {
let entry_kind = spotify::get_entry_kind(message);
let playlist_info = match entry_kind {
Some(entry) => match entry {
ContentKind::Playlist(id) => self.spotify.get_playlist(id.as_str()).await,
_ => None,
},
None => None,
};
PlaylistItem {
spotify_playlist: playlist_info,
}
}
}
pub(crate) fn get_spotify_kind(spotify_id: &str) -> Option<ContentKind> {
get_entry_kind(spotify_id)
}

278
src/search/spotify/mod.rs Normal file
View File

@ -0,0 +1,278 @@
use async_trait::async_trait;
#[cfg(test)]
use mockall::{automock, mock, predicate::*};
use rspotify::model::PlayableItem::{Episode, Track};
use rspotify::model::{AlbumId, PlaylistId, TrackId};
use rspotify::prelude::*;
use rspotify::{ClientCredsSpotify, Credentials};
use std::sync::Arc;
use std::time::Duration;
#[cfg_attr(test, automock)]
#[async_trait]
pub(crate) trait SearchableClient {
async fn get_track(&self, id: &str) -> Option<TrackInfo>;
async fn get_album(&self, id: &str) -> Option<AlbumInfo>;
async fn get_playlist(&self, id: &str) -> Option<PlaylistInfo>;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ContentKind {
Track(String),
Album(String),
Playlist(String),
Podcast(String),
Episode(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PlayableKind {
Track(TrackInfo),
Podcast(EpisodeInfo),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TrackInfo {
pub(crate) name: String,
pub(crate) artists: Vec<String>,
pub(crate) duration: Duration,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EpisodeInfo {
pub(crate) name: String,
pub(crate) show: String,
pub(crate) duration: Duration,
pub(crate) description: String,
pub(crate) languages: Vec<String>,
pub(crate) release_date: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AlbumInfo {
pub(crate) name: String,
pub(crate) artists: Vec<String>,
pub(crate) genres: Vec<String>,
pub(crate) tracks: Vec<TrackInfo>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PlaylistInfo {
pub(crate) name: String,
pub(crate) artists: Vec<String>,
pub(crate) tracks: Vec<PlayableKind>,
pub(crate) owner: Option<String>,
}
#[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),
}
}
#[allow(dead_code)]
#[allow(unused_variables)]
#[cfg(test)]
pub(crate) fn new_with_dependencies(client: ClientCredsSpotify) -> Self {
Client {
client: Arc::new(client),
}
}
}
#[async_trait]
impl SearchableClient for Client {
async fn get_track(&self, id: &str) -> Option<TrackInfo> {
// FIXME should we really return Option here? We're hiding a possible error or a entry not found
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) => return None,
}
}
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,
}
}
async fn get_playlist(&self, id: &str) -> Option<PlaylistInfo> {
let playlist_id = match PlaylistId::from_id(id) {
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(),
owner: playlist.owner.display_name,
}),
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<ContentKind> {
// TODO WE SHOULD PROPERLY TEST THIS FUNCTION
if uri.contains("spotify:track:") {
let track_id = get_id_in_uri(uri);
return match track_id {
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(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(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(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(ContentKind::Playlist(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(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(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(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(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(ContentKind::Episode(id.to_string())),
None => None,
};
}
return None;
}

147
src/search/tests.rs Normal file
View File

@ -0,0 +1,147 @@
use super::*;
use crate::search::youtube::VideoSearch;
use crate::spotify::PlayableKind;
use mockall::predicate;
#[tokio::test]
async fn should_search_track_by_spotify_id() {
let spotify_id = "spotify:track:no-value-kek";
let mut spotify_mock = spotify::MockSearchableClient::new();
spotify_mock
.expect_get_track()
.with(predicate::eq("no-value-kek"))
.returning(|_id| {
Some(TrackInfo {
name: "A name".to_string(),
artists: vec!["Art1".to_string()],
duration: Default::default(),
})
});
let mut youtube_mock = youtube::MockSearchableClient::new();
youtube_mock
.expect_search_video()
.returning(|_id, _sort_by| {
Ok(VideoSearch {
items: vec![Video {
title: "An example".to_string(),
video_id: "id123".to_string(),
author: "An Art".to_string(),
author_id: "artId123".to_string(),
author_url: "https://example.com".to_string(),
length_seconds: 42,
description: "A song".to_string(),
description_html: "A song 2".to_string(),
view_count: 0,
published: 0,
published_text: "".to_string(),
live_now: false,
paid: false,
premium: false,
}],
})
});
let engine = Engine::new_with_dependencies(Box::new(spotify_mock), Box::new(youtube_mock));
let got = engine.get_song_from_spotify_id(spotify_id).await;
assert_eq!(true, got.spotify_track.is_some());
let boxed_st = Box::new(got.spotify_track.unwrap());
assert_eq!(1, boxed_st.artists.len());
assert_eq!("Art1".to_string(), boxed_st.artists.get(0).unwrap().clone());
assert_eq!("A name".to_string(), boxed_st.name);
assert_eq!(true, got.youtube_track.is_some());
let boxed_yt = Box::new(got.youtube_track.unwrap());
assert_eq!(1, boxed_yt.len());
let got_video = boxed_yt.get(0).unwrap();
assert_eq!("An example".to_string(), got_video.title);
}
#[tokio::test]
async fn should_search_album_by_spotify_id() {
let spotify_id = "spotify:album:no-value-kek";
let mut spotify_mock = spotify::MockSearchableClient::new();
spotify_mock
.expect_get_album()
.with(predicate::eq("no-value-kek"))
.returning(|_id| {
Some(AlbumInfo {
name: "An album".to_string(),
artists: vec!["Art1".to_string(), "Art2".to_string()],
genres: vec!["Rock".to_string(), "Hip-hop".to_string()],
tracks: vec![TrackInfo {
name: "Track info 1".to_string(),
artists: vec!["Art1".to_string()],
duration: Default::default(),
}],
})
});
let youtube_mock = youtube::MockSearchableClient::new();
let engine = Engine::new_with_dependencies(Box::new(spotify_mock), Box::new(youtube_mock));
let got = engine.get_album_from_spotify_id(spotify_id).await;
assert_eq!(true, got.spotify_album.is_some());
let boxed_st = Box::new(got.spotify_album.unwrap());
assert_eq!(2, boxed_st.artists.len());
assert_eq!("Art1".to_string(), boxed_st.artists.get(0).unwrap().clone());
assert_eq!("Art2".to_string(), boxed_st.artists.get(1).unwrap().clone());
assert_eq!("An album".to_string(), boxed_st.name);
assert_eq!(2, boxed_st.genres.len());
assert_eq!("Rock".to_string(), boxed_st.genres.get(0).unwrap().clone());
assert_eq!(
"Hip-hop".to_string(),
boxed_st.genres.get(1).unwrap().clone()
);
assert_eq!(1, boxed_st.tracks.len());
assert_eq!(
TrackInfo {
name: "Track info 1".to_string(),
artists: vec!["Art1".to_string()],
duration: Default::default(),
},
boxed_st.tracks.get(0).unwrap().clone()
);
}
#[tokio::test]
async fn should_search_playlist_by_spotify_id() {
let spotify_id = "spotify:playlist:no-value-kek";
let mut spotify_mock = spotify::MockSearchableClient::new();
spotify_mock
.expect_get_playlist()
.with(predicate::eq("no-value-kek"))
.returning(|_id| {
Some(PlaylistInfo {
name: "A playlist".to_string(),
artists: vec!["Art1".to_string(), "Art2".to_string()],
tracks: vec![PlayableKind::Track(TrackInfo {
name: "A track".to_string(),
artists: vec!["Art1".to_string()],
duration: Default::default(),
})],
owner: Some("Frodo".to_string()),
})
});
let youtube_mock = youtube::MockSearchableClient::new();
let engine = Engine::new_with_dependencies(Box::new(spotify_mock), Box::new(youtube_mock));
let got = engine.get_playlist_from_spotify_id(spotify_id).await;
assert_eq!(true, got.spotify_playlist.is_some());
let boxed_st = Box::new(got.spotify_playlist.unwrap());
assert_eq!(2, boxed_st.artists.len());
assert_eq!("Art1".to_string(), boxed_st.artists.get(0).unwrap().clone());
assert_eq!("Art2".to_string(), boxed_st.artists.get(1).unwrap().clone());
assert_eq!("A playlist".to_string(), boxed_st.name);
assert_eq!(1, boxed_st.tracks.len());
assert_eq!(
PlayableKind::Track(TrackInfo {
name: "A track".to_string(),
artists: vec!["Art1".to_string()],
duration: Default::default(),
}),
boxed_st.tracks.get(0).unwrap().clone()
);
assert_eq!(Some("Frodo".to_string()), boxed_st.owner);
}

161
src/search/youtube/mod.rs Normal file
View File

@ -0,0 +1,161 @@
use async_trait::async_trait;
#[cfg(test)]
use mockall::{automock, mock, predicate::*};
use std::error::Error;
use std::sync::Arc;
#[cfg_attr(test, automock)]
#[async_trait]
pub(crate) trait SearchableClient {
async fn search_video<'a>(
&self,
id: &str,
sort_by: Option<&'a SearchSortBy>,
) -> Result<VideoSearch, Box<dyn Error>>;
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct VideoSearch {
pub(crate) items: Vec<Video>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct Video {
pub(crate) title: String,
pub(crate) video_id: String,
pub(crate) author: String,
pub(crate) author_id: String,
pub(crate) author_url: String,
pub(crate) length_seconds: u64,
pub(crate) description: String,
pub(crate) description_html: String,
pub(crate) view_count: u64,
pub(crate) published: u64,
pub(crate) published_text: String,
pub(crate) live_now: bool,
pub(crate) paid: bool,
pub(crate) premium: bool,
}
type SearchSortBy = str;
#[allow(dead_code)]
#[allow(unused_variables)]
const BY_RELEVANCE: &SearchSortBy = "relevance";
#[allow(dead_code)]
#[allow(unused_variables)]
const BY_RATING: &SearchSortBy = "rating";
#[allow(dead_code)]
#[allow(unused_variables)]
const BY_UPLOAD_DATE: &SearchSortBy = "upload_date";
#[allow(dead_code)]
#[allow(unused_variables)]
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://inv.bp.projectsegfau.lt"));
Client {
client: Arc::new(client),
}
}
#[allow(dead_code)]
#[allow(unused_variables)]
#[cfg(test)]
pub(crate) fn new_with_dependencies(client: invidious::asynchronous::Client) -> Self {
Client {
client: Arc::new(client),
}
}
}
#[async_trait]
impl SearchableClient for Client {
async fn search_video<'a>(
&self,
keyword: &str,
sort_by: Option<&'a 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(),
video_id: videoId.clone(),
author: author.clone(),
author_id: authorId.clone(),
author_url: authorUrl.clone(),
length_seconds: lengthSeconds.clone(),
description: description.clone(),
description_html: descriptionHtml.clone(),
view_count: viewCount.clone(),
published: published.clone(),
published_text: 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())
}
}

View File

@ -1,203 +0,0 @@
use rspotify::model::PlayableItem::{Episode, Track};
use rspotify::model::{AlbumId, FullTrack, PlaylistId, TrackId};
use rspotify::prelude::*;
use rspotify::{ClientCredsSpotify, Credentials};
use std::time::Duration;
pub enum SpotifyKind {
Track(String),
Album(String),
Playlist(String),
Podcast(String),
Episode(String),
}
pub enum PlayableKind {
Track(TrackInfo),
Podcast(EpisodeInfo),
}
pub struct TrackInfo {
pub(crate) name: String,
pub(crate) artists: Vec<String>,
pub(crate) duration: Duration,
}
pub struct EpisodeInfo {
pub(crate) name: String,
pub(crate) show: String,
pub(crate) duration: Duration,
pub(crate) description: String,
pub(crate) languages: Vec<String>,
pub(crate) release_date: String,
}
pub struct AlbumInfo {
pub(crate) name: String,
pub(crate) artists: Vec<String>,
pub(crate) genres: Vec<String>,
pub(crate) tracks: Vec<TrackInfo>,
}
pub struct PlaylistInfo {
pub(crate) name: String,
pub(crate) artists: Vec<String>,
pub(crate) tracks: Vec<PlayableKind>,
}
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 extract_artists_from_tracks(tracks: Vec<FullTrack>) -> Vec<String> {
tracks
.iter()
.flat_map(|t| t.artists.iter().map(|a| a.name.clone()))
.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);
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,
}
}

139
src/tgformatter/mod.rs Normal file
View File

@ -0,0 +1,139 @@
mod utils;
use crate::search::{AlbumItem, ArtistComposed, PlaylistItem, TrackItem};
use std::collections::HashSet;
use std::time::Duration;
static MAX_ARTISTS_CHARS: usize = 200;
pub(crate) fn format_track_message(track_info: TrackItem) -> Option<String> {
// Let's avoid copying all the structure...we place it in the heap and pass the pointer to all the other functions
let boxed_info = Box::new(track_info);
if boxed_info.spotify_track.is_none() && boxed_info.youtube_track.is_none() {
return None;
}
let mut reply = format!(
"Track information:\n\
🎵 Track name: {}\n\
🧑🎤 Artist(s): {}\n\
Duration: {}",
get_track_name(boxed_info.clone()),
get_artists(boxed_info.clone()),
get_track_duration(boxed_info.clone())
);
let possible_first_video_link = boxed_info
.youtube_track
.and_then(|x| x.get(0).map(|x| format!("https://youtu.be/{}", x.video_id)));
if possible_first_video_link.is_some() {
reply.push_str(
format!("\n\n🔗Youtube link: {}", possible_first_video_link.unwrap()).as_str(),
);
}
Some(reply)
}
pub(crate) fn format_album_message(album_info: AlbumItem) -> Option<String> {
let boxed_info = Box::new(album_info);
if boxed_info.spotify_album.is_none() {
return None;
}
let artists = boxed_info.get_artists_name().len();
let mut reply = format!(
"Album information:\n\
🎵 Album name: {}\n\
🧑🎤 {} artist(s): {}",
get_album_name(boxed_info.clone()),
artists,
get_artists(boxed_info.clone())
);
let album_genres = get_album_genres(boxed_info.clone());
if album_genres.len() > 0 {
reply.push_str(format!("\n💿 Genre(s): {}", itertools::join(&album_genres, ", ")).as_str());
}
return Some(reply);
}
pub(crate) fn format_playlist_message(playlist_info: PlaylistItem) -> Option<String> {
let boxed_info = Box::new(playlist_info);
if boxed_info.spotify_playlist.is_none() {
return None;
}
let artists = boxed_info.clone().get_artists_name().len();
let reply = format!(
"Playlist information:\n\
Playlist name: {}\n\
🧞 Made by: {}\n\
🧑🎤 {} artist(s): {}",
boxed_info.clone().spotify_playlist.unwrap().name,
boxed_info
.clone()
.spotify_playlist
.unwrap()
.owner
.unwrap_or("Unknown 🤷".to_string()),
artists,
get_artists(boxed_info)
);
return Some(reply);
}
fn get_album_name(ai: Box<AlbumItem>) -> String {
ai.spotify_album
.map(|s| s.name)
.unwrap_or("Unknown album name".to_string())
}
fn get_album_genres(ai: Box<AlbumItem>) -> HashSet<String> {
if ai.spotify_album.is_some() {
return ai.spotify_album.unwrap().genres.into_iter().collect();
}
return HashSet::new();
}
fn get_track_name(ti: Box<TrackItem>) -> String {
if ti.spotify_track.is_some() {
ti.spotify_track.unwrap().name
} else {
ti.youtube_track
.and_then(|youtube_tracks| youtube_tracks.get(0).map(|t| t.title.clone()))
.unwrap_or("Unknown title".to_string())
}
}
fn get_artists(ti: Box<dyn ArtistComposed>) -> String {
let artists = ti.get_artists_name();
let mut artists_names = "Unknown artist list".to_string();
if artists.len() > 0 {
artists_names = itertools::join(&artists, ", ");
}
utils::truncate_with_dots(artists_names, MAX_ARTISTS_CHARS)
}
fn get_track_duration(ti: Box<TrackItem>) -> String {
if ti.spotify_track.is_some() {
utils::human_readable_duration(ti.spotify_track.unwrap().duration)
} else {
ti.youtube_track
.map(|youtube_tracks| {
let seconds_in_string =
youtube_tracks.get(0).map(|t| t.length_seconds).unwrap_or(0);
utils::human_readable_duration(Duration::from_secs(seconds_in_string))
})
.unwrap_or("Unknown duration".to_string())
}
}