Compare commits
2 Commits
63eb891f91
...
c2307b53a7
Author | SHA1 | Date |
---|---|---|
Davide Polonio | c2307b53a7 | |
Davide Polonio | 1cda3984d8 |
|
@ -9,4 +9,3 @@ Cargo.lock
|
||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<code_scheme name="Project" version="173">
|
||||||
|
<codeStyleSettings language="XML">
|
||||||
|
<indentOptions>
|
||||||
|
<option name="INDENT_SIZE" value="2" />
|
||||||
|
<option name="CONTINUATION_INDENT_SIZE" value="4" />
|
||||||
|
<option name="TAB_SIZE" value="2" />
|
||||||
|
</indentOptions>
|
||||||
|
</codeStyleSettings>
|
||||||
|
</code_scheme>
|
||||||
|
</component>
|
|
@ -0,0 +1,5 @@
|
||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||||
|
</state>
|
||||||
|
</component>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="GoogleJavaFormatSettings">
|
||||||
|
<option name="enabled" value="true" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -3,7 +3,4 @@
|
||||||
<component name="ProjectRootManager">
|
<component name="ProjectRootManager">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ScalaSbtSettings">
|
|
||||||
<option name="customVMPath" />
|
|
||||||
</component>
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ScalaSbtSettings">
|
||||||
|
<option name="customVMPath" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,17 @@
|
||||||
|
# See https://pre-commit.com for more information
|
||||||
|
# See https://pre-commit.com/hooks.html for more hooks
|
||||||
|
repos:
|
||||||
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
|
rev: v3.2.0
|
||||||
|
hooks:
|
||||||
|
- id: trailing-whitespace
|
||||||
|
- id: check-merge-conflict
|
||||||
|
- id: end-of-file-fixer
|
||||||
|
- id: check-yaml
|
||||||
|
- id: check-added-large-files
|
||||||
|
- repo: https://github.com/doublify/pre-commit-rust
|
||||||
|
rev: v1.0
|
||||||
|
hooks:
|
||||||
|
- id: fmt
|
||||||
|
args: ['--verbose', '--']
|
||||||
|
- id: cargo-check
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "songlify"
|
name = "songlify"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
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
|
||||||
|
|
63
src/main.rs
63
src/main.rs
|
@ -1,8 +1,9 @@
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
|
|
||||||
|
use crate::spotify::TrackInfo;
|
||||||
use spotify::SpotifyKind::Track;
|
use spotify::SpotifyKind::Track;
|
||||||
|
|
||||||
use crate::spotify::SpotifyKind::Album;
|
use crate::spotify::SpotifyKind::{Album, Playlist};
|
||||||
|
|
||||||
mod spotify;
|
mod spotify;
|
||||||
|
|
||||||
|
@ -13,14 +14,14 @@ 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 text = message.update.text().and_then(spotify::get_spotify_entry);
|
let text = message.update.text().and_then(spotify::get_entry_kind);
|
||||||
match text {
|
match text {
|
||||||
Some(spotify) => {
|
Some(spotify) => {
|
||||||
let spotify_client = spotify::get_spotify_client();
|
let spotify_client = spotify::get_client();
|
||||||
match spotify {
|
match spotify {
|
||||||
Track(id) => {
|
Track(id) => {
|
||||||
log::debug!("Parsing spotify song: {}", id);
|
log::debug!("Parsing spotify song: {}", id);
|
||||||
let track_info = spotify::get_spotify_track(spotify_client, &id).await;
|
let track_info = spotify::get_track(spotify_client, &id).await;
|
||||||
match track_info {
|
match track_info {
|
||||||
Some(info) => {
|
Some(info) => {
|
||||||
let reply = format!(
|
let reply = format!(
|
||||||
|
@ -39,14 +40,15 @@ async fn main() {
|
||||||
}
|
}
|
||||||
Album(id) => {
|
Album(id) => {
|
||||||
log::debug!("Parsing spotify album: {}", id);
|
log::debug!("Parsing spotify album: {}", id);
|
||||||
let album_info = spotify::get_spotify_album(spotify_client, &id).await;
|
let album_info = spotify::get_album(spotify_client, &id).await;
|
||||||
match album_info {
|
match album_info {
|
||||||
Some(info) => {
|
Some(info) => {
|
||||||
let mut reply = format!(
|
let mut reply = format!(
|
||||||
"Album information:\n\
|
"Album information:\n\
|
||||||
🎵 Album name name: {}\n\
|
🎵 Album name: {}\n\
|
||||||
🧑🎤 Artist(s): {}",
|
🧑🎤 {} artist(s): {}",
|
||||||
info.name,
|
info.name,
|
||||||
|
info.artists.len(),
|
||||||
info.artists.join(", ")
|
info.artists.join(", ")
|
||||||
);
|
);
|
||||||
if !info.genres.is_empty() {
|
if !info.genres.is_empty() {
|
||||||
|
@ -55,15 +57,33 @@ async fn main() {
|
||||||
.as_str(),
|
.as_str(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if !info.songs.is_empty() {
|
Some(
|
||||||
let songs = info
|
message
|
||||||
.songs
|
.reply_to(add_track_section(info.tracks, reply))
|
||||||
.iter()
|
.await?,
|
||||||
.map(|x| x.name.clone() + "\n")
|
)
|
||||||
.collect::<String>();
|
|
||||||
reply.push_str(format!("\n🎶 Song(s): {}", songs).as_str());
|
|
||||||
}
|
}
|
||||||
Some(message.reply_to(reply).await?)
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Playlist(id) => {
|
||||||
|
log::debug!("Parsing spotify playlist: {}", id);
|
||||||
|
let playlist_info = spotify::get_playlist(spotify_client, &id).await;
|
||||||
|
match playlist_info {
|
||||||
|
Some(info) => {
|
||||||
|
let reply = format!(
|
||||||
|
"Playlist information:\n\
|
||||||
|
✒️ Playlist name: {}\n\
|
||||||
|
🧑🎤 {} artist(s): {}",
|
||||||
|
info.name,
|
||||||
|
info.artists.len(),
|
||||||
|
info.artists.join(", ")
|
||||||
|
);
|
||||||
|
Some(
|
||||||
|
message
|
||||||
|
.reply_to(add_track_section(info.tracks, reply))
|
||||||
|
.await?,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
None => None,
|
None => None,
|
||||||
}
|
}
|
||||||
|
@ -78,3 +98,16 @@ async fn main() {
|
||||||
|
|
||||||
log::info!("Exiting...");
|
log::info!("Exiting...");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_track_section(tracks: Vec<TrackInfo>, reply: String) -> String {
|
||||||
|
if !tracks.is_empty() {
|
||||||
|
let songs = tracks
|
||||||
|
.iter()
|
||||||
|
.map(|x| x.name.clone() + "\n")
|
||||||
|
.collect::<String>();
|
||||||
|
reply
|
||||||
|
.clone()
|
||||||
|
.push_str(format!("\n🎶 {} Track(s): {}", tracks.len(), songs).as_str())
|
||||||
|
}
|
||||||
|
reply
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,31 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use aspotify::{Client, ClientCredentials};
|
use aspotify::PlaylistItemType::{Episode, Track};
|
||||||
|
use aspotify::{Client, ClientCredentials, TrackSimplified};
|
||||||
|
|
||||||
pub enum SpotifyKind {
|
pub enum SpotifyKind {
|
||||||
Track(String),
|
Track(String),
|
||||||
Album(String),
|
Album(String),
|
||||||
|
Playlist(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TrackInfo {
|
||||||
|
pub(crate) name: String,
|
||||||
|
pub(crate) artists: Vec<String>,
|
||||||
|
pub(crate) duration: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AlbumInfo {
|
||||||
|
pub(crate) name: String,
|
||||||
|
pub(crate) artists: Vec<String>,
|
||||||
|
pub(crate) genres: Vec<String>,
|
||||||
|
pub(crate) tracks: Vec<TrackInfo>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PlaylistInfo {
|
||||||
|
pub(crate) name: String,
|
||||||
|
pub(crate) artists: Vec<String>,
|
||||||
|
pub(crate) tracks: Vec<TrackInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_id_in_url(url: &str) -> Option<&str> {
|
fn get_id_in_url(url: &str) -> Option<&str> {
|
||||||
|
@ -14,7 +35,14 @@ fn get_id_in_url(url: &str) -> Option<&str> {
|
||||||
.and_then(|x| x.split('?').next())
|
.and_then(|x| x.split('?').next())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_spotify_entry(url: &str) -> Option<SpotifyKind> {
|
fn extract_artists_from_tracks(tracks: Vec<TrackSimplified>) -> Vec<String> {
|
||||||
|
tracks
|
||||||
|
.iter()
|
||||||
|
.flat_map(|t| t.artists.iter().map(|a| a.name.clone()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_entry_kind(url: &str) -> Option<SpotifyKind> {
|
||||||
if url.contains("https://open.spotify.com/track/") {
|
if url.contains("https://open.spotify.com/track/") {
|
||||||
let track_id = get_id_in_url(url);
|
let track_id = get_id_in_url(url);
|
||||||
return match track_id {
|
return match track_id {
|
||||||
|
@ -29,30 +57,24 @@ pub fn get_spotify_entry(url: &str) -> Option<SpotifyKind> {
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if url.contains("https://open.spotify.com/playlist/") {
|
||||||
|
let playlist_id = get_id_in_url(url);
|
||||||
|
return match playlist_id {
|
||||||
|
Some(id) => Some(SpotifyKind::Playlist(id.to_string())),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
}
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_spotify_client() -> Box<Client> {
|
pub fn get_client() -> Box<Client> {
|
||||||
let spotify_creds =
|
let spotify_creds =
|
||||||
ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found.");
|
ClientCredentials::from_env().expect("CLIENT_ID and CLIENT_SECRET not found.");
|
||||||
let spotify_client = Box::new(Client::new(spotify_creds));
|
let spotify_client = Box::new(Client::new(spotify_creds));
|
||||||
spotify_client
|
spotify_client
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TrackInfo {
|
pub async fn get_track(spotify: Box<Client>, id: &String) -> Option<TrackInfo> {
|
||||||
pub(crate) name: String,
|
|
||||||
pub(crate) artists: Vec<String>,
|
|
||||||
pub(crate) duration: Duration,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct AlbumInfo {
|
|
||||||
pub(crate) name: String,
|
|
||||||
pub(crate) artists: Vec<String>,
|
|
||||||
pub(crate) genres: Vec<String>,
|
|
||||||
pub(crate) songs: Vec<TrackInfo>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_spotify_track(spotify: Box<Client>, id: &String) -> Option<TrackInfo> {
|
|
||||||
match spotify.tracks().get_track(id.as_str(), None).await {
|
match spotify.tracks().get_track(id.as_str(), None).await {
|
||||||
Ok(track) => Some(TrackInfo {
|
Ok(track) => Some(TrackInfo {
|
||||||
name: track.data.name,
|
name: track.data.name,
|
||||||
|
@ -63,13 +85,60 @@ pub async fn get_spotify_track(spotify: Box<Client>, id: &String) -> Option<Trac
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_spotify_album(spotify: Box<Client>, id: &String) -> Option<AlbumInfo> {
|
pub async fn get_album(spotify: Box<Client>, id: &String) -> Option<AlbumInfo> {
|
||||||
match spotify.albums().get_album(id.as_str(), None).await {
|
match spotify.albums().get_album(id.as_str(), None).await {
|
||||||
Ok(album) => Some(AlbumInfo {
|
Ok(album) => Some(AlbumInfo {
|
||||||
name: album.data.name,
|
name: album.data.name,
|
||||||
artists: album.data.artists.iter().map(|x| x.name.clone()).collect(),
|
artists: album.data.artists.iter().map(|x| x.name.clone()).collect(),
|
||||||
genres: album.data.genres,
|
genres: album.data.genres,
|
||||||
songs: vec![], // TODO we could lookup songs and put them here!
|
tracks: album
|
||||||
|
.data
|
||||||
|
.tracks
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.map(|t| TrackInfo {
|
||||||
|
name: t.name.clone(),
|
||||||
|
artists: t.artists.iter().map(|x| x.name.clone()).collect(),
|
||||||
|
duration: t.duration,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
}),
|
||||||
|
Err(_e) => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_playlist(spotify: Box<Client>, id: &String) -> Option<PlaylistInfo> {
|
||||||
|
match spotify.playlists().get_playlist(id.as_str(), None).await {
|
||||||
|
Ok(playlist) => Some(PlaylistInfo {
|
||||||
|
name: playlist.data.name,
|
||||||
|
artists: playlist
|
||||||
|
.data
|
||||||
|
.tracks
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.flat_map(|p| {
|
||||||
|
p.item.as_ref().map_or(Vec::new(), |x| match x {
|
||||||
|
Track(t) => extract_artists_from_tracks(vec![t.clone().simplify()]),
|
||||||
|
Episode(_e) => Vec::new(),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
tracks: playlist
|
||||||
|
.data
|
||||||
|
.tracks
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.flat_map(|p| {
|
||||||
|
p.item.as_ref().map_or(None, |x| match x {
|
||||||
|
Track(t) => Some(TrackInfo {
|
||||||
|
name: t.name.clone(),
|
||||||
|
artists: extract_artists_from_tracks(vec![t.clone().simplify()]),
|
||||||
|
duration: t.duration,
|
||||||
|
}),
|
||||||
|
Episode(_e) => None,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
}),
|
}),
|
||||||
Err(_e) => None,
|
Err(_e) => None,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue