Using blocking rspotify, first non-working version
parent
f3d19182d7
commit
c9a252fb9a
|
@ -10,4 +10,4 @@ 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"] }
|
||||
rspotify = "0.10.0"
|
||||
rspotify = { version = "0.10.0", features = ["blocking"] }
|
69
src/main.rs
69
src/main.rs
|
@ -1,27 +1,44 @@
|
|||
use crate::SpotifyURL::Track;
|
||||
use rspotify::blocking::client::Spotify;
|
||||
use rspotify::blocking::oauth2::SpotifyClientCredentials;
|
||||
use teloxide::prelude::*;
|
||||
use crate::Spotify::Track;
|
||||
|
||||
enum Spotify {
|
||||
Track(String)
|
||||
enum SpotifyURL {
|
||||
Track(String),
|
||||
}
|
||||
|
||||
fn get_spotify_entry(url : &str) -> Option<Spotify> {
|
||||
fn get_spotify_entry(url: &str) -> Option<SpotifyURL> {
|
||||
if url.contains("https://open.spotify.com/track/") {
|
||||
let track_id = url.rsplit('/').next().and_then(|x| x.split('?').next());
|
||||
return match track_id {
|
||||
Some(id) => Some(Spotify::Track(id.to_string())),
|
||||
None => None
|
||||
Some(id) => Some(SpotifyURL::Track(id.to_string())),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
return None
|
||||
return None;
|
||||
}
|
||||
|
||||
fn is_spotify_url(url : &str) -> Option<&str> {
|
||||
fn is_spotify_url(url: &str) -> Option<&str> {
|
||||
// https://open.spotify.com/track/0VffaI2jwQknRrxpECYHsF?si=1e16c64779744375
|
||||
if url.contains("https://open.spotify.com/") {
|
||||
return Some(url)
|
||||
return Some(url);
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
struct TrackInfo {
|
||||
name: String,
|
||||
artist: Vec<String>,
|
||||
}
|
||||
|
||||
fn get_spotify_track(spotify: Box<Spotify>, id: &String) -> Option<TrackInfo> {
|
||||
match spotify.track(id.as_str()) {
|
||||
Ok(track) => Some(TrackInfo {
|
||||
name: track.name,
|
||||
artist: track.artists.iter().map(|x| x.name.clone()).collect(),
|
||||
}),
|
||||
Err(_e) => None,
|
||||
}
|
||||
return None
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
@ -30,16 +47,36 @@ async fn main() {
|
|||
log::info!("Starting Songlify...");
|
||||
|
||||
let bot = Bot::from_env().auto_send();
|
||||
|
||||
teloxide::repl(bot, |message| async move {
|
||||
let spotify_creds = SpotifyClientCredentials::default().build();
|
||||
let spotify_client = Box::new(
|
||||
Spotify::default()
|
||||
.client_credentials_manager(spotify_creds)
|
||||
.build(),
|
||||
);
|
||||
|
||||
log::info!("Connected to Spotify");
|
||||
let text = message.update.text().and_then(get_spotify_entry);
|
||||
match text {
|
||||
Some(spotify) => {
|
||||
match spotify {
|
||||
Track(id) => Some(message.reply_to(id).await?)
|
||||
Some(spotify) => match spotify {
|
||||
Track(id) => {
|
||||
let track_info = get_spotify_track(spotify_client, &id);
|
||||
match track_info {
|
||||
Some(info) => {
|
||||
let reply = format!(
|
||||
"Track information:\n\
|
||||
Track name: {}\n\
|
||||
Artists: {}",
|
||||
info.name,
|
||||
info.artist.join(", ")
|
||||
);
|
||||
Some(message.reply_to(reply).await?)
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
None => None
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
respond(())
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue