diff --git a/src/metadata.rs b/src/metadata.rs index 51b1ff47..f9c50e24 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -23,7 +23,7 @@ pub trait MetadataTrait : Send + 'static { type Message: protobuf::MessageStatic; fn base_url() -> &'static str; - fn parse(msg: &Self::Message) -> Self; + fn parse(msg: &Self::Message, session: &Session) -> Self; } #[derive(Debug)] @@ -62,7 +62,7 @@ impl MetadataTrait for Track { "hm://metadata/3/track" } - fn parse(msg: &Self::Message) -> Self { + fn parse(msg: &Self::Message, session: &Session) -> Self { Track { id: SpotifyId::from_raw(msg.get_gid()), name: msg.get_name().to_owned(), @@ -77,7 +77,10 @@ impl MetadataTrait for Track { alternatives: msg.get_alternative().iter() .map(|alt| SpotifyId::from_raw(alt.get_gid())) .collect(), - available: parse_restrictions(msg.get_restriction().iter(), "FR", "premium"), + available: parse_restrictions( + msg.get_restriction().iter(), + &session.0.data.read().unwrap().country, + "premium"), } } } @@ -89,7 +92,7 @@ impl MetadataTrait for Album { "hm://metadata/3/album" } - fn parse(msg: &Self::Message) -> Self { + fn parse(msg: &Self::Message, session: &Session) -> Self { Album { id: SpotifyId::from_raw(msg.get_gid()), name: msg.get_name().to_owned(), @@ -115,7 +118,7 @@ impl MetadataTrait for Artist { "hm://metadata/3/artist" } - fn parse(msg: &Self::Message) -> Self { + fn parse(msg: &Self::Message, session: &Session) -> Self { Artist { id: SpotifyId::from_raw(msg.get_gid()), name: msg.get_name().to_owned(), @@ -131,18 +134,19 @@ impl MetadataManager { } pub fn get(&mut self, session: &Session, id: SpotifyId) - -> MetadataRef { + -> MetadataRef { + let _session = session.clone(); session.mercury(MercuryRequest { method: MercuryMethod::GET, uri: format!("{}/{}", T::base_url(), id.to_base16()), content_type: None, payload: Vec::new() - }).and_then(|response| { + }).and_then(move |response| { let msg : T::Message = protobuf::parse_from_bytes( response.payload.first().unwrap()).unwrap(); - Ok(T::parse(&msg)) + Ok(T::parse(&msg, &_session)) }) } } diff --git a/src/session.rs b/src/session.rs index 613c4456..27275cef 100644 --- a/src/session.rs +++ b/src/session.rs @@ -3,7 +3,7 @@ use crypto::sha1::Sha1; use eventual::Future; use protobuf::{self, Message}; use rand::thread_rng; -use std::sync::{Mutex, Arc, mpsc}; +use std::sync::{Mutex, RwLock, Arc, mpsc}; use std::path::PathBuf; use connection::{self, PlainConnection, CipherConnection}; @@ -28,7 +28,12 @@ pub struct Config { } pub struct SessionData { + pub country: String, +} + +pub struct SessionInternal { pub config: Config, + pub data: RwLock, mercury: Mutex, metadata: Mutex, @@ -40,7 +45,7 @@ pub struct SessionData { } #[derive(Clone)] -pub struct Session(pub Arc); +pub struct Session(pub Arc); impl Session { pub fn new(mut config: Config) -> Session { @@ -116,8 +121,11 @@ impl Session { let cipher_connection = connection.setup_cipher(shared_keys); - Session(Arc::new(SessionData { + Session(Arc::new(SessionInternal { config: config, + data: RwLock::new(SessionData { + country: String::new(), + }), rx_connection: Mutex::new(cipher_connection.clone()), tx_connection: Mutex::new(cipher_connection), @@ -165,6 +173,11 @@ impl Session { 0x4a => (), 0x9 => self.0.stream.lock().unwrap().handle(cmd, data), 0xd | 0xe => self.0.audio_key.lock().unwrap().handle(cmd, data), + 0x1b => { + self.0.data.write().unwrap().country = + String::from_utf8(data).unwrap(); + }, + 0xb2...0xb6 => self.0.mercury.lock().unwrap().handle(cmd, data), 0xac => eprintln!("Authentication succeedded"), 0xad => eprintln!("Authentication failed"), @@ -175,7 +188,7 @@ impl Session { pub fn send_packet(&self, cmd: u8, data: &[u8]) -> connection::Result<()> { self.0.tx_connection.lock().unwrap().send_packet(cmd, data) } - + pub fn audio_key(&self, track: SpotifyId, file: FileId) -> Future { self.0.audio_key.lock().unwrap().request(self, track, file) }