last.fm.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* https://github.com/alexcash/jQuery.last.fm */
  2. (function( $ ) {
  3. $.fn.lfm = function(options){
  4. var settings = $.extend({
  5. APIkey: null,
  6. User: null,
  7. Behavior: "hover",
  8. limit: 20,
  9. period: "3month" // overall | 7day | 1month | 3month | 6month | 12month
  10. }, options);
  11. var url = "http://ws.audioscrobbler.com/2.0/?method=user.gettopalbums&user=" + settings.User + "&period=" + settings.period + "&api_key=" + settings.APIkey + "&format=json&limit=" + settings.limit +"&callback=?";
  12. var albums = [];
  13. function isLoaded (albumElement) {
  14. for (var i = 0; i < albums.length; i++){
  15. var markup = $("<div class='album'><div class='front'><img src='" + albums[i].art + "'><div class='alpha'></div></div><div class='back'><h2>" + albums[i].artist + "</h2><h1>" + albums[i].name + "</h1><h3>" + albums[i].played + " tracks played</h3></div></div>");
  16. albumElement.append(markup);
  17. }
  18. if (settings.Behavior == "hover") {
  19. albumElement.find('.album').hover(function(){
  20. $(this).addClass('flip');
  21. },function(){
  22. $(this).removeClass('flip');
  23. });
  24. } else {
  25. $(document).bind('click', function (e) {
  26. $('.flip').removeClass('flip');
  27. });
  28. albumElement.find('.album').click(function(e){
  29. e.stopPropagation();
  30. if($('.flip')[0] === this){
  31. $(this).removeClass('flip');
  32. } else {
  33. $('.flip').removeClass('flip');
  34. $(this).addClass('flip');
  35. }
  36. });
  37. }
  38. }
  39. return this.each(function(){
  40. var $this = $(this);
  41. $.getJSON( url, function(data){
  42. $(data.topalbums.album).each(function(){
  43. albums.push ({
  44. name: this.name,
  45. artist: this.artist.name,
  46. played: this.playcount,
  47. art: this.image[this.image.length-1]["#text"]
  48. });
  49. });
  50. isLoaded($this);
  51. });
  52. });
  53. };
  54. })( jQuery );
  55. $('.albums').lfm({
  56. APIkey: "e12ea1d0253898ee9a93edfe42ffdeab",
  57. User: "windhamdavid",
  58. Behavior: "hover",
  59. limit: 100,
  60. period: "3month"
  61. });