var info_spotify = function() { var me = "spotify"; var medebug = "SPOTIFY PLUGIN"; var maxwidth = 300; function getTrackHTML(data) { debug.trace(medebug,"Making Track Info From",data); if (data.error) { return '

'+data.error+'

'; } var h = '
'; h += '
'; h += ''; h += '
'; if (data.explicit) { h += ''; } h += '
'; h += '
'; h += '
'; return h; } function getAlbumHTML(data) { debug.trace(medebug,"Making Album Info From",data); if (data.error) { return '

'+data.error+'

'; } var html = '
'; html += '
'; html += ''; html += '
'; html += '
'; html += spotifyTrackListing(data)+'
'; html += '
'; if (data.images && data.images[0]) { html += ''; } html += '
'; html += '
'; return html; } function getArtistHTML(data, parent, artistmeta) { debug.trace(medebug,"Making Artist Info From",data); if (data.error) { return '

'+data.error+'

'; } var h = ""; if (artistmeta.spotify.possibilities && artistmeta.spotify.possibilities.length > 1) { h += '
'+ '
'+ '
All possibilities for "'+ artistmeta.spotify.artist.name+'"
'+ '
'; for (var i in artistmeta.spotify.possibilities) { h += '
'; if (artistmeta.spotify.possibilities[i].image) { h += ''; } else { h += ''; } h += ''+artistmeta.spotify.possibilities[i].name+''; h += '
'; } h += '
'; h += '
'; } h += '
'; h += '
'; if (data.images && data.images[0]) { h += ''; } h += '
'; h += '
'; h += '
' + '
' + '
' + '
'; return h; } function findDisplayPanel(element) { var c = element; while (!c.hasClass('nobwobbler')) { c = c.parent(); } if (c.hasClass('nobalbum')) { debug.log(medebug,"Opening Album Panel Via Widget"); $('#artistalbums').spotifyAlbumThing('handleClick', element); return true; } else if (c.hasClass('nobartist')) { debug.log(medebug,"Opening Artist Panel Via Widget"); $('#artistalbums').spotifyArtistThing('handleClick', element); return true; } else { debug.error(medebug,"Click On Unknown Element!",element); return false; } } return { getRequirements: function(parent) { return ["musicbrainz"]; }, collection: function(parent, artistmeta, albummeta, trackmeta) { debug.trace(medebug, "Creating data collection"); var self = this; var displaying = false; if (artistmeta.spotify === undefined) { artistmeta.spotify = {}; } if (artistmeta.spotify.showing === undefined) { artistmeta.spotify.showing = "albums"; } this.populate = function() { self.track.populate(); } this.displayData = function() { displaying = true; self.artist.doBrowserUpdate(); self.album.doBrowserUpdate(); self.track.doBrowserUpdate(); } this.stopDisplaying = function() { displaying = false; } this.handleClick = function(source, element, event) { debug.trace(medebug,parent.nowplayingindex,source,"is handling a click event"); if (element.hasClass('clickzoomimage')) { imagePopup.create(element, event, element.attr("src")); } else if (element.hasClass('clickspotifywidget')) { findDisplayPanel(element); } else if (element.hasClass('clickchooseposs')) { var poss = element.attr("name"); if (poss != artistmeta.spotify.currentposs) { artistmeta.spotify = { currentposs: poss, possibilities: artistmeta.spotify.possibilities, id: artistmeta.spotify.possibilities[poss].id, showing: "albums" } self.artist.force = true; self.artist.populate(); } } else if (element.hasClass('clickshowalbums') && artistmeta.spotify.showing != "albums") { $('#artistalbums').spotifyArtistThing('destroy'); artistmeta.spotify.showing = "albums"; $("#bumhole .bsel").removeClass("bsel"); element.addClass("bsel"); getAlbums(); } else if (element.hasClass('clickshowartists') && artistmeta.spotify.showing != "artists") { $('#artistalbums').spotifyAlbumThing('destroy'); artistmeta.spotify.showing = "artists"; $("#bumhole .bsel").removeClass("bsel"); element.addClass("bsel"); getArtists(); } else if (element.hasClass('clickaddtolistenlater')) { metaHandlers.addToListenLater(albummeta.spotify.album); } else if (element.hasClass('clickaddtocollection')) { metaHandlers.fromSpotifyData.addAlbumTracksToCollection(albummeta.spotify.album, artistmeta.spotify.artist.name); } else if (element.hasClass('clickstartradio')) { playlist.radioManager.load("artistRadio", 'spotify:artist:'+artistmeta.spotify.id); } else if (element.hasClass('clickstartsingleradio')) { playlist.radioManager.load("singleArtistRadio", artistmeta.name); } else if (element.hasClass('clickstarttrackradio')) { debug.log("SPOTIFY","Starting Track Recommendations With",trackmeta.spotify.id); playlist.radioManager.load("spotiTrackRadio", {seed_tracks: trackmeta.spotify.id, name: trackmeta.spotify.track.name}); } else if (element.hasClass('clickstartartistradio')) { debug.log("SPOTIFY","Starting Artist Recommendations With",artistmeta.spotify.id); playlist.radioManager.load("spotiTrackRadio", {seed_artists: artistmeta.spotify.id, name: artistmeta.spotify.artist.name}); } } function getAlbums() { $("#hibbert").makeSpinner(); if (artistmeta.spotify.albums === undefined) { spotify.artist.getAlbums(artistmeta.spotify.id, 'album,single', storeAlbums, self.artist.spotifyError, true) } else { doAlbums(artistmeta.spotify.albums); } } function getArtists() { $("#hibbert").makeSpinner() if (artistmeta.spotify.related === undefined) { spotify.artist.getRelatedArtists(artistmeta.spotify.id, storeArtists, self.artist.spotifyError, true) } else { doArtists(artistmeta.spotify.related); } } function storeAlbums(data) { artistmeta.spotify.albums = data; doAlbums(data); } function storeArtists(data) { artistmeta.spotify.related = data; doArtists(data); } function doAlbums(data) { debug.trace(medebug,"DoAlbums",artistmeta.spotify.showing, displaying); if (artistmeta.spotify.showing == "albums" && displaying && data) { debug.trace(medebug,"Doing Albums For Artist",data); $('#artistalbums').spotifyAlbumThing({ classes: 'nobwobbler nobalbum tagholder2 selecotron', itemselector: 'nobwobbler', sub: null, showbiogs: false, layoutcallback: function() { $("#hibbert").stopSpinner(); browser.rePoint() }, maxwidth: maxwidth, is_plugin: false, imageclass: 'masochist', data: data.items }); } } function doArtists(data) { if (artistmeta.spotify.showing == "artists" && displaying && data) { debug.trace(medebug,"Doing Related Artists",data); $('#artistalbums').spotifyArtistThing({ classes: 'nobwobbler nobartist tagholder2', itemselector: 'nobwobbler', sub: null, layoutcallback: function() { $("#hibbert").stopSpinner(); browser.rePoint() }, is_plugin: false, imageclass: 'jalopy', maxalbumwidth: maxwidth, data: data.artists }); } } this.track = function() { function spotifyResponse(data) { debug.trace(medebug, "Got Spotify Track Data"); debug.trace(medebug, data); if (trackmeta.spotify.track === undefined) { trackmeta.spotify.track = data; } if (albummeta.spotify === undefined) { albummeta.spotify = {id: data.album.id}; } for(var i in data.artists) { if (data.artists[i].name == artistmeta.name) { debug.trace(medebug,parent.nowplayingindex,"Found Spotify ID for", artistmeta.name); artistmeta.spotify.id = data.artists[i].id; } } debug.trace(medebug,"Spotify Data now looks like",artistmeta, albummeta, trackmeta); self.track.doBrowserUpdate(); self.artist.populate(); } function gotTrackRecommendations(data) { trackmeta.spotify.recommendations = data; doRecommendations(data); } function doRecommendations(data) { $('#helpful_title').html('

'+language.gettext('discover_now', [trackmeta.spotify.track.name])+'

'); for (var i in data.tracks) { var x = $('
', {class: 'arsecandle tagholder4 clickable draggable clicktrack playable notthere', name: data.tracks[i].uri}).appendTo($('#helpful_tracks')); var a = data.tracks[i].album; var img = ''; if (a.images && a.images[0]) { img = 'getRemoteImage.php?url='+a.images[0].url for (var j in a.images) { if (a.images[j].width <= maxwidth) { img = 'getRemoteImage.php?url='+a.images[j].url; break; } } img += '&rompr_resize_size=smallish'; } else { img = 'newimages/spotify-icon.png'; } x.append('
'); var an = new Array(); for (var j in data.tracks[i].artists) { an.push(data.tracks[i].artists[j].name); } x.append('
'+concatenate_artist_names(an)+'
'+data.tracks[i].name+'
'); } $('#helpful_tracks').imagesLoaded(doBlockLayout); } function doBlockLayout() { debug.shout(medebug,"Track Images Have Loaded"); browser.rePoint($('#helpful_tracks'),{ itemSelector: '.arsecandle', columnWidth: '.arsecandle', percentPosition: true}); donetheother = true; setDraggable('#helpful_tracks'); $('#helpful_title').find('.notthere').removeClass('notthere'); $('#helpful_tracks').find('.notthere').removeClass('notthere'); } return { populate: function() { if (trackmeta.spotify === undefined || artistmeta.spotify.id === undefined) { if (parent.playlistinfo.file.substring(0,8) !== 'spotify:') { self.track.doBrowserUpdate() self.artist.populate(); self.album.populate(); } else { if (trackmeta.spotify === undefined) { trackmeta.spotify = {id: parent.playlistinfo.file.substr(14, parent.playlistinfo.file.length) }; } spotify.track.getInfo(trackmeta.spotify.id, spotifyResponse, self.track.spotifyError, true); } } else { self.artist.populate(); } }, spotifyError: function(data) { debug.warn(medebug, "Spotify Error!", data); data.name = parent.playlistinfo.Title; data.external_urls = {spotify: ''}; trackmeta.spotify.track = data; if (albummeta.spotify === undefined) { albummeta.spotify = {}; } self.track.doBrowserUpdate() self.artist.populate(); }, spotifyRecError: function(data) { debug.warn(medebug,"Error getting track reccomendations",data); }, doBrowserUpdate: function() { var accepted = false; if (displaying && trackmeta.spotify !== undefined && trackmeta.spotify.track !== undefined) { debug.trace(medebug,parent.nowplayingindex,"track was asked to display"); accepted = browser.Update( null, 'track', me, parent.nowplayingindex, { name: trackmeta.spotify.track.name, link: trackmeta.spotify.track.external_urls.spotify, data: getTrackHTML(trackmeta.spotify.track) } ); } else if (parent.playlistinfo.file.substring(0,8) !== 'spotify:') { browser.Update(null, 'track', me, parent.nowplayingindex, { name: "", link: "", data: null } ); } if (accepted && !trackmeta.spotify.track.error) { if (trackmeta.spotify.recommendations) { doRecommendations(trackmeta.spotify.recommendations); } else { var params = { limit: 8 } if (prefs.lastfm_country_code) { params.market = prefs.lastfm_country_code; } params.seed_tracks = trackmeta.spotify.id; spotify.recommendations.getRecommendations(params, gotTrackRecommendations, self.track.spotifyRecError); } } } } }(); this.album = function() { function spotifyResponse(data) { debug.trace(medebug, "Got Spotify Album Data"); debug.trace(medebug, data); albummeta.spotify.album = data; self.album.doBrowserUpdate(); } return { populate: function() { if (albummeta.spotify === undefined || albummeta.spotify.album === undefined ) { if (parent.playlistinfo.file.substring(0,8) !== 'spotify:') { self.album.doBrowserUpdate(); } else { spotify.album.getInfo(albummeta.spotify.id, spotifyResponse, self.album.spotifyError, true); } } }, spotifyError: function(data) { debug.error(medebug, "Spotify Error!"); data.name = parent.playlistinfo.Album; data.external_urls = {spotify: ''}; albummeta.spotify.album = data; self.album.doBrowserUpdate(); }, doBrowserUpdate: function() { if (displaying && albummeta.spotify !== undefined && albummeta.spotify.album !== undefined) { debug.trace(medebug,parent.nowplayingindex,"album was asked to display"); var accepted = browser.Update( null, 'album', me, parent.nowplayingindex, { name: albummeta.spotify.album.name, link: albummeta.spotify.album.external_urls.spotify, data: getAlbumHTML(albummeta.spotify.album) } ); } else if (parent.playlistinfo.file.substring(0,8) !== 'spotify:') { browser.Update(null, 'album', me, parent.nowplayingindex, { name: "", link: "", data: null } ); } infobar.markCurrentTrack(); } } }(); this.artist = function() { var triedWithoutBrackets = 0; var retries = 10; var searchingfor = artistmeta.name; function spotifyResponse(data) { debug.trace(medebug, "Got Spotify Artist Data"); debug.trace(medebug, data); artistmeta.spotify.artist = data; self.artist.doBrowserUpdate(); self.album.populate(); } function search(aname) { if (parent.playlistinfo.type == "stream" && artistmeta.name == "" && trackmeta.name == "") { debug.shout(medebug, "Searching Spotify for artist",albummeta.name) spotify.artist.search(albummeta.name, searchResponse, searchFail, true); } else { debug.shout(medebug, "Searching Spotify for artist",aname) spotify.artist.search(aname, searchResponse, searchFail, true); } } function searchResponse(data) { debug.trace(medebug,"Got Spotify Search Data",data); var match = searchingfor.toLowerCase(); artistmeta.spotify.possibilities = new Array(); for (var i in data.artists.items) { if (data.artists.items[i].name.toLowerCase() == match) { artistmeta.spotify.possibilities.push({ name: data.artists.items[i].name, id: data.artists.items[i].id, image: (data.artists.items[i].images && data.artists.items[i].images.length > 0) ? data.artists.items[i].images[data.artists.items[i].images.length-1].url : null }); } } if (artistmeta.spotify.possibilities.length == 0 && data.artists.items.length == 1) { // only one match returned, it wasn't an exact match, but use it anyway artistmeta.spotify.possibilities.push({ name: data.artists.items[0].name, id: data.artists.items[0].id, image: (data.artists.items[0].images && data.artists.items[0].images.length > 0) ? data.artists.items[0].images[data.artists.items[0].images.length-1].url : null }); } if (artistmeta.spotify.possibilities.length > 0) { artistmeta.spotify.currentposs = 0; artistmeta.spotify.id = artistmeta.spotify.possibilities[0].id; artistmeta.spotify.showing = "albums"; } if (artistmeta.spotify.id === undefined) { searchFail(); } else { self.artist.populate(); } } function searchFail() { debug.trace("SPOTIFY PLUGIN","Couldn't find anything for",artistmeta.name); var test; switch (triedWithoutBrackets) { case 0: triedWithoutBrackets = 1; test = artistmeta.name.replace(/ \(+.+?\)+$/, ''); if (test != artistmeta.name) { searchingfor = test; debug.trace("SPOTIFY PLUGIN","Searching instead for",test); search(test); return; } // Fall Through case 1: triedWithoutBrackets = 2; test = artistmeta.name.split(/ & | and /)[0]; if (test != artistmeta.name) { searchingfor = test; debug.trace("SPOTIFY PLUGIN","Searching instead for",test); search(test); return; } // Fall Through default: artistmeta.spotify = { artist: { error: '

'+ language.gettext("label_noartistinfo")+ '

', name: artistmeta.name, external_urls: { spotify: '' } } }; self.artist.doBrowserUpdate(); break; } } return { force: false, populate: function() { if (artistmeta.spotify.id === undefined) { search(artistmeta.name); } else { if (artistmeta.spotify.artist === undefined) { spotify.artist.getInfo(artistmeta.spotify.id, spotifyResponse, self.artist.spotifyError, true); } else { self.album.populate(); } } }, spotifyError: function(data) { debug.error(medebug, "Spotify Error!"); data.external_urls = {spotify: ''}; data.name = artistmeta.name; artistmeta.spotify.artist = data; self.artist.doBrowserUpdate(); self.album.populate(); }, tryForAllmusicBio: function() { if (typeof artistmeta.allmusic == 'undefined' || typeof artistmeta.allmusic.artistlink === 'undefined') { debug.shout(medebug,"Allmusic artist link not back yet"); retries--; if (retries > 0) { setTimeout(self.artist.tryForAllmusicBio, 2000); } else { debug.shout(medebug,"Artist giving up waiting for musicbrainz"); } } else if (artistmeta.allmusic.artistlink === null) { debug.shout(medebug,"No Allmusic artist bio link found"); } else { debug.shout(medebug,"Getting allmusic bio from",artistmeta.allmusic.artistlink); $.post('browser/backends/getambio.php', {url: artistmeta.allmusic.artistlink}) .done( function(data) { debug.log(medebug,"Got Allmusic Bio"); if (displaying) $("#artistbio").html(data); }) .fail( function() { debug.log(medebug,"Didn't Get Allmusic Bio"); if (displaying) $("#artistbio").html(""); }); } }, doBrowserUpdate: function() { if (displaying && artistmeta.spotify !== undefined && artistmeta.spotify.artist !== undefined) { debug.trace(medebug,parent.nowplayingindex,"artist was asked to display"); var accepted = browser.Update( null, 'artist', me, parent.nowplayingindex, { name: artistmeta.spotify.artist.name, link: artistmeta.spotify.artist.external_urls.spotify, data: getArtistHTML(artistmeta.spotify.artist, parent, artistmeta) }, false, self.artist.force ); if (accepted && artistmeta.spotify.artist.error == undefined) { debug.trace(medebug,"Update was accepted by browser"); self.artist.tryForAllmusicBio(); if (artistmeta.spotify.showing == "albums") { getAlbums(); } else { getArtists(); } } } } } }(); } } }(); nowplaying.registerPlugin("spotify", info_spotify, "icon-spotify-circled", "button_infospotify");