Compare commits

..

21 Commits

Author SHA1 Message Date
Davide Polonio 1e9e401088 chore: bump version 2024-01-15 13:00:20 +01:00
Davide Polonio 0561c49d47 chore: bump Invidious client deps, update tests
* set result sorting by most viewed to return the mostly seen video -
  which probably is the one we want (yes, wild assumption I know)
2024-01-15 12:50:57 +01:00
Davide Polonio 30990304d9 chore: bump rspotify to latest version 2024-01-14 19:08:12 +01:00
Davide Polonio 8b227c7bbb chore: bump rspotify 2024-01-14 19:00:55 +01:00
Davide Polonio 1a0400a21f chore: update deps and fix compilation errors 2024-01-14 11:43:33 +01:00
Davide Polonio 571de470d9 Merge pull request 'feat: add youtube search' (#8) from issue#2 into 0.3.x
Reviewed-on: #8
2023-11-03 10:10:39 +01:00
Davide Polonio c7427d1b6c feat: add tests, start using traits
* Fix ContentKind bug
2022-07-17 17:07:59 +02:00
Davide Polonio c94e002311 chore: bump Dockerfile deps 2022-07-15 17:57:19 +02:00
Davide Polonio a1efd07c2e chore: set beta version, update deps 2022-07-15 17:54:07 +02:00
Davide Polonio 66bb5d1333 feat: add back playlist support 2022-07-15 17:46:14 +02:00
Davide Polonio ff86b4c26d feat: add youtube search for tracks. Still WIP
* missing tests, docs and other stuff
* missing playlist porting and other content too (maybe)
2022-06-23 18:33:16 +02:00
Davide Polonio ba2b689e77 feat: add first engine song search 2022-06-08 18:17:22 +02:00
Davide Polonio 06fff8e972 feat: finish first method draft implementation 2022-05-31 23:13:28 +02:00
Davide Polonio 469991d20b feat: add first engine draft for music search 2022-05-31 22:07:53 +02:00
Davide Polonio 1b452517e3 feat: add first Youtube search functionality 2022-05-31 17:06:39 +02:00
Davide Polonio 3066753c69 feat: better logging output 2022-05-31 13:09:31 +02:00
Davide Polonio 540c306483 feat: add Sentry integration 2022-05-31 11:55:12 +02:00
Davide Polonio c02c390a6d chore: bump depedencies 2022-05-31 11:37:30 +02:00
Davide Polonio b39f68622b chore: update Dockerfile 2022-03-18 11:06:40 +01:00
Davide Polonio df42e3b138 chore: bump dependencies, switch to rspotify (#6)
Fixes: #5
Co-authored-by: Davide Polonio <poloniodavide@gmail.com>
Reviewed-on: #6
Co-authored-by: polpetta <polpetta@poldebra.me>
Co-committed-by: polpetta <polpetta@poldebra.me>
2022-03-18 10:02:11 +00:00
Davide Polonio c157fd17f5 chore: idea cleanup 2022-01-22 15:55:28 +01:00
13 changed files with 1050 additions and 263 deletions

View File

@ -3,4 +3,7 @@
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="ProjectType">
<option name="id" value="jpab" />
</component>
</project>

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

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,13 +1,21 @@
[package]
name = "songlify"
version = "0.2.2"
version = "0.3.5"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
teloxide = { version = "0.5", features = ["auto-send", "macros"] }
log = "0.4"
pretty_env_logger = "0.4.0"
tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] }
aspotify = "0.7.0"
teloxide = { version = "0.9.2", features = ["auto-send", "macros"] }
log = "0.4.17"
pretty_env_logger = "0.5.0"
tokio = { version = "1.20.0", features = ["rt-multi-thread", "macros"] }
rspotify = { version = "0.12.0", features = ["default"]}
sentry = "0.32.1"
invidious = { version = "0.7.4", no-default-features = true, features = ["reqwest_async"]}
itertools = "0.12.0"
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.56.1-slim-bullseye as builder
FROM rust:1.62.0-slim-bullseye as builder
WORKDIR /build
RUN apt-get update && apt-get install -y --no-install-recommends \
libssl-dev=1.1.1k-1+deb11u1 \
libssl-dev=1.1.1n-0+deb11u3 \
pkg-config=0.29.2-1
COPY ./ /build

View File

@ -1,117 +1,95 @@
use log::{debug, info, LevelFilter};
use search::spotify;
use sentry::ClientInitGuard;
use std::env;
use teloxide::prelude::*;
use crate::spotify::TrackInfo;
use spotify::SpotifyKind::Track;
use search::spotify::ContentKind::Track;
use search::spotify::TrackInfo;
use crate::spotify::SpotifyKind::{Album, Playlist};
use crate::utils::{human_readable_duration, truncate_with_dots};
use crate::search::get_spotify_kind;
use search::spotify::ContentKind::{Album, Playlist};
mod spotify;
mod utils;
static MAX_ARTISTS_CHARS: usize = 140;
mod search;
mod tgformatter;
#[tokio::main]
async fn main() {
teloxide::enable_logging!();
if env::var("RUST_LOG").is_err() {
pretty_env_logger::formatted_builder()
.filter_module("songlify", LevelFilter::Info)
.filter_module("teloxide", LevelFilter::Info)
.init();
} else {
pretty_env_logger::init();
}
log::info!("Starting Songlify...");
log::trace!("You're running in trace mode!");
let mut _guard: ClientInitGuard;
match env::var("SENTRY_DSN") {
Ok(sentry_dsn) => {
log::debug!("Sentry DSN found, enabling error reporting");
_guard = sentry::init((
sentry_dsn,
sentry::ClientOptions {
release: sentry::release_name!(),
..Default::default()
},
));
}
Err(_) => {
log::warn!("No sentry DSN set, errors will not be reported. Use SENTRY_DSN env variable if you want to set error reporting")
}
}
let bot = Bot::from_env().auto_send();
teloxide::repl(bot, |message| async move {
let text = message.update.text().and_then(spotify::get_entry_kind);
match text {
Some(spotify) => {
let spotify_client = spotify::get_client();
match spotify {
teloxide::repl(bot, |message: Message, bot: AutoSend<Bot>| async move {
let music_engine = search::Engine::new().await;
let opt_text_message = message.text();
if opt_text_message.is_none() {
return respond(());
}
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) => {
log::debug!("Parsing spotify song: {}", id);
let track_info = spotify::get_track(spotify_client, &id).await;
match track_info {
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)
);
Some(message.reply_to(reply).await?)
}
None => None,
}
info!("Processing song with spotify id: {}", id);
let track_item = music_engine.get_song_from_spotify_id(text_message).await;
tgformatter::format_track_message(track_item)
}
Album(id) => {
log::debug!("Parsing spotify album: {}", id);
let album_info = spotify::get_album(spotify_client, &id).await;
match album_info {
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(),
);
}
Some(
message
.reply_to(add_track_section(info.tracks, reply))
.await?,
)
}
None => None,
}
info!("Processing album with spotify id: {}", id);
let album_item = music_engine.get_album_from_spotify_id(text_message).await;
tgformatter::format_album_message(album_item)
}
Playlist(id) => {
log::debug!("Parsing spotify playlist: {}", id);
let playlist_info = spotify::get_playlist(spotify_client, &id).await;
match playlist_info {
Some(info) => {
let reply = format!(
"Playlist information:\n\
Playlist name: {}\n\
🧑🎤 {} artist(s): {}",
info.name,
info.artists.len(),
truncate_with_dots(info.artists.join(", "), MAX_ARTISTS_CHARS)
);
Some(
message
.reply_to(add_track_section(info.tracks, reply))
.await?,
)
info!("Processing playlist with spotify id: {}", id);
let playlist_item = music_engine
.get_playlist_from_spotify_id(text_message)
.await;
tgformatter::format_playlist_message(playlist_item)
}
None => None,
_ => {
log::warn!("This kind of media has been not supported yet");
None
}
}
}
}
None => None,
},
};
respond(())
if option_reply.is_some() {
debug!("Got reply to send back");
let reply = option_reply.unwrap();
bot.send_message(message.chat.id, reply)
.reply_to_message_id(message.id)
.await?;
}
return respond(());
})
.await;
log::info!("Exiting...");
}
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)
}

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

@ -0,0 +1,289 @@
use async_trait::async_trait;
#[cfg(test)]
use mockall::{automock, predicate::*};
use rspotify::model::Country::UnitedStates;
use rspotify::model::PlayableItem::{Episode, Track};
use rspotify::model::{AlbumId, Market, 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 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,
};
// Search track from US market
match self
.client
.track(track_id, Some(Market::Country(UnitedStates)))
.await
{
Ok(track) => Some(TrackInfo {
name: track.name,
artists: track.artists.iter().map(|x| x.name.clone()).collect(),
duration: Duration::from_secs(track.duration.num_seconds() as u64),
}),
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,
};
// Search album from US market
match self
.client
.album(album_id, Some(Market::Country(UnitedStates)))
.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: Duration::from_secs(t.duration.num_seconds() as u64),
})
.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: Duration::from_secs(t.duration.num_seconds() as u64),
})),
Episode(e) => Some(PlayableKind::Podcast(EpisodeInfo {
name: e.name.clone(),
show: e.show.name.clone(),
duration: Duration::from_secs(e.duration.num_seconds() as u64),
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;
}

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

@ -0,0 +1,146 @@
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,
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);
}

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

@ -0,0 +1,156 @@
use async_trait::async_trait;
use invidious::hidden::SearchItem;
use invidious::{ClientAsync, ClientAsyncTrait, MethodAsync};
use itertools::Itertools;
#[cfg(test)]
use mockall::{automock, 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) 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(Clone)]
pub(crate) struct Client {
client: Arc<ClientAsync>,
}
impl Client {
pub(crate) async fn new() -> Self {
// TODO check for a stable instance, or rotate between a pool of stable ones
let client = ClientAsync::new(
String::from("https://inv.bp.projectsegfau.lt"),
MethodAsync::default(),
);
Client {
client: Arc::new(client),
}
}
#[allow(dead_code)]
#[allow(unused_variables)]
#[cfg(test)]
pub(crate) fn new_with_dependencies(client: invidious::ClientAsync) -> 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| {
let video = match search_result {
SearchItem::Video(item) => Some(Video {
title: item.title.clone(),
video_id: item.id.clone(),
author: item.author.clone(),
author_id: item.author_id.clone(),
author_url: item.author_url.clone(),
length_seconds: item.length.clone() as u64,
description: item.description.clone(),
description_html: item.description_html.clone(),
view_count: item.views.clone(),
published: item.published.clone(),
published_text: item.published_text.clone(),
live_now: item.live.clone(),
premium: item.premium.clone(),
}),
_ => None,
};
video
})
.filter(|x| x.is_some())
.map(|x| x.unwrap())
.filter(|x| !x.premium && !x.live_now)
.sorted_by(|a, b| b.view_count.cmp(&a.view_count))
.collect();
Result::Ok(VideoSearch { items: videos })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_search_properly() {
// FIXME: the real solution for properly testing this piece of code would be to spin up a Mocked Invidious
// instance (i.e. mock-server) and mock the server responses. With the current implementation, the tests
// will always be flaky.
let client = tokio_test::block_on(Client::new());
let result = tokio_test::block_on(
client.search_video("Eminem - Lose Yourself", Some(BY_UPLOAD_DATE)),
)
.unwrap();
assert_eq!(19, result.items.len());
// Just check the first one for now - in the future we can check for the whole array
assert_eq!("Eminem - Lose Yourself [HD]", result.items[0].title);
}
}

View File

@ -1,145 +0,0 @@
use std::time::Duration;
use aspotify::PlaylistItemType::{Episode, Track};
use aspotify::{Client, ClientCredentials, TrackSimplified};
pub enum SpotifyKind {
Track(String),
Album(String),
Playlist(String),
}
pub struct TrackInfo {
pub(crate) name: String,
pub(crate) artists: Vec<String>,
pub(crate) duration: Duration,
}
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<TrackInfo>,
}
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<TrackSimplified>) -> 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,
};
}
return None;
}
pub fn get_client() -> Box<Client> {
let spotify_creds =
ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found.");
let spotify_client = Box::new(Client::new(spotify_creds));
spotify_client
}
pub async fn get_track(spotify: Box<Client>, id: &String) -> Option<TrackInfo> {
match spotify.tracks().get_track(id.as_str(), None).await {
Ok(track) => Some(TrackInfo {
name: track.data.name,
artists: track.data.artists.iter().map(|x| x.name.clone()).collect(),
duration: track.data.duration,
}),
Err(_e) => None,
}
}
pub async fn get_album(spotify: Box<Client>, id: &String) -> Option<AlbumInfo> {
match spotify.albums().get_album(id.as_str(), None).await {
Ok(album) => Some(AlbumInfo {
name: album.data.name,
artists: album.data.artists.iter().map(|x| x.name.clone()).collect(),
genres: album.data.genres,
tracks: album
.data
.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<Client>, id: &String) -> Option<PlaylistInfo> {
match spotify.playlists().get_playlist(id.as_str(), None).await {
Ok(playlist) => Some(PlaylistInfo {
name: playlist.data.name,
artists: playlist
.data
.tracks
.items
.iter()
.flat_map(|p| {
p.item.as_ref().map_or(Vec::new(), |x| match x {
Track(t) => extract_artists_from_tracks(vec![t.clone().simplify()]),
Episode(_e) => Vec::new(),
})
})
.collect(),
tracks: playlist
.data
.tracks
.items
.iter()
.flat_map(|p| {
p.item.as_ref().map_or(None, |x| match x {
Track(t) => Some(TrackInfo {
name: t.name.clone(),
artists: extract_artists_from_tracks(vec![t.clone().simplify()]),
duration: t.duration,
}),
Episode(_e) => None,
})
})
.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())
}
}