adapt to new rspotify structures

these changes are necessary to handle playlists with local tracks properly
This commit is contained in:
Henrik Friedrichsen
2019-04-17 18:25:00 +02:00
parent 0bbba3c57d
commit 0de6ad2dfe
6 changed files with 64 additions and 29 deletions

View File

@@ -10,12 +10,12 @@ use traits::ListItem;
#[derive(Clone, Deserialize, Serialize)] #[derive(Clone, Deserialize, Serialize)]
pub struct Album { pub struct Album {
pub id: String, pub id: Option<String>,
pub title: String, pub title: String,
pub artists: Vec<String>, pub artists: Vec<String>,
pub year: String, pub year: String,
pub cover_url: Option<String>, pub cover_url: Option<String>,
pub url: String, pub url: Option<String>,
pub tracks: Option<Vec<Track>>, pub tracks: Option<Vec<Track>>,
} }
@@ -25,14 +25,16 @@ impl Album {
return; return;
} }
if let Some(fa) = spotify.full_album(&self.id) { if let Some(ref album_id) = self.id {
self.tracks = Some( if let Some(fa) = spotify.full_album(&album_id) {
fa.tracks self.tracks = Some(
.items fa.tracks
.iter() .items
.map(|st| Track::from_simplified_track(&st, &fa)) .iter()
.collect(), .map(|st| Track::from_simplified_track(&st, &fa))
); .collect(),
);
}
} }
} }
} }
@@ -43,7 +45,14 @@ impl From<&SimplifiedAlbum> for Album {
id: sa.id.clone(), id: sa.id.clone(),
title: sa.name.clone(), title: sa.name.clone(),
artists: sa.artists.iter().map(|sa| sa.name.clone()).collect(), artists: sa.artists.iter().map(|sa| sa.name.clone()).collect(),
year: sa.release_date.split('-').next().unwrap().into(), year: sa
.release_date
.clone()
.unwrap_or_default()
.split('-')
.next()
.unwrap()
.into(),
cover_url: sa.images.get(0).map(|i| i.url.clone()), cover_url: sa.images.get(0).map(|i| i.url.clone()),
url: sa.uri.clone(), url: sa.uri.clone(),
tracks: None, tracks: None,
@@ -62,12 +71,12 @@ impl From<&FullAlbum> for Album {
); );
Self { Self {
id: fa.id.clone(), id: Some(fa.id.clone()),
title: fa.name.clone(), title: fa.name.clone(),
artists: fa.artists.iter().map(|sa| sa.name.clone()).collect(), artists: fa.artists.iter().map(|sa| sa.name.clone()).collect(),
year: fa.release_date.split('-').next().unwrap().into(), year: fa.release_date.split('-').next().unwrap().into(),
cover_url: fa.images.get(0).map(|i| i.url.clone()), cover_url: fa.images.get(0).map(|i| i.url.clone()),
url: fa.uri.clone(), url: Some(fa.uri.clone()),
tracks, tracks,
} }
} }
@@ -83,7 +92,7 @@ impl fmt::Debug for Album {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!( write!(
f, f,
"({} - {} ({}))", "({} - {} ({:?}))",
self.artists.join(", "), self.artists.join(", "),
self.title, self.title,
self.id self.id
@@ -99,9 +108,14 @@ impl ListItem for Album {
.read() .read()
.unwrap() .unwrap()
.iter() .iter()
.map(|t| t.id.clone()) .filter(|t| t.id.is_some())
.map(|t| t.id.clone().unwrap())
.collect();
let ids: Vec<String> = tracks
.iter()
.filter(|t| t.id.is_some())
.map(|t| t.id.clone().unwrap())
.collect(); .collect();
let ids: Vec<String> = tracks.iter().map(|t| t.id.clone()).collect();
!ids.is_empty() && playing == ids !ids.is_empty() && playing == ids
} else { } else {
false false

View File

@@ -31,8 +31,10 @@ impl Artist {
continue; continue;
} }
if let Some(fa) = spotify.full_album(&sa.id).as_ref() { if let Some(album_id) = sa.id {
albums.push(fa.into()); if let Some(fa) = spotify.full_album(&album_id).as_ref() {
albums.push(fa.into());
}
} }
} }
@@ -86,9 +88,14 @@ impl ListItem for Artist {
.read() .read()
.unwrap() .unwrap()
.iter() .iter()
.map(|t| t.id.clone()) .filter(|t| t.id.is_some())
.map(|t| t.id.clone().unwrap())
.collect();
let ids: Vec<String> = tracks
.iter()
.filter(|t| t.id.is_some())
.map(|t| t.id.clone().unwrap())
.collect(); .collect();
let ids: Vec<String> = tracks.iter().map(|t| t.id.clone()).collect();
!ids.is_empty() && playing == ids !ids.is_empty() && playing == ids
} else { } else {
false false

View File

@@ -29,7 +29,7 @@ fn get_metadata(queue: Arc<Queue>) -> HashMap<String, Variant<Box<RefArg>>> {
"mpris:trackid".to_string(), "mpris:trackid".to_string(),
Variant(Box::new( Variant(Box::new(
track track
.map(|t| format!("spotify:track:{}", t.id)) .map(|t| format!("spotify:track:{}", t.id.clone().unwrap_or("0".to_string())))
.unwrap_or_default(), .unwrap_or_default(),
)), )),
); );

View File

@@ -37,9 +37,15 @@ impl ListItem for Playlist {
.read() .read()
.unwrap() .unwrap()
.iter() .iter()
.map(|t| t.id.clone()) .filter(|t| t.id.is_some())
.map(|t| t.id.clone().unwrap())
.collect();
let ids: Vec<String> = self
.tracks
.iter()
.filter(|t| t.id.is_some())
.map(|t| t.id.clone().unwrap())
.collect(); .collect();
let ids: Vec<String> = self.tracks.iter().map(|t| t.id.clone()).collect();
!ids.is_empty() && playing == ids !ids.is_empty() && playing == ids
} }

View File

@@ -123,9 +123,13 @@ impl futures::Future for Worker {
debug!("message received!"); debug!("message received!");
match cmd { match cmd {
WorkerCommand::Load(track) => { WorkerCommand::Load(track) => {
let id = SpotifyId::from_base62(&track.id).expect("could not parse id"); if let Some(track_id) = &track.id {
self.play_task = Box::new(self.player.load(id, false, 0)); let id = SpotifyId::from_base62(track_id).expect("could not parse id");
info!("player loading track: {:?}", track); self.play_task = Box::new(self.player.load(id, false, 0));
info!("player loading track: {:?}", track);
} else {
self.events.send(Event::Player(PlayerEvent::FinishedTrack));
}
} }
WorkerCommand::Play => { WorkerCommand::Play => {
self.player.play(); self.player.play();
@@ -407,7 +411,11 @@ impl Spotify {
pub fn overwrite_playlist(&self, id: &str, tracks: &[Track]) { pub fn overwrite_playlist(&self, id: &str, tracks: &[Track]) {
// extract only track IDs // extract only track IDs
let mut tracks: Vec<String> = tracks.iter().map(|track| track.id.clone()).collect(); let mut tracks: Vec<String> = tracks
.iter()
.filter(|track| track.id.is_some())
.map(|track| track.id.clone().unwrap())
.collect();
// we can only send 100 tracks per request // we can only send 100 tracks per request
let mut remainder = if tracks.len() > 100 { let mut remainder = if tracks.len() > 100 {

View File

@@ -9,7 +9,7 @@ use traits::ListItem;
#[derive(Clone, Deserialize, Serialize)] #[derive(Clone, Deserialize, Serialize)]
pub struct Track { pub struct Track {
pub id: String, pub id: Option<String>,
pub title: String, pub title: String,
pub track_number: u32, pub track_number: u32,
pub disc_number: i32, pub disc_number: i32,
@@ -104,7 +104,7 @@ impl fmt::Debug for Track {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!( write!(
f, f,
"({} - {} ({}))", "({} - {} ({:?}))",
self.artists.join(", "), self.artists.join(", "),
self.title, self.title,
self.id self.id