Compare commits
4 Commits
a65a0469c1
...
f0b904f17e
Author | SHA1 | Date |
---|---|---|
Davide Polonio | f0b904f17e | |
Davide Polonio | 10040d1f39 | |
Davide Polonio | 0ba77b0aa6 | |
Davide Polonio | c9a252fb9a |
|
@ -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 = { version = "0.10.0", features = ["blocking"] }
|
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
|
32
src/main.rs
32
src/main.rs
|
@ -1,6 +1,5 @@
|
||||||
use crate::SpotifyURL::Track;
|
use crate::SpotifyURL::Track;
|
||||||
use rspotify::blocking::client::Spotify;
|
use aspotify::{Client, ClientCredentials};
|
||||||
use rspotify::blocking::oauth2::SpotifyClientCredentials;
|
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
enum SpotifyURL {
|
enum SpotifyURL {
|
||||||
|
@ -18,24 +17,16 @@ fn get_spotify_entry(url: &str) -> Option<SpotifyURL> {
|
||||||
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);
|
|
||||||
}
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TrackInfo {
|
struct TrackInfo {
|
||||||
name: String,
|
name: String,
|
||||||
artist: Vec<String>,
|
artist: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_spotify_track(spotify: Box<Spotify>, id: &String) -> Option<TrackInfo> {
|
async fn get_spotify_track(spotify: Box<Client>, id: &String) -> Option<TrackInfo> {
|
||||||
match spotify.track(id.as_str()) {
|
match spotify.tracks().get_track(id.as_str(), None).await {
|
||||||
Ok(track) => Some(TrackInfo {
|
Ok(track) => Some(TrackInfo {
|
||||||
name: track.name,
|
name: track.data.name,
|
||||||
artist: track.artists.iter().map(|x| x.name.clone()).collect(),
|
artist: track.data.artists.iter().map(|x| x.name.clone()).collect(),
|
||||||
}),
|
}),
|
||||||
Err(_e) => None,
|
Err(_e) => None,
|
||||||
}
|
}
|
||||||
|
@ -48,19 +39,16 @@ 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 = SpotifyClientCredentials::default().build();
|
let spotify_creds =
|
||||||
let spotify_client = Box::new(
|
ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found.");
|
||||||
Spotify::default()
|
let spotify_client = Box::new(Client::new(spotify_creds));
|
||||||
.client_credentials_manager(spotify_creds)
|
|
||||||
.build(),
|
|
||||||
);
|
|
||||||
|
|
||||||
log::info!("Connected to Spotify");
|
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) => match spotify {
|
Some(spotify) => match spotify {
|
||||||
Track(id) => {
|
Track(id) => {
|
||||||
let track_info = get_spotify_track(spotify_client, &id);
|
let track_info = get_spotify_track(spotify_client, &id).await;
|
||||||
match track_info {
|
match track_info {
|
||||||
Some(info) => {
|
Some(info) => {
|
||||||
let reply = format!(
|
let reply = format!(
|
||||||
|
@ -81,4 +69,6 @@ async fn main() {
|
||||||
respond(())
|
respond(())
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
log::info!("Exiting...");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue