Migrate ncspot to librespot 0.5 breaking changes

- Set `client_id` via `SessionConfig`
- Use `TokenProvider` to obtain client token instead of custom Mercury call
- Other minor changes
This commit is contained in:
Henrik Friedrichsen
2023-07-26 02:02:48 +02:00
parent 4bebdef81b
commit ace23462f7
4 changed files with 36 additions and 48 deletions

View File

@@ -9,7 +9,6 @@ use librespot_core::authentication::Credentials;
use librespot_core::cache::Cache;
use librespot_core::config::SessionConfig;
use librespot_core::session::Session;
use librespot_core::session::SessionError;
use librespot_playback::audio_backend;
use librespot_playback::audio_backend::SinkBuilder;
use librespot_playback::config::Bitrate;
@@ -129,7 +128,10 @@ impl Spotify {
/// Generate the librespot [SessionConfig] used when creating a [Session].
pub fn session_config(cfg: &config::Config) -> SessionConfig {
let mut session_config = SessionConfig::default();
let mut session_config = librespot_core::SessionConfig {
client_id: config::CLIENT_ID.to_string(),
..Default::default()
};
match env::var("http_proxy") {
Ok(proxy) => {
info!("Setting HTTP proxy {}", proxy);
@@ -143,17 +145,18 @@ impl Spotify {
session_config
}
/// Test whether `credentials` are valid Spotify credentials.
pub fn test_credentials(
cfg: &config::Config,
credentials: Credentials,
) -> Result<Session, SessionError> {
) -> Result<Session, librespot_core::Error> {
let config = Self::session_config(cfg);
let _guard = ASYNC_RUNTIME.get().unwrap().enter();
let session = Session::new(config, None);
ASYNC_RUNTIME
.get()
.unwrap()
.block_on(Session::connect(config, credentials, None, true))
.map(|r| r.0)
.block_on(session.connect(credentials, true))
.map(|_| session)
}
/// Create a [Session] that respects the user configuration in `cfg` and with the given
@@ -161,7 +164,7 @@ impl Spotify {
async fn create_session(
cfg: &config::Config,
credentials: Credentials,
) -> Result<Session, SessionError> {
) -> Result<Session, librespot_core::Error> {
let librespot_cache_path = config::cache_path("librespot");
let audio_cache_path = if let Some(false) = cfg.values().audio_cache {
None
@@ -179,9 +182,8 @@ impl Spotify {
.expect("Could not create cache");
debug!("opening spotify session");
let session_config = Self::session_config(cfg);
Session::connect(session_config, credentials, Some(cache), true)
.await
.map(|r| r.0)
let session = Session::new(session_config, Some(cache));
session.connect(credentials, true).await.map(|_| session)
}
/// Create and initialize the requested audio backend.
@@ -248,12 +250,13 @@ impl Spotify {
mixer.set_volume(volume);
let audio_format: librespot_playback::config::AudioFormat = Default::default();
let (player, player_events) = Player::new(
let player = Player::new(
player_config,
session.clone(),
mixer.get_soft_volume(),
move || (backend)(cfg.values().backend_device.clone(), audio_format),
);
let player_events = player.get_player_event_channel();
let mut worker = Worker::new(
events.clone(),