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

View File

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

View File

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

View File

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

View File

@@ -123,9 +123,13 @@ impl futures::Future for Worker {
debug!("message received!");
match cmd {
WorkerCommand::Load(track) => {
let id = SpotifyId::from_base62(&track.id).expect("could not parse id");
self.play_task = Box::new(self.player.load(id, false, 0));
info!("player loading track: {:?}", track);
if let Some(track_id) = &track.id {
let id = SpotifyId::from_base62(track_id).expect("could not parse id");
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 => {
self.player.play();
@@ -407,7 +411,11 @@ impl Spotify {
pub fn overwrite_playlist(&self, id: &str, tracks: &[Track]) {
// 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
let mut remainder = if tracks.len() > 100 {

View File

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