Last active
January 22, 2022 11:12
-
-
Save 9re/5939575 to your computer and use it in GitHub Desktop.
Beatport widget live demo:
http://jsdo.it/9re/beatport_widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
function Beatport(artistId) { | |
this.artistId = artistId; | |
} | |
Beatport.prototype.load = function load(container) { | |
fetchRelease(this.artistId, function (tracks) { | |
tracks.forEach(function (track) { | |
$(container).append(getBeatportPlayerEmbedCode(track)); | |
}); | |
}); | |
}; | |
window.Beatport = Beatport; | |
function fetchRelease(artistId, callback) { | |
var url = 'http://api.beatport.com/catalog/3/tracks?' + | |
buildQuery({ | |
perPage : 5, | |
page : 1, | |
'facets[]' : 'artistId:' + artistId, | |
sortBy : 'publishDate+DESC,releaseId+DESC' | |
}); | |
$.ajax({ | |
method : "GET", | |
url : url | |
}).done(function(res){ | |
var tracks = []; | |
var lastReleaseId = null; | |
var releaseCount = 0; | |
for (var i = 0, len = res.results.length; | |
i < len; ++i) { | |
var track = res.results[i]; | |
if (tracks.length < 3 || | |
lastReleaseId == track.release.id) { | |
tracks.push(track.id); | |
} else { | |
break; | |
} | |
lastReleaseId = track.release.id; | |
} | |
callback(tracks); | |
}); | |
} | |
function getBeatportPlayerEmbedCode(trackId) { | |
return '<iframe src="http://embed.beatport.com/player/?id=__trackId__&type=track" width="100%" height="160" frameborder="0" scrolling="no" style="max-width:600px;"></iframe>'.replace('__trackId__', trackId); | |
} | |
function buildQuery(params) { | |
return Object.keys(params).map(function(key) { | |
return ([key, params[key]]).join('='); | |
}).join('&'); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works I can't see the live demo!