public
Last active

Ubiquity command to lookup artists on Last.fm

  • Download Gist
x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
CmdUtils.CreateCommand({
name: "Last.fm",
synonyms: ["music"],
description: "Opens the artist page",
help: "Looks for an Artist page on Last.fm",
icon: "http://cdn.last.fm/flatness/favicon.2.ico",
takes: {search: noun_arb_text},
execute: function( directObj ) {
var artist = directObj.text;
Utils.openUrlInBrowser( "http://www.last.fm/music/"+artist );
},
preview: function( pblock, directObj ) {
var artist = directObj.text;
if (artist.length < 2) pblock.innerHTML = "Looks for an artist page";
else {
var api_key = "3a6bad64c6351a7ab602a643d97bf01a";
var artistName = artist;
function requestArtistStats( artistName )
{
var getArtistInfoUrl = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo" +
"&api_key=" + api_key +
"&artist=" + encodeURIComponent(artistName);
CmdUtils.previewAjax(pblock, {
type: "GET",
url: getArtistInfoUrl,
datatype: "xml",
error: function() {
pblock.innerHTML = "<i>Error retreiving summary.</i>";
},
success: function( responseDetails, textStatus ) {
if( textStatus == "success" )
{
parseArtistInfoXML( responseDetails , artistName );
}
}
});
}
function parseArtistInfoXML( text, artist )
{
var artistImageSrc = text.getElementsByTagName( "image" )[1].textContent;
var articleSummary = text.getElementsByTagName( "summary" )[0].textContent;
var previewTemplate = "<img src=\"${img}\" style=\"float: left; max-width: 80px; max-height: 80px; background-color: white; margin:0 15px 15px 0;\" />" +
"<span class=\"wikisummary\">${summary}</span>";
var previewData = {
img: artistImageSrc,
summary: articleSummary
};
pblock.innerHTML = CmdUtils.renderTemplate( previewTemplate, previewData );
}
requestArtistStats( artistName );
 
}
}
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.