Last active
August 29, 2015 17:35
-
-
Save abhin4v/24250 to your computer and use it in GitHub Desktop.
Provides an ubiquity command: "lyrics", to find the lyrics for a song. Type the song in the form "Song <by> Artist". Provides suggestions as you type. Uses lyricwiki.org API for fetching lyrics and last.fm webservices API for providing suggestions.
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
/* | |
* Author: Abhinav Sarkar <[email protected]> | |
* License: MPL 1.1 | |
* | |
* Provides an ubiquity command: "lyrics", to find the lyrics for a song. | |
* Usage: Type the song in the form "Song <by> Artist". Provides suggestions as you type. | |
* Uses lyricwiki.org API for fetching lyrics and last.fm webservices API for | |
* providing suggestions. | |
*/ | |
var noun_type_song = { | |
_name: "song", | |
_suggestions: [], | |
suggest: function(text, html, callback) { | |
track = parseTrack(text); | |
var baseUrl = "http://ws.audioscrobbler.com/2.0/"; | |
var params = { | |
'method':'track.search', | |
'track':track['song'], | |
'api_key':'b25b959554ed76058ac220b7b2e0a026', | |
'limit':'3', | |
'raw':'true' | |
}; | |
if (track['artist'] != null && track['artist'] != "") | |
params['artist'] = track['artist']; | |
jQuery.get(baseUrl, params, function(xml) { | |
suggestions = []; | |
jQuery(xml).find("trackmatches>track").each(function() { | |
that = this; | |
[song, artist] = jQuery.map(['name', 'artist'], function(item) { | |
return jQuery.trim(jQuery(that).find(item)[0].textContent); | |
}); | |
//CmdUtils.log(artist + " - " + song); | |
suggestion = {'song': song, 'artist': artist}; | |
suggestions.push(suggestion); | |
}); | |
noun_type_song._suggestions = suggestions; | |
}); | |
goodSuggestions = jQuery.map(noun_type_song._suggestions, function(suggestion) { | |
track = suggestion['song'] + " by " + suggestion['artist']; | |
return (track.match(text, "i")) ? CmdUtils.makeSugg(track) : null; | |
}).slice(0, 3); | |
return (goodSuggestions.length > 0) ? goodSuggestions : [CmdUtils.makeSugg(text)]; | |
} | |
} | |
function processLyrics(track, callback) { | |
var baseUrl = "http://lyricwiki.org/api.php"; | |
var params = {artist: track['artist'], song: track['song'], fmt: "json"}; | |
jQuery.getJSON(baseUrl, params, callback); | |
} | |
function parseTrack(track) { | |
track = jQuery.trim(track); | |
if (track.lastIndexOf("by") != -1) { | |
var song = jQuery.trim(track.substr(0, track.lastIndexOf("by"))); | |
var artist = jQuery.trim(track.substr(track.lastIndexOf("by")+2)); | |
return {'song': song, 'artist': artist}; | |
} else | |
return {'song': track, 'artist': ""}; | |
} | |
CmdUtils.CreateCommand({ | |
name: "lyrics", | |
author: {name: "Abhinav Sarkar", email: "[email protected]"}, | |
license: "MPL", | |
description: "Shows the lyrics for a song.", | |
help: "Type the song in the form \"Song <by> Artist\". Provides suggestions as you type.", | |
takes: {track: noun_type_song}, | |
preview: function(pblock, track) { | |
track = parseTrack(track.text) | |
song = track['song']; | |
artist = track['artist']; | |
if (song.length < 1 || artist.length < 1) { | |
pblock.innerHTML = "Searches for lyrics of the song"; | |
return; | |
} | |
processLyrics(track, function(json) { | |
lyricsTemplate = "<b>${artist}</b> - <b>${song}</b><br/><pre style='overflow-y:auto;max-height:500px'>${lyrics}</pre>"; | |
pblock.innerHTML = CmdUtils.renderTemplate(lyricsTemplate, json); | |
}); | |
}, | |
execute: function(track) { | |
track = parseTrack(track.text) | |
processLyrics(track, function(json) { | |
Utils.openUrlInBrowser(json['url']); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment