mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Refactor 'find_available_alternatives'
This commit is contained in:
parent
5aeb733ad9
commit
45f42acb82
2 changed files with 15 additions and 16 deletions
|
@ -19,7 +19,7 @@ version = "0.1.6"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures-executor = { version = "0.3", default_features = false }
|
futures-executor = { version = "0.3", default_features = false }
|
||||||
futures-util = { version = "0.3", default_features = false }
|
futures-util = { version = "0.3", default_features = false, features = ["alloc"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
byteorder = "1.4"
|
byteorder = "1.4"
|
||||||
shell-words = "1.0.0"
|
shell-words = "1.0.0"
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use std::borrow::Cow;
|
|
||||||
use std::cmp::max;
|
use std::cmp::max;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::io::{self, Read, Seek, SeekFrom};
|
use std::io::{self, Read, Seek, SeekFrom};
|
||||||
|
@ -8,7 +7,8 @@ use std::time::{Duration, Instant};
|
||||||
use std::{mem, thread};
|
use std::{mem, thread};
|
||||||
|
|
||||||
use byteorder::{LittleEndian, ReadBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt};
|
||||||
use futures_util::{future, TryFutureExt};
|
use futures_util::stream::futures_unordered::FuturesUnordered;
|
||||||
|
use futures_util::{future, StreamExt, TryFutureExt};
|
||||||
use tokio::sync::{mpsc, oneshot};
|
use tokio::sync::{mpsc, oneshot};
|
||||||
|
|
||||||
use crate::audio::{AudioDecoder, AudioError, AudioPacket, PassthroughDecoder, VorbisDecoder};
|
use crate::audio::{AudioDecoder, AudioError, AudioPacket, PassthroughDecoder, VorbisDecoder};
|
||||||
|
@ -576,21 +576,20 @@ struct PlayerTrackLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PlayerTrackLoader {
|
impl PlayerTrackLoader {
|
||||||
async fn find_available_alternative<'a, 'b>(
|
async fn find_available_alternative(&self, audio: AudioItem) -> Option<AudioItem> {
|
||||||
&'a self,
|
|
||||||
audio: &'b AudioItem,
|
|
||||||
) -> Option<Cow<'b, AudioItem>> {
|
|
||||||
if audio.available {
|
if audio.available {
|
||||||
Some(Cow::Borrowed(audio))
|
Some(audio)
|
||||||
} else if let Some(alternatives) = &audio.alternatives {
|
} else if let Some(alternatives) = &audio.alternatives {
|
||||||
let alternatives = alternatives
|
let alternatives: FuturesUnordered<_> = alternatives
|
||||||
.iter()
|
.iter()
|
||||||
.map(|alt_id| AudioItem::get_audio_item(&self.session, *alt_id));
|
.map(|alt_id| AudioItem::get_audio_item(&self.session, *alt_id))
|
||||||
let alternatives = future::try_join_all(alternatives).await.unwrap();
|
.collect();
|
||||||
|
|
||||||
alternatives
|
alternatives
|
||||||
.into_iter()
|
.filter_map(|x| future::ready(x.ok()))
|
||||||
.find(|alt| alt.available)
|
.filter(|x| future::ready(x.available))
|
||||||
.map(Cow::Owned)
|
.next()
|
||||||
|
.await
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -630,10 +629,10 @@ impl PlayerTrackLoader {
|
||||||
|
|
||||||
info!("Loading <{}> with Spotify URI <{}>", audio.name, audio.uri);
|
info!("Loading <{}> with Spotify URI <{}>", audio.name, audio.uri);
|
||||||
|
|
||||||
let audio = match self.find_available_alternative(&audio).await {
|
let audio = match self.find_available_alternative(audio).await {
|
||||||
Some(audio) => audio,
|
Some(audio) => audio,
|
||||||
None => {
|
None => {
|
||||||
warn!("<{}> is not available", audio.uri);
|
warn!("<{}> is not available", spotify_id.to_uri());
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue