Songlify/src/main.rs

81 lines
3.2 KiB
Rust
Raw Normal View History

use teloxide::prelude::*;
2021-09-21 23:41:50 +02:00
use spotify::SpotifyKind::Track;
use crate::spotify::SpotifyKind::Album;
mod spotify;
2021-09-21 23:41:50 +02:00
#[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_spotify_entry);
2021-09-21 23:41:50 +02:00
match text {
Some(spotify) => {
let spotify_client = spotify::get_spotify_client();
match spotify {
Track(id) => {
log::debug!("Parsing spotify song: {}", id);
let track_info = spotify::get_spotify_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_spotify_album(spotify_client, &id).await;
match album_info {
Some(info) => {
let mut reply = format!(
"Album information:\n\
🎵 Album name name: {}\n\
🧑🎤 Artist(s): {}",
info.name,
info.artists.join(", ")
);
if !info.genres.is_empty() {
reply.push_str(
format!("\n💿 Genre(s): {}", info.genres.join(", "))
.as_str(),
);
}
if !info.songs.is_empty() {
let songs = info
.songs
.iter()
.map(|x| x.name.clone() + "\n")
.collect::<String>();
reply.push_str(format!("\n🎶 Song(s): {}", songs).as_str());
}
Some(message.reply_to(reply).await?)
}
None => None,
}
}
2021-09-21 23:41:50 +02:00
}
}
None => None,
2021-09-21 23:41:50 +02:00
};
respond(())
})
.await;
log::info!("Exiting...");
}