From 06c0db1574b9b82064537ee856f5604d68756ee2 Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Wed, 22 Sep 2021 12:37:48 +0200 Subject: [PATCH] feat: first working bot version, add Dockerfile - swap rspotify with aspotify - add Docker image --- .dockerignore | 2 ++ Cargo.toml | 2 +- Dockerfile | 18 +++++++++++++ src/main.rs | 71 +++++++++++++++++++++++++++++++++++---------------- 4 files changed, 70 insertions(+), 23 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..07827cc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +target/ +.idea/ \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 35ec4a3..37f1f2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" \ No newline at end of file +aspotify = "0.7.0" \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0dbde9c --- /dev/null +++ b/Dockerfile @@ -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 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 75e8ac2..94788e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,35 @@ +use crate::SpotifyURL::Track; +use aspotify::{Client, ClientCredentials}; use teloxide::prelude::*; -use crate::Spotify::Track; -enum Spotify { - Track(String) +enum SpotifyURL { + Track(String), } -fn get_spotify_entry(url : &str) -> Option { +fn get_spotify_entry(url: &str) -> Option { 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> { - // https://open.spotify.com/track/0VffaI2jwQknRrxpECYHsF?si=1e16c64779744375 - if url.contains("https://open.spotify.com/") { - return Some(url) +struct TrackInfo { + name: String, + artist: Vec, +} + +async fn get_spotify_track(spotify: Box, id: &String) -> Option { + match spotify.tracks().get_track(id.as_str(), None).await { + Ok(track) => Some(TrackInfo { + name: track.data.name, + artist: track.data.artists.iter().map(|x| x.name.clone()).collect(), + }), + Err(_e) => None, } - return None } #[tokio::main] @@ -30,18 +38,37 @@ async fn main() { log::info!("Starting Songlify..."); let bot = Bot::from_env().auto_send(); - teloxide::repl(bot, |message| async move { - let text = message.update.text().and_then(get_spotify_entry); + 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); 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).await; + 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(()) }) - .await; -} \ No newline at end of file + .await; + + log::info!("Exiting..."); +}