librespot/src/metadata.rs

131 lines
3.2 KiB
Rust
Raw Normal View History

use eventual::{Async, Future};
2015-12-28 00:29:53 +00:00
use protobuf;
2015-06-23 14:38:29 +00:00
use librespot_protocol as protocol;
use mercury::{MercuryRequest, MercuryMethod};
use util::{SpotifyId, FileId};
use session::Session;
2015-06-23 14:38:29 +00:00
pub trait MetadataTrait : Send + 'static {
2015-06-23 14:38:29 +00:00
type Message: protobuf::MessageStatic;
2015-06-23 14:38:29 +00:00
fn base_url() -> &'static str;
fn parse(msg: &Self::Message) -> Self;
2015-06-23 14:38:29 +00:00
}
#[derive(Debug)]
pub struct Track {
pub id: SpotifyId,
2015-06-23 14:38:29 +00:00
pub name: String,
pub album: SpotifyId,
pub files: Vec<FileId>,
}
#[derive(Debug)]
pub struct Album {
pub id: SpotifyId,
pub name: String,
pub artists: Vec<SpotifyId>,
pub covers: Vec<FileId>
2015-06-23 14:38:29 +00:00
}
#[derive(Debug)]
pub struct Artist {
pub id: SpotifyId,
pub name: String,
}
pub type MetadataRef<T> = Future<T, ()>;
pub type TrackRef = MetadataRef<Track>;
pub type AlbumRef = MetadataRef<Album>;
pub type ArtistRef = MetadataRef<Artist>;
2015-06-23 14:38:29 +00:00
impl MetadataTrait for Track {
type Message = protocol::metadata::Track;
fn base_url() -> &'static str {
"hm://metadata/3/track"
}
fn parse(msg: &Self::Message) -> Self {
2015-06-23 14:38:29 +00:00
Track {
id: SpotifyId::from_raw(msg.get_gid()),
2015-09-01 11:20:37 +00:00
name: msg.get_name().to_owned(),
2015-06-23 14:38:29 +00:00
album: SpotifyId::from_raw(msg.get_album().get_gid()),
files: msg.get_file().iter()
.map(|file| {
let mut dst = [0u8; 20];
2015-12-28 00:29:53 +00:00
dst.clone_from_slice(&file.get_file_id());
2015-07-07 21:40:31 +00:00
FileId(dst)
2015-06-23 14:38:29 +00:00
})
.collect(),
}
}
}
impl MetadataTrait for Album {
type Message = protocol::metadata::Album;
fn base_url() -> &'static str {
"hm://metadata/3/album"
}
fn parse(msg: &Self::Message) -> Self {
2015-06-23 14:38:29 +00:00
Album {
id: SpotifyId::from_raw(msg.get_gid()),
2015-09-01 11:20:37 +00:00
name: msg.get_name().to_owned(),
2015-06-23 14:38:29 +00:00
artists: msg.get_artist().iter()
.map(|a| SpotifyId::from_raw(a.get_gid()))
.collect(),
covers: msg.get_cover_group().get_image().iter()
.map(|image| {
let mut dst = [0u8; 20];
2015-12-28 00:29:53 +00:00
dst.clone_from_slice(&image.get_file_id());
2015-07-07 21:40:31 +00:00
FileId(dst)
2015-06-23 14:38:29 +00:00
})
.collect(),
}
}
}
impl MetadataTrait for Artist {
type Message = protocol::metadata::Artist;
2015-06-23 14:38:29 +00:00
fn base_url() -> &'static str {
"hm://metadata/3/artist"
}
fn parse(msg: &Self::Message) -> Self {
Artist {
id: SpotifyId::from_raw(msg.get_gid()),
name: msg.get_name().to_owned(),
2015-06-23 14:38:29 +00:00
}
}
}
pub struct MetadataManager;
2015-06-23 14:38:29 +00:00
impl MetadataManager {
pub fn new() -> MetadataManager {
MetadataManager
2015-06-23 14:38:29 +00:00
}
pub fn get<T: MetadataTrait>(&mut self, session: &Session, id: SpotifyId)
-> MetadataRef<T> {
session.mercury(MercuryRequest {
method: MercuryMethod::GET,
uri: format!("{}/{}", T::base_url(), id.to_base16()),
content_type: None,
payload: Vec::new()
}).and_then(|response| {
2015-06-23 14:38:29 +00:00
let msg : T::Message = protobuf::parse_from_bytes(
2015-09-01 11:20:37 +00:00
response.payload.first().unwrap()).unwrap();
2015-06-23 14:38:29 +00:00
Ok(T::parse(&msg))
})
2015-06-23 14:38:29 +00:00
}
}