Compare commits
3 Commits
b39f68622b
...
3066753c69
Author | SHA1 | Date |
---|---|---|
Davide Polonio | 3066753c69 | |
Davide Polonio | 540c306483 | |
Davide Polonio | c02c390a6d |
|
@ -3,4 +3,7 @@
|
||||||
<component name="ProjectRootManager">
|
<component name="ProjectRootManager">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
|
<component name="ProjectType">
|
||||||
|
<option name="id" value="jpab" />
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
||||||
|
|
11
Cargo.toml
11
Cargo.toml
|
@ -1,13 +1,14 @@
|
||||||
[package]
|
[package]
|
||||||
name = "songlify"
|
name = "songlify"
|
||||||
version = "0.2.3"
|
version = "0.3.3"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
teloxide = { version = "0.7.1", features = ["auto-send", "macros"] }
|
teloxide = { version = "0.9.1", features = ["auto-send", "macros"] }
|
||||||
log = "0.4.14"
|
log = "0.4.17"
|
||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
tokio = { version = "1.17.0", features = ["rt-multi-thread", "macros"] }
|
tokio = { version = "1.18.2", features = ["rt-multi-thread", "macros"] }
|
||||||
rspotify = { version = "0.11.4", features = ["default"]}
|
rspotify = { version = "0.11.5", features = ["default"]}
|
||||||
|
sentry = "0.26.0"
|
||||||
|
|
59
src/main.rs
59
src/main.rs
|
@ -1,3 +1,6 @@
|
||||||
|
use log::LevelFilter;
|
||||||
|
use sentry::ClientInitGuard;
|
||||||
|
use std::env;
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
use crate::spotify::{PlayableKind, TrackInfo};
|
use crate::spotify::{PlayableKind, TrackInfo};
|
||||||
|
@ -13,12 +16,37 @@ static MAX_ARTISTS_CHARS: usize = 140;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
teloxide::enable_logging!();
|
if env::var("RUST_LOG").is_err() {
|
||||||
|
pretty_env_logger::formatted_builder()
|
||||||
|
.filter_module("songlify", LevelFilter::Info)
|
||||||
|
.filter_module("teloxide", LevelFilter::Info)
|
||||||
|
.init();
|
||||||
|
} else {
|
||||||
|
pretty_env_logger::init();
|
||||||
|
}
|
||||||
log::info!("Starting Songlify...");
|
log::info!("Starting Songlify...");
|
||||||
|
log::trace!("You're running in trace mode!");
|
||||||
|
|
||||||
|
let mut _guard: ClientInitGuard;
|
||||||
|
match env::var("SENTRY_DSN") {
|
||||||
|
Ok(sentry_dsn) => {
|
||||||
|
log::debug!("Sentry DSN found, enabling error reporting");
|
||||||
|
_guard = sentry::init((
|
||||||
|
sentry_dsn,
|
||||||
|
sentry::ClientOptions {
|
||||||
|
release: sentry::release_name!(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
log::warn!("No sentry DSN set, errors will not be reported. Use SENTRY_DSN env variable if you want to set error reporting")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let bot = Bot::from_env().auto_send();
|
let bot = Bot::from_env().auto_send();
|
||||||
teloxide::repl(bot, |message| async move {
|
teloxide::repl(bot, |message: Message, bot: AutoSend<Bot>| async move {
|
||||||
let text = message.update.text().and_then(spotify::get_entry_kind);
|
let text = message.text().and_then(spotify::get_entry_kind);
|
||||||
match text {
|
match text {
|
||||||
Some(spotify) => {
|
Some(spotify) => {
|
||||||
let spotify_client = spotify::get_client().await;
|
let spotify_client = spotify::get_client().await;
|
||||||
|
@ -37,7 +65,10 @@ async fn main() {
|
||||||
truncate_with_dots(info.artists.join(", "), MAX_ARTISTS_CHARS),
|
truncate_with_dots(info.artists.join(", "), MAX_ARTISTS_CHARS),
|
||||||
human_readable_duration(info.duration)
|
human_readable_duration(info.duration)
|
||||||
);
|
);
|
||||||
Some(message.reply_to(reply).await?)
|
bot.send_message(message.chat.id, reply)
|
||||||
|
.reply_to_message_id(message.id)
|
||||||
|
.await?;
|
||||||
|
Some(respond(()))
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
}
|
}
|
||||||
|
@ -61,11 +92,10 @@ async fn main() {
|
||||||
.as_str(),
|
.as_str(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Some(
|
bot.send_message(message.chat.id, reply)
|
||||||
message
|
.reply_to_message_id(message.id)
|
||||||
.reply_to(add_track_section(info.tracks, reply))
|
.await?;
|
||||||
.await?,
|
Some(respond(()))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
}
|
}
|
||||||
|
@ -83,15 +113,14 @@ async fn main() {
|
||||||
info.artists.len(),
|
info.artists.len(),
|
||||||
truncate_with_dots(info.artists.join(", "), MAX_ARTISTS_CHARS)
|
truncate_with_dots(info.artists.join(", "), MAX_ARTISTS_CHARS)
|
||||||
);
|
);
|
||||||
Some(
|
bot.send_message(message.chat.id, reply)
|
||||||
message
|
.reply_to_message_id(message.id)
|
||||||
.reply_to(add_track_section_for_playlist(info.tracks, reply))
|
.await?;
|
||||||
.await?,
|
Some(respond(()))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Episode(id) => {
|
Episode(id) => {
|
||||||
log::warn!("Support for episodes ({}) has not be implemented yet!", id);
|
log::warn!("Support for episodes ({}) has not be implemented yet!", id);
|
||||||
None
|
None
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
use std::time::Duration;
|
|
||||||
use rspotify::{ClientCredsSpotify, Credentials};
|
|
||||||
use rspotify::model::{AlbumId, FullTrack, PlaylistId, TrackId};
|
|
||||||
use rspotify::model::PlayableItem::{Episode, Track};
|
use rspotify::model::PlayableItem::{Episode, Track};
|
||||||
|
use rspotify::model::{AlbumId, FullTrack, PlaylistId, TrackId};
|
||||||
use rspotify::prelude::*;
|
use rspotify::prelude::*;
|
||||||
|
use rspotify::{ClientCredsSpotify, Credentials};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
pub enum SpotifyKind {
|
pub enum SpotifyKind {
|
||||||
Track(String),
|
Track(String),
|
||||||
Album(String),
|
Album(String),
|
||||||
Playlist(String),
|
Playlist(String),
|
||||||
Podcast(String),
|
Podcast(String),
|
||||||
Episode(String)
|
Episode(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum PlayableKind {
|
pub enum PlayableKind {
|
||||||
Track(TrackInfo),
|
Track(TrackInfo),
|
||||||
Podcast(EpisodeInfo)
|
Podcast(EpisodeInfo),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TrackInfo {
|
pub struct TrackInfo {
|
||||||
|
@ -29,7 +29,7 @@ pub struct EpisodeInfo {
|
||||||
pub(crate) duration: Duration,
|
pub(crate) duration: Duration,
|
||||||
pub(crate) description: String,
|
pub(crate) description: String,
|
||||||
pub(crate) languages: Vec<String>,
|
pub(crate) languages: Vec<String>,
|
||||||
pub(crate) release_date: String
|
pub(crate) release_date: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AlbumInfo {
|
pub struct AlbumInfo {
|
||||||
|
@ -99,7 +99,8 @@ pub fn get_entry_kind(url: &str) -> Option<SpotifyKind> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_client() -> Box<ClientCredsSpotify> {
|
pub async fn get_client() -> Box<ClientCredsSpotify> {
|
||||||
let spotify_creds = Credentials::from_env().expect("RSPOTIFY_CLIENT_ID and RSPOTIFY_CLIENT_SECRET not found.");
|
let spotify_creds =
|
||||||
|
Credentials::from_env().expect("RSPOTIFY_CLIENT_ID and RSPOTIFY_CLIENT_SECRET not found.");
|
||||||
let mut spotify = ClientCredsSpotify::new(spotify_creds);
|
let mut spotify = ClientCredsSpotify::new(spotify_creds);
|
||||||
spotify.request_token().await.unwrap();
|
spotify.request_token().await.unwrap();
|
||||||
Box::new(spotify)
|
Box::new(spotify)
|
||||||
|
@ -108,23 +109,23 @@ pub async fn get_client() -> Box<ClientCredsSpotify> {
|
||||||
pub async fn get_track(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<TrackInfo> {
|
pub async fn get_track(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<TrackInfo> {
|
||||||
let track_id = match TrackId::from_id(id.as_str()) {
|
let track_id = match TrackId::from_id(id.as_str()) {
|
||||||
Ok(track) => track,
|
Ok(track) => track,
|
||||||
Err(_e) => return None
|
Err(_e) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match spotify.track(&track_id).await {
|
match spotify.track(&track_id).await {
|
||||||
Ok(track) => Some(TrackInfo{
|
Ok(track) => Some(TrackInfo {
|
||||||
name: track.name,
|
name: track.name,
|
||||||
artists: track.artists.iter().map(|x| x.name.clone()).collect(),
|
artists: track.artists.iter().map(|x| x.name.clone()).collect(),
|
||||||
duration: track.duration,
|
duration: track.duration,
|
||||||
}),
|
}),
|
||||||
Err(_e) => None
|
Err(_e) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_album(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<AlbumInfo> {
|
pub async fn get_album(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<AlbumInfo> {
|
||||||
let album_id = match AlbumId::from_id(id.as_str()) {
|
let album_id = match AlbumId::from_id(id.as_str()) {
|
||||||
Ok(album) => album,
|
Ok(album) => album,
|
||||||
Err(_e) => return None
|
Err(_e) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match spotify.album(&album_id).await {
|
match spotify.album(&album_id).await {
|
||||||
|
@ -150,7 +151,7 @@ pub async fn get_album(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<
|
||||||
pub async fn get_playlist(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<PlaylistInfo> {
|
pub async fn get_playlist(spotify: Box<ClientCredsSpotify>, id: &String) -> Option<PlaylistInfo> {
|
||||||
let playlist_id = match PlaylistId::from_id(id.as_str()) {
|
let playlist_id = match PlaylistId::from_id(id.as_str()) {
|
||||||
Ok(playlist) => playlist,
|
Ok(playlist) => playlist,
|
||||||
Err(_e) => return None
|
Err(_e) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
match spotify.playlist(&playlist_id, None, None).await {
|
match spotify.playlist(&playlist_id, None, None).await {
|
||||||
|
@ -163,37 +164,35 @@ pub async fn get_playlist(spotify: Box<ClientCredsSpotify>, id: &String) -> Opti
|
||||||
.flat_map(|p| {
|
.flat_map(|p| {
|
||||||
match &p.track {
|
match &p.track {
|
||||||
Some(t) => match t {
|
Some(t) => match t {
|
||||||
Track(t) => t.artists.iter().map(|a| {
|
Track(t) => t.artists.iter().map(|a| a.name.clone()).collect(),
|
||||||
a.name.clone()
|
Episode(e) => vec![e.show.publisher.clone()],
|
||||||
}).collect(),
|
},
|
||||||
Episode(e) => vec![e.show.publisher.clone()]
|
|
||||||
}
|
|
||||||
None => Vec::new(),
|
None => Vec::new(),
|
||||||
}.into_iter()
|
}
|
||||||
}).collect(),
|
.into_iter()
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
tracks: playlist
|
tracks: playlist
|
||||||
.tracks
|
.tracks
|
||||||
.items
|
.items
|
||||||
.iter()
|
.iter()
|
||||||
.map(|p| {
|
.map(|p| match &p.track {
|
||||||
match &p.track {
|
|
||||||
Some(t) => match t {
|
Some(t) => match t {
|
||||||
Track(t) => Some(PlayableKind::Track(TrackInfo{
|
Track(t) => Some(PlayableKind::Track(TrackInfo {
|
||||||
name: t.name.clone(),
|
name: t.name.clone(),
|
||||||
artists: t.artists.iter().map(|a| a.name.clone()).collect(),
|
artists: t.artists.iter().map(|a| a.name.clone()).collect(),
|
||||||
duration: t.duration
|
duration: t.duration,
|
||||||
})),
|
})),
|
||||||
Episode(e) => Some(PlayableKind::Podcast(EpisodeInfo{
|
Episode(e) => Some(PlayableKind::Podcast(EpisodeInfo {
|
||||||
name: e.name.clone(),
|
name: e.name.clone(),
|
||||||
show: e.show.name.clone(),
|
show: e.show.name.clone(),
|
||||||
duration: e.duration,
|
duration: e.duration,
|
||||||
description: e.description.clone(),
|
description: e.description.clone(),
|
||||||
languages: e.languages.clone(),
|
languages: e.languages.clone(),
|
||||||
release_date: e.release_date.clone()
|
release_date: e.release_date.clone(),
|
||||||
}))
|
})),
|
||||||
},
|
},
|
||||||
None => None
|
None => None,
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.filter(|i| i.is_some())
|
.filter(|i| i.is_some())
|
||||||
.map(|i| i.unwrap())
|
.map(|i| i.unwrap())
|
||||||
|
|
Loading…
Reference in New Issue