2021-12-26 20:18:42 +00:00
|
|
|
use std::{
|
|
|
|
fmt::Debug,
|
2022-08-02 10:45:37 +00:00
|
|
|
ops::{Deref, DerefMut},
|
2021-12-26 20:18:42 +00:00
|
|
|
};
|
2021-12-07 22:22:24 +00:00
|
|
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
artist::{Artists, ArtistsWithRole},
|
2022-08-23 20:23:37 +00:00
|
|
|
audio::file::AudioFiles,
|
|
|
|
availability::Availabilities,
|
2021-12-07 22:22:24 +00:00
|
|
|
content_rating::ContentRatings,
|
|
|
|
external_id::ExternalIds,
|
|
|
|
restriction::Restrictions,
|
|
|
|
sale_period::SalePeriods,
|
2022-08-02 12:21:07 +00:00
|
|
|
util::{impl_deref_wrapped, impl_try_from_repeated},
|
2022-06-29 22:22:27 +00:00
|
|
|
Album, Metadata, RequestResult,
|
2021-12-07 22:22:24 +00:00
|
|
|
};
|
|
|
|
|
2021-12-26 20:18:42 +00:00
|
|
|
use librespot_core::{date::Date, Error, Session, SpotifyId};
|
2021-12-07 22:22:24 +00:00
|
|
|
use librespot_protocol as protocol;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Track {
|
|
|
|
pub id: SpotifyId,
|
|
|
|
pub name: String,
|
2022-06-29 22:22:27 +00:00
|
|
|
pub album: Album,
|
2021-12-07 22:22:24 +00:00
|
|
|
pub artists: Artists,
|
|
|
|
pub number: i32,
|
|
|
|
pub disc_number: i32,
|
|
|
|
pub duration: i32,
|
|
|
|
pub popularity: i32,
|
|
|
|
pub is_explicit: bool,
|
|
|
|
pub external_ids: ExternalIds,
|
|
|
|
pub restrictions: Restrictions,
|
|
|
|
pub files: AudioFiles,
|
|
|
|
pub alternatives: Tracks,
|
|
|
|
pub sale_periods: SalePeriods,
|
|
|
|
pub previews: AudioFiles,
|
|
|
|
pub tags: Vec<String>,
|
|
|
|
pub earliest_live_timestamp: Date,
|
|
|
|
pub has_lyrics: bool,
|
|
|
|
pub availability: Availabilities,
|
|
|
|
pub licensor: Uuid,
|
|
|
|
pub language_of_performance: Vec<String>,
|
|
|
|
pub content_ratings: ContentRatings,
|
|
|
|
pub original_title: String,
|
|
|
|
pub version_title: String,
|
|
|
|
pub artists_with_role: ArtistsWithRole,
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:44:43 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2021-12-07 22:22:24 +00:00
|
|
|
pub struct Tracks(pub Vec<SpotifyId>);
|
|
|
|
|
2022-08-02 10:45:37 +00:00
|
|
|
impl_deref_wrapped!(Tracks, Vec<SpotifyId>);
|
2021-12-07 22:22:24 +00:00
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Metadata for Track {
|
|
|
|
type Message = protocol::metadata::Track;
|
|
|
|
|
2022-08-03 20:26:52 +00:00
|
|
|
async fn request(session: &Session, track_id: &SpotifyId) -> RequestResult {
|
2021-12-26 20:18:42 +00:00
|
|
|
session.spclient().get_track_metadata(track_id).await
|
2021-12-07 22:22:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 20:26:52 +00:00
|
|
|
fn parse(msg: &Self::Message, _: &SpotifyId) -> Result<Self, Error> {
|
2021-12-07 22:22:24 +00:00
|
|
|
Self::try_from(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&<Self as Metadata>::Message> for Track {
|
2021-12-26 20:18:42 +00:00
|
|
|
type Error = librespot_core::Error;
|
2021-12-07 22:22:24 +00:00
|
|
|
fn try_from(track: &<Self as Metadata>::Message) -> Result<Self, Self::Error> {
|
|
|
|
Ok(Self {
|
|
|
|
id: track.try_into()?,
|
2023-01-17 20:46:14 +00:00
|
|
|
name: track.name().to_owned(),
|
|
|
|
album: track.album.get_or_default().try_into()?,
|
|
|
|
artists: track.artist.as_slice().try_into()?,
|
|
|
|
number: track.number(),
|
|
|
|
disc_number: track.disc_number(),
|
|
|
|
duration: track.duration(),
|
|
|
|
popularity: track.popularity(),
|
|
|
|
is_explicit: track.explicit(),
|
|
|
|
external_ids: track.external_id.as_slice().into(),
|
|
|
|
restrictions: track.restriction.as_slice().into(),
|
|
|
|
files: track.file.as_slice().into(),
|
|
|
|
alternatives: track.alternative.as_slice().try_into()?,
|
|
|
|
sale_periods: track.sale_period.as_slice().try_into()?,
|
|
|
|
previews: track.preview.as_slice().into(),
|
|
|
|
tags: track.tags.to_vec(),
|
|
|
|
earliest_live_timestamp: Date::from_timestamp_ms(track.earliest_live_timestamp())?,
|
|
|
|
has_lyrics: track.has_lyrics(),
|
|
|
|
availability: track.availability.as_slice().try_into()?,
|
|
|
|
licensor: Uuid::from_slice(track.licensor.uuid()).unwrap_or_else(|_| Uuid::nil()),
|
|
|
|
language_of_performance: track.language_of_performance.to_vec(),
|
|
|
|
content_ratings: track.content_rating.as_slice().into(),
|
|
|
|
original_title: track.original_title().to_owned(),
|
|
|
|
version_title: track.version_title().to_owned(),
|
|
|
|
artists_with_role: track.artist_with_role.as_slice().try_into()?,
|
2021-12-07 22:22:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 12:21:07 +00:00
|
|
|
impl_try_from_repeated!(<Track as Metadata>::Message, Tracks);
|