Skip to content

Commit

Permalink
Get track availability and alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar committed Dec 28, 2015
1 parent 5db1410 commit 4f518fc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
8 changes: 4 additions & 4 deletions protocol/proto/metadata.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ message Album {
optional bytes gid = 0x1;
optional string name = 0x2;
repeated Artist artist = 0x3;
optional Type type = 0x4;
optional Type typ = 0x4;
enum Type {
ALBUM = 0x1;
SINGLE = 0x2;
Expand Down Expand Up @@ -112,7 +112,7 @@ message Disc {
}

message Copyright {
optional Type type = 0x1;
optional Type typ = 0x1;
enum Type {
P = 0x0;
C = 0x1;
Expand All @@ -123,7 +123,7 @@ message Copyright {
message Restriction {
optional string countries_allowed = 0x2;
optional string countries_forbidden = 0x3;
optional Type type = 0x4;
optional Type typ = 0x4;
enum Type {
STREAMING = 0x0;
}
Expand All @@ -137,7 +137,7 @@ message SalePeriod {
}

message ExternalId {
optional string type = 0x1;
optional string typ = 0x1;
optional string id = 0x2;
}

Expand Down
21 changes: 20 additions & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@ use protobuf;

use librespot_protocol as protocol;
use mercury::{MercuryRequest, MercuryMethod};
use util::{SpotifyId, FileId};
use util::{SpotifyId, FileId, StrChunksExt};
use session::Session;

fn countrylist_contains(list: &str, country: &str) -> bool {
list.chunks(2).any(|cc| cc == country)
}

fn parse_restrictions<'s, I>(restrictions: I, country: &str, catalogue: &str) -> bool
where I : Iterator<Item=&'s protocol::metadata::Restriction> {
restrictions
.filter(|r| r.get_catalogue_str().contains(&catalogue.to_owned()))
.all(|r| !countrylist_contains(r.get_countries_forbidden(), country)
&& (!r.has_countries_allowed()
|| countrylist_contains(r.get_countries_allowed(), country)))
}

pub trait MetadataTrait : Send + 'static {
type Message: protobuf::MessageStatic;

Expand All @@ -19,6 +32,8 @@ pub struct Track {
pub name: String,
pub album: SpotifyId,
pub files: Vec<FileId>,
pub alternatives: Vec<SpotifyId>,
pub available: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -59,6 +74,10 @@ impl MetadataTrait for Track {
FileId(dst)
})
.collect(),
alternatives: msg.get_alternative().iter()
.map(|alt| SpotifyId::from_raw(alt.get_gid()))
.collect(),
available: parse_restrictions(msg.get_restriction().iter(), "FR", "premium"),
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,29 @@ pub fn powm(base: &BigUint, exp: &BigUint, modulus: &BigUint) -> BigUint {
return result;
}

pub struct StrChunks<'s>(&'s str, usize);

pub trait StrChunksExt {
fn chunks<'s>(&'s self, size: usize) -> StrChunks<'s>;
}

impl StrChunksExt for str {
fn chunks<'a>(&'a self, size: usize) -> StrChunks<'a> {
StrChunks(self, size)
}
}

impl <'s> Iterator for StrChunks<'s> {
type Item = &'s str;
fn next(&mut self) -> Option<&'s str> {
let &mut StrChunks(data, size) = self;
if data.is_empty() {
None
} else {
let ret = Some(&data[..size]);
self.0 = &data[size..];
ret
}
}
}

0 comments on commit 4f518fc

Please sign in to comment.