Compare commits
2 Commits
f0b904f17e
...
34c294ef0f
Author | SHA1 | Date |
---|---|---|
Davide Polonio | 34c294ef0f | |
Davide Polonio | 06c0db1574 |
|
@ -0,0 +1,2 @@
|
||||||
|
target/
|
||||||
|
.idea/
|
|
@ -10,4 +10,4 @@ teloxide = { version = "0.5", features = ["auto-send", "macros"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] }
|
tokio = { version = "1.8", features = ["rt-multi-thread", "macros"] }
|
||||||
rspotify = "0.10.0"
|
aspotify = "0.7.0"
|
|
@ -0,0 +1,18 @@
|
||||||
|
FROM rust:1.55.0-bullseye as builder
|
||||||
|
|
||||||
|
# ENV RUSTFLAGS="-C target-feature=+crt-static"
|
||||||
|
WORKDIR /build
|
||||||
|
|
||||||
|
RUN apt-get update && apt-get install -y \
|
||||||
|
libssl-dev
|
||||||
|
|
||||||
|
COPY ./ /build
|
||||||
|
RUN cargo build --release
|
||||||
|
|
||||||
|
FROM gcr.io/distroless/base
|
||||||
|
|
||||||
|
COPY --from=builder /build/target/release/songlify /usr/bin/songlify
|
||||||
|
COPY --from=builder /lib/x86_64-linux-gnu/libgcc_s.so.1 /lib/x86_64-linux-gnu/
|
||||||
|
COPY --from=builder /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/lib/x86_64-linux-gnu/
|
||||||
|
|
||||||
|
ENTRYPOINT /usr/bin/songlify
|
65
src/main.rs
65
src/main.rs
|
@ -1,27 +1,38 @@
|
||||||
|
use crate::SpotifyURL::Track;
|
||||||
|
use aspotify::{Client, ClientCredentials};
|
||||||
|
use std::time::Duration;
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
use crate::Spotify::Track;
|
|
||||||
|
|
||||||
enum Spotify {
|
enum SpotifyURL {
|
||||||
Track(String)
|
Track(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_spotify_entry(url : &str) -> Option<Spotify> {
|
struct TrackInfo {
|
||||||
|
name: String,
|
||||||
|
artist: Vec<String>,
|
||||||
|
duration: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
return match track_id {
|
return match track_id {
|
||||||
Some(id) => Some(Spotify::Track(id.to_string())),
|
Some(id) => Some(SpotifyURL::Track(id.to_string())),
|
||||||
None => None
|
None => None,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
return None;
|
||||||
return None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_spotify_url(url : &str) -> Option<&str> {
|
async fn get_spotify_track(spotify: Box<Client>, id: &String) -> Option<TrackInfo> {
|
||||||
// https://open.spotify.com/track/0VffaI2jwQknRrxpECYHsF?si=1e16c64779744375
|
match spotify.tracks().get_track(id.as_str(), None).await {
|
||||||
if url.contains("https://open.spotify.com/") {
|
Ok(track) => Some(TrackInfo {
|
||||||
return Some(url)
|
name: track.data.name,
|
||||||
|
artist: track.data.artists.iter().map(|x| x.name.clone()).collect(),
|
||||||
|
duration: track.data.duration,
|
||||||
|
}),
|
||||||
|
Err(_e) => None,
|
||||||
}
|
}
|
||||||
return None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -30,18 +41,40 @@ async fn main() {
|
||||||
log::info!("Starting Songlify...");
|
log::info!("Starting Songlify...");
|
||||||
|
|
||||||
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 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) => {
|
||||||
|
let spotify_creds =
|
||||||
|
ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found.");
|
||||||
|
let spotify_client = Box::new(Client::new(spotify_creds));
|
||||||
match spotify {
|
match spotify {
|
||||||
Track(id) => Some(message.reply_to(id).await?)
|
Track(id) => {
|
||||||
|
log::debug!("Parsing spotify song: {}", id);
|
||||||
|
let track_info = 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.artist.join(", "),
|
||||||
|
info.duration.as_secs()
|
||||||
|
);
|
||||||
|
Some(message.reply_to(reply).await?)
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => None
|
}
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
};
|
};
|
||||||
respond(())
|
respond(())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
log::info!("Exiting...");
|
||||||
}
|
}
|
Loading…
Reference in New Issue