mirror of
https://github.com/Dangoware/dango-music-player.git
synced 2025-04-19 10:02:53 -05:00
Changed connections to take their input from the front-end
This commit is contained in:
parent
a2637f4329
commit
43a98d1151
4 changed files with 296 additions and 227 deletions
|
@ -41,3 +41,7 @@ prismriver = { git = "https://github.com/Dangoware/prismriver.git" }
|
||||||
parking_lot = "0.12.3"
|
parking_lot = "0.12.3"
|
||||||
discord-presence = { version = "1.4.1", features = ["activity_type"] }
|
discord-presence = { version = "1.4.1", features = ["activity_type"] }
|
||||||
listenbrainz = "0.8.1"
|
listenbrainz = "0.8.1"
|
||||||
|
rustfm-scrobble = "1.1.1"
|
||||||
|
reqwest = { version = "0.12.12", features = ["json"] }
|
||||||
|
tokio = { version = "1.43.0", features = ["macros"] }
|
||||||
|
opener = "0.7.2"
|
||||||
|
|
|
@ -94,7 +94,9 @@ impl ConfigLibraries {
|
||||||
|
|
||||||
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
|
||||||
pub struct ConfigConnections {
|
pub struct ConfigConnections {
|
||||||
|
pub discord_rpc_client_id: Option<u64>,
|
||||||
pub listenbrainz_token: Option<String>,
|
pub listenbrainz_token: Option<String>,
|
||||||
|
pub last_fm_session: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
|
||||||
|
|
|
@ -8,19 +8,20 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use chrono::TimeDelta;
|
use chrono::TimeDelta;
|
||||||
use crossbeam::{scope, select};
|
use crossbeam::select;
|
||||||
use crossbeam_channel::{unbounded, Receiver};
|
use crossbeam_channel::{unbounded, Receiver, Sender};
|
||||||
use discord_presence::Client;
|
use discord_presence::Client;
|
||||||
use listenbrainz::ListenBrainz;
|
use listenbrainz::ListenBrainz;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use prismriver::State as PrismState;
|
use prismriver::State as PrismState;
|
||||||
|
use rustfm_scrobble::Scrobbler;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
music_storage::library::{Song, Tag},
|
music_storage::library::{Song, Tag},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::controller::Controller;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(super) enum ConnectionsNotification {
|
pub(super) enum ConnectionsNotification {
|
||||||
|
@ -32,41 +33,47 @@ pub(super) enum ConnectionsNotification {
|
||||||
SongChange(Song),
|
SongChange(Song),
|
||||||
AboutToFinish,
|
AboutToFinish,
|
||||||
EOS,
|
EOS,
|
||||||
|
TryEnableConnection(TryConnectionType)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ConnectionsInput {
|
pub(super) enum TryConnectionType {
|
||||||
pub discord_rpc_client_id: Option<u64>,
|
Discord(u64),
|
||||||
|
LastFM {
|
||||||
|
api_key: String,
|
||||||
|
api_secret: String,
|
||||||
|
session: Option<String>
|
||||||
|
},
|
||||||
|
ListenBrainz(String),
|
||||||
|
Custom(String)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) struct ControllerConnections {
|
pub(super) struct ControllerConnections {
|
||||||
|
pub notifications_rx: Sender<ConnectionsNotification>,
|
||||||
pub notifications_tx: Receiver<ConnectionsNotification>,
|
pub notifications_tx: Receiver<ConnectionsNotification>,
|
||||||
pub inner: ConnectionsInput,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static DC_ACTIVE: AtomicBool = AtomicBool::new(false);
|
static DC_ACTIVE: AtomicBool = AtomicBool::new(false);
|
||||||
static LB_ACTIVE: AtomicBool = AtomicBool::new(false);
|
static LB_ACTIVE: AtomicBool = AtomicBool::new(false);
|
||||||
|
static LAST_FM_ACTIVE: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
impl Controller {
|
pub(super) fn handle_connections(
|
||||||
pub(super) fn handle_connections(
|
|
||||||
config: Arc<RwLock<Config>>,
|
config: Arc<RwLock<Config>>,
|
||||||
ControllerConnections {
|
ControllerConnections {
|
||||||
|
notifications_rx,
|
||||||
notifications_tx,
|
notifications_tx,
|
||||||
inner: ConnectionsInput {
|
|
||||||
discord_rpc_client_id,
|
|
||||||
},
|
|
||||||
}: ControllerConnections,
|
}: ControllerConnections,
|
||||||
) {
|
) {
|
||||||
let (dc_state_rx, dc_state_tx) = unbounded::<PrismState>();
|
let (dc_state_rx, dc_state_tx) = unbounded::<PrismState>();
|
||||||
let (dc_song_rx, dc_song_tx) = unbounded::<Song>();
|
let (dc_song_rx, dc_song_tx) = unbounded::<Song>();
|
||||||
let (lb_song_rx, lb_song_tx) = unbounded::<Song>();
|
let (lb_song_rx, lb_song_tx) = unbounded::<Song>();
|
||||||
let (lb_abt_fin_rx, lb_abt_fn_tx) = unbounded::<()>();
|
let (lb_abt_fin_rx, lb_abt_fin_tx) = unbounded::<()>();
|
||||||
let (lb_eos_rx, lb_eos_tx) = unbounded::<()>();
|
let (lb_eos_rx, lb_eos_tx) = unbounded::<()>();
|
||||||
|
let (last_song_rx, last_song_tx) = unbounded::<Song>();
|
||||||
|
let (last_abt_fin_rx, last_abt_fin_tx) = unbounded::<()>();
|
||||||
|
let (last_eos_rx, last_eos_tx) = unbounded::<()>();
|
||||||
|
|
||||||
|
|
||||||
scope(|s| {
|
|
||||||
s.builder()
|
|
||||||
.name("Notifications Sorter".to_string())
|
|
||||||
.spawn(|_| {
|
|
||||||
use ConnectionsNotification::*;
|
use ConnectionsNotification::*;
|
||||||
while true {
|
while true {
|
||||||
match notifications_tx.recv().unwrap() {
|
match notifications_tx.recv().unwrap() {
|
||||||
|
@ -94,33 +101,50 @@ impl Controller {
|
||||||
lb_abt_fin_rx.send(()).unwrap();
|
lb_abt_fin_rx.send(()).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
TryEnableConnection(c) => { match c {
|
||||||
}
|
TryConnectionType::Discord(client_id) => {
|
||||||
})
|
let (dc_song_tx, dc_state_tx) = (dc_song_tx.clone(), dc_state_tx.clone());
|
||||||
.unwrap();
|
std::thread::Builder::new()
|
||||||
|
|
||||||
if let Some(client_id) = discord_rpc_client_id {
|
|
||||||
s.builder()
|
|
||||||
.name("Discord RPC Handler".to_string())
|
.name("Discord RPC Handler".to_string())
|
||||||
.spawn(move |_| {
|
.spawn(move || {
|
||||||
Controller::discord_rpc(client_id, dc_song_tx, dc_state_tx);
|
// TODO: add proper error handling here
|
||||||
|
discord_rpc(client_id, dc_song_tx, dc_state_tx);
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
};
|
},
|
||||||
|
TryConnectionType::ListenBrainz(token) => {
|
||||||
if let Some(token) = config.read().connections.listenbrainz_token.clone() {
|
let (lb_song_tx, lb_abt_fin_tx, lb_eos_tx) = (lb_song_tx.clone(), lb_abt_fin_tx.clone(), lb_eos_tx.clone());
|
||||||
s.builder()
|
std::thread::Builder::new()
|
||||||
.name("ListenBrainz Handler".to_string())
|
.name("ListenBrainz Handler".to_string())
|
||||||
.spawn(move |_| {
|
.spawn(move || {
|
||||||
Controller::listenbrainz_scrobble(&token, lb_song_tx, lb_abt_fn_tx, lb_eos_tx);
|
listenbrainz_scrobble(&token, lb_song_tx, lb_abt_fin_tx, lb_eos_tx);
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
TryConnectionType::LastFM { api_key, api_secret, session } => {
|
||||||
|
let (config, notifications_rx) = (config.clone(), notifications_rx.clone());
|
||||||
|
std::thread::Builder::new()
|
||||||
|
.name("last.fm Handler".to_string())
|
||||||
|
.spawn(move || {
|
||||||
|
let scrobbler = if let Some(session) = session {
|
||||||
|
let mut scrobbler = Scrobbler::new(&api_key, &api_secret);
|
||||||
|
scrobbler.authenticate_with_session_key(&session);
|
||||||
|
Ok(scrobbler)
|
||||||
|
} else {
|
||||||
|
last_fm_auth(config, notifications_rx, &api_key, &api_secret)
|
||||||
|
};
|
||||||
|
// TODO: Add scrobbling support
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
TryConnectionType::Custom(_) => unimplemented!()
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn discord_rpc(client_id: u64, song_tx: Receiver<Song>, state_tx: Receiver<PrismState>) {
|
}
|
||||||
|
|
||||||
|
fn discord_rpc(client_id: u64, song_tx: Receiver<Song>, state_tx: Receiver<PrismState>) {
|
||||||
// TODO: Handle seeking position change and pause
|
// TODO: Handle seeking position change and pause
|
||||||
let mut client =
|
let mut client =
|
||||||
discord_presence::Client::with_error_config(client_id, Duration::from_secs(5), None);
|
discord_presence::Client::with_error_config(client_id, Duration::from_secs(5), None);
|
||||||
|
@ -204,9 +228,9 @@ impl Controller {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
DC_ACTIVE.store(false, Ordering::Relaxed);
|
DC_ACTIVE.store(false, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn listenbrainz_scrobble(token: &str, song_tx: Receiver<Song>, abt_fn_tx: Receiver<()>, eos_tx: Receiver<()>) {
|
fn listenbrainz_scrobble(token: &str, song_tx: Receiver<Song>, abt_fn_tx: Receiver<()>, eos_tx: Receiver<()>) {
|
||||||
let mut client = ListenBrainz::new();
|
let mut client = ListenBrainz::new();
|
||||||
client.authenticate(token).unwrap();
|
client.authenticate(token).unwrap();
|
||||||
if !client.is_authenticated() {
|
if !client.is_authenticated() {
|
||||||
|
@ -268,5 +292,47 @@ impl Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LB_ACTIVE.store(false, Ordering::Relaxed);
|
LB_ACTIVE.store(false, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
pub(super) fn last_fm_auth(
|
||||||
|
config: Arc<RwLock<Config>>,
|
||||||
|
notifications_rx: Sender<ConnectionsNotification>,
|
||||||
|
api_key: &str,
|
||||||
|
api_secret: &str
|
||||||
|
) -> Result<Scrobbler, Box<dyn std::error::Error>> {
|
||||||
|
let token = {
|
||||||
|
tokio::runtime::Builder::new_current_thread()
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
.block_on(
|
||||||
|
async {
|
||||||
|
reqwest::get(
|
||||||
|
format!("http://ws.audioscrobbler.com/2.0/?method=auth.gettoken&api_key={api_key}&format=json"))
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.json::<Token>()
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
};
|
||||||
|
let mut scrobbler = Scrobbler::new(api_key, api_secret);
|
||||||
|
println!("Token: {}", token.token);
|
||||||
|
opener::open_browser(format!("http://www.last.fm/api/auth/?api_key={api_key}&token={}", token.token)).unwrap();
|
||||||
|
|
||||||
|
let session = loop {
|
||||||
|
if let Ok(session) = scrobbler.authenticate_with_token(&token.token) {
|
||||||
|
break session;
|
||||||
|
}
|
||||||
|
sleep(Duration::from_millis(1000));
|
||||||
|
};
|
||||||
|
println!("Session: {}", session.key);
|
||||||
|
|
||||||
|
config.write().connections.last_fm_session = Some(session.key);
|
||||||
|
Ok(scrobbler)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct Token {
|
||||||
|
token: String,
|
||||||
}
|
}
|
|
@ -20,11 +20,12 @@ use thiserror::Error;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::config::ConfigError;
|
use crate::config::ConfigError;
|
||||||
|
use crate::music_controller::connections::handle_connections;
|
||||||
use crate::music_storage::library::Song;
|
use crate::music_storage::library::Song;
|
||||||
use crate::music_storage::playlist::{ExternalPlaylist, Playlist};
|
use crate::music_storage::playlist::{ExternalPlaylist, Playlist};
|
||||||
use crate::{config::Config, music_storage::library::MusicLibrary};
|
use crate::{config::Config, music_storage::library::MusicLibrary};
|
||||||
|
|
||||||
use super::connections::{ConnectionsInput, ConnectionsNotification, ControllerConnections};
|
use super::connections::{ConnectionsNotification, ControllerConnections};
|
||||||
use super::controller_handle::{LibraryCommandInput, PlayerCommandInput, QueueCommandInput};
|
use super::controller_handle::{LibraryCommandInput, PlayerCommandInput, QueueCommandInput};
|
||||||
use super::queue::{QueueAlbum, QueueSong};
|
use super::queue::{QueueAlbum, QueueSong};
|
||||||
|
|
||||||
|
@ -163,7 +164,6 @@ pub struct ControllerInput {
|
||||||
config: Arc<RwLock<Config>>,
|
config: Arc<RwLock<Config>>,
|
||||||
playback_info: Arc<AtomicCell<PlaybackInfo>>,
|
playback_info: Arc<AtomicCell<PlaybackInfo>>,
|
||||||
notify_next_song: Sender<Song>,
|
notify_next_song: Sender<Song>,
|
||||||
connections: Option<ConnectionsInput>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ControllerHandle {
|
pub struct ControllerHandle {
|
||||||
|
@ -176,7 +176,6 @@ impl ControllerHandle {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
library: MusicLibrary,
|
library: MusicLibrary,
|
||||||
config: Arc<RwLock<Config>>,
|
config: Arc<RwLock<Config>>,
|
||||||
connections: Option<ConnectionsInput>,
|
|
||||||
) -> (
|
) -> (
|
||||||
Self,
|
Self,
|
||||||
ControllerInput,
|
ControllerInput,
|
||||||
|
@ -202,7 +201,6 @@ impl ControllerHandle {
|
||||||
config,
|
config,
|
||||||
playback_info: Arc::clone(&playback_info),
|
playback_info: Arc::clone(&playback_info),
|
||||||
notify_next_song: notify_next_song.0,
|
notify_next_song: notify_next_song.0,
|
||||||
connections,
|
|
||||||
},
|
},
|
||||||
playback_info,
|
playback_info,
|
||||||
notify_next_song.1,
|
notify_next_song.1,
|
||||||
|
@ -254,7 +252,6 @@ impl Controller {
|
||||||
config,
|
config,
|
||||||
playback_info,
|
playback_info,
|
||||||
notify_next_song,
|
notify_next_song,
|
||||||
connections,
|
|
||||||
}: ControllerInput,
|
}: ControllerInput,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
let queue: Queue<QueueSong, QueueAlbum> = Queue {
|
let queue: Queue<QueueSong, QueueAlbum> = Queue {
|
||||||
|
@ -322,6 +319,7 @@ impl Controller {
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let _notifications_rx = notifications_rx.clone();
|
||||||
let c = scope.spawn(|| {
|
let c = scope.spawn(|| {
|
||||||
Controller::player_monitor_loop(
|
Controller::player_monitor_loop(
|
||||||
player_state,
|
player_state,
|
||||||
|
@ -330,27 +328,26 @@ impl Controller {
|
||||||
finished_tx,
|
finished_tx,
|
||||||
player_mail.0,
|
player_mail.0,
|
||||||
notify_next_song,
|
notify_next_song,
|
||||||
notifications_rx,
|
_notifications_rx,
|
||||||
playback_info,
|
playback_info,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(inner) = connections {
|
|
||||||
dbg!(&inner);
|
|
||||||
let d = scope.spawn(|| {
|
let d = scope.spawn(|| {
|
||||||
Controller::handle_connections(
|
handle_connections(
|
||||||
config,
|
config,
|
||||||
ControllerConnections {
|
ControllerConnections {
|
||||||
|
notifications_rx,
|
||||||
notifications_tx,
|
notifications_tx,
|
||||||
inner,
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
a.join().unwrap();
|
a.join().unwrap();
|
||||||
b.join().unwrap();
|
b.join().unwrap();
|
||||||
c.join().unwrap();
|
c.join().unwrap();
|
||||||
|
d.join().unwrap();
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue