Songlify/src/main.rs

114 lines
4.5 KiB
Rust

use teloxide::prelude::*;
use crate::spotify::TrackInfo;
use spotify::SpotifyKind::Track;
use crate::spotify::SpotifyKind::{Album, Playlist};
mod spotify;
#[tokio::main]
async fn main() {
teloxide::enable_logging!();
log::info!("Starting Songlify...");
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 {
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: {} second(s)",
info.name,
info.artists.join(", "),
info.duration.as_secs()
);
Some(message.reply_to(reply).await?)
}
None => None,
}
}
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(),
info.artists.join(", ")
);
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,
}
}
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(),
info.artists.join(", ")
);
Some(
message
.reply_to(add_track_section(info.tracks, reply))
.await?,
)
}
None => None,
}
}
}
}
None => None,
};
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
}