Skip to content

Commit

Permalink
Added some compatible algorithm, tries to select the best track possi…
Browse files Browse the repository at this point in the history
…ble for availability
  • Loading branch information
peol committed Jul 27, 2011
1 parent 741efdf commit 8eb9881
Showing 1 changed file with 57 additions and 11 deletions.
68 changes: 57 additions & 11 deletions modules/spotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var formatting = {
' (' + data.track.album.released + ')' :
'';

suffix += checkAvailability( data.track.availability );
suffix += this._availability( checkAvailability( data.track.availability ) );

return '[Track] ' + artist + ' - ' + song + suffix;
},
Expand All @@ -22,7 +22,7 @@ var formatting = {
var artist = data.album.artist,
album = data.album.name,
released = data.album.released,
suffix = checkAvailability( data.album.availability );
suffix = this._availability( checkAvailability( data.album.availability ) );

return '[Album] ' + artist + ' - ' + album + ' (' + released + ')' + suffix;
},
Expand All @@ -32,13 +32,19 @@ var formatting = {
noOfAlbums = data.artist.albums.length;

return '[Artist] ' + artist + ', featured on ' + noOfAlbums + ' albums';
}
},

_availability: function(notAvailableIn) {
return notAvailableIn.length ?
' [N/A in ' + notAvailableIn.join(', ') + ']':
'';
}
};

// Compare the availability data with our local data and return
// a string that describes which countries that this track, artist,
// or album is unavailable in. If it's available in all, it'll return
// and empty string
// an array that describes which countries that this track, artist,
// or album is unavailable in. It'll return an empty array if the
// availability string was empty, even if there (obviously) was matches.
function checkAvailability(availability) {
var i, l = availTargets.length, a = [], ret = '';

Expand All @@ -50,9 +56,35 @@ function checkAvailability(availability) {
}
}

return a.length ?
' [N/A in ' + a.join(', ') + ']':
'';
return availability.length ?
a:
[];
}

// Searches through the first five tracks and sees if there's anyone better suited
// availability-wise.
// Returns an array containing # of skipped tracks and the actual track
function findCompatibleTrack(tracks) {
var i = 0,
l = 10,
bestMatch = null,
tTrack,
tAvail;

for (; i < l; i += 1) {
tTrack = tracks[ i ];
tAvail = checkAvailability( tTrack.album && tTrack.album.availability );

if ( !bestMatch || bestMatch[ 0 ] > tAvail.length ) {
bestMatch = [ tAvail.length, tTrack ];

if ( tAvail.length === 0 ) {
break;
}
}
}

return [ i, bestMatch[ 1 ] ];
}

// Creates a ten-in-length string illustrating how popular this particular track
Expand Down Expand Up @@ -127,7 +159,8 @@ function request(path, cb) {
// querying the spotify API again to get more data on the subject
function respond(api, message) {
var type = message.match_data[ 1 ],
id = message.match_data[ 2 ];
id = message.match_data[ 2 ],
skipped = message.match_data[ 3 ];

lookUp(type, id, function(data) {
var msg = '',
Expand All @@ -136,6 +169,10 @@ function respond(api, message) {
if ( data.info ) {
msg = ' ' + formatting[ data.info.type ]( data );
popularity = data[ type ].popularity && ' Popularity: ' + createPopularity( data[ type ].popularity ) || '';
// Append the `skipped` stuff here
if ( skipped ) {
popularity += ' (skipped ' + skipped + ' unavailable tracks)';
}
}
else {
msg = 'Invalid Spotify URL (nothing found)';
Expand All @@ -160,10 +197,19 @@ exports.invoke = function(api) {
searchStr = encodeURI( message.match_data[ 2 ] );

search(type, searchStr, function(data) {
var d = data[ type + 's' ][ 0 ];
var entry,
d = data[ type + 's' ][ 0 ];

if ( type === 'track' ) {
entry = findCompatibleTrack( data.tracks );
d = entry[ 1 ];
}

if ( d ) {
message.match_data[ 2 ] = d.href.split(':')[ 2 ];
// Fake a fourth match parameter if we're dealing with tracks,
// this will be the # of skipped tracks before finding this one
message.match_data[ 3 ] = entry && entry[ 0 ];
wrap( message );
}
else {
Expand Down

0 comments on commit 8eb9881

Please sign in to comment.