Compare commits
4 Commits
34c294ef0f
...
f0b904f17e
Author | SHA1 | Date |
---|---|---|
Davide Polonio | f0b904f17e | |
Davide Polonio | 10040d1f39 | |
Davide Polonio | 0ba77b0aa6 | |
Davide Polonio | c9a252fb9a |
56
src/main.rs
56
src/main.rs
|
@ -1,18 +1,11 @@
|
||||||
use crate::SpotifyURL::Track;
|
use crate::SpotifyURL::Track;
|
||||||
use aspotify::{Client, ClientCredentials};
|
use aspotify::{Client, ClientCredentials};
|
||||||
use std::time::Duration;
|
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
enum SpotifyURL {
|
enum SpotifyURL {
|
||||||
Track(String),
|
Track(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TrackInfo {
|
|
||||||
name: String,
|
|
||||||
artist: Vec<String>,
|
|
||||||
duration: Duration,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_spotify_entry(url: &str) -> Option<SpotifyURL> {
|
fn get_spotify_entry(url: &str) -> Option<SpotifyURL> {
|
||||||
if url.contains("https://open.spotify.com/track/") {
|
if url.contains("https://open.spotify.com/track/") {
|
||||||
let track_id = url.rsplit('/').next().and_then(|x| x.split('?').next());
|
let track_id = url.rsplit('/').next().and_then(|x| x.split('?').next());
|
||||||
|
@ -24,12 +17,16 @@ fn get_spotify_entry(url: &str) -> Option<SpotifyURL> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct TrackInfo {
|
||||||
|
name: String,
|
||||||
|
artist: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
async fn get_spotify_track(spotify: Box<Client>, id: &String) -> Option<TrackInfo> {
|
async fn get_spotify_track(spotify: Box<Client>, id: &String) -> Option<TrackInfo> {
|
||||||
match spotify.tracks().get_track(id.as_str(), None).await {
|
match spotify.tracks().get_track(id.as_str(), None).await {
|
||||||
Ok(track) => Some(TrackInfo {
|
Ok(track) => Some(TrackInfo {
|
||||||
name: track.data.name,
|
name: track.data.name,
|
||||||
artist: track.data.artists.iter().map(|x| x.name.clone()).collect(),
|
artist: track.data.artists.iter().map(|x| x.name.clone()).collect(),
|
||||||
duration: track.data.duration,
|
|
||||||
}),
|
}),
|
||||||
Err(_e) => None,
|
Err(_e) => None,
|
||||||
}
|
}
|
||||||
|
@ -42,34 +39,31 @@ async fn main() {
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env().auto_send();
|
||||||
teloxide::repl(bot, |message| async move {
|
teloxide::repl(bot, |message| async move {
|
||||||
|
let spotify_creds =
|
||||||
|
ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found.");
|
||||||
|
let spotify_client = Box::new(Client::new(spotify_creds));
|
||||||
|
|
||||||
|
log::info!("Connected to Spotify");
|
||||||
let text = message.update.text().and_then(get_spotify_entry);
|
let text = message.update.text().and_then(get_spotify_entry);
|
||||||
match text {
|
match text {
|
||||||
Some(spotify) => {
|
Some(spotify) => match spotify {
|
||||||
let spotify_creds =
|
Track(id) => {
|
||||||
ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found.");
|
let track_info = get_spotify_track(spotify_client, &id).await;
|
||||||
let spotify_client = Box::new(Client::new(spotify_creds));
|
match track_info {
|
||||||
match spotify {
|
Some(info) => {
|
||||||
Track(id) => {
|
let reply = format!(
|
||||||
log::debug!("Parsing spotify song: {}", id);
|
"Track information:\n\
|
||||||
let track_info = get_spotify_track(spotify_client, &id).await;
|
Track name: {}\n\
|
||||||
match track_info {
|
Artists: {}",
|
||||||
Some(info) => {
|
info.name,
|
||||||
let reply = format!(
|
info.artist.join(", ")
|
||||||
"Track information:\n\
|
);
|
||||||
🎵 Track name: {}\n\
|
Some(message.reply_to(reply).await?)
|
||||||
🧑🎤 Artist(s): {}\n\
|
|
||||||
⏳ Duration: {} second(s)",
|
|
||||||
info.name,
|
|
||||||
info.artist.join(", "),
|
|
||||||
info.duration.as_secs()
|
|
||||||
);
|
|
||||||
Some(message.reply_to(reply).await?)
|
|
||||||
}
|
|
||||||
None => None,
|
|
||||||
}
|
}
|
||||||
|
None => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
respond(())
|
respond(())
|
||||||
|
|
Loading…
Reference in New Issue