Browse Source

Use the correct country when checking for track availability.

Paul Lietar 9 years ago
parent
commit
8c2aa28d43
2 changed files with 29 additions and 12 deletions
  1. 12 8
      src/metadata.rs
  2. 17 4
      src/session.rs

+ 12 - 8
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<T: MetadataTrait>(&mut self, session: &Session, id: SpotifyId)
-      -> MetadataRef<T> {
+          -> MetadataRef<T> {
 
+        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))
         })
     }
 }

+ 17 - 4
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<SessionData>,
 
     mercury: Mutex<MercuryManager>,
     metadata: Mutex<MetadataManager>,
@@ -40,7 +45,7 @@ pub struct SessionData {
 }
 
 #[derive(Clone)]
-pub struct Session(pub Arc<SessionData>);
+pub struct Session(pub Arc<SessionInternal>);
 
 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<AudioKey, ()> {
         self.0.audio_key.lock().unwrap().request(self, track, file)
     }