-
-
Save afonsoduarte/50505 to your computer and use it in GitHub Desktop.
Ubiquity command to lookup artists on Last.fm
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
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 ); | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment