Created
April 26, 2012 13:09
-
-
Save mortenjust/2499441 to your computer and use it in GitHub Desktop.
spotify api metaapi tracks artists coffeescript
This file contains hidden or 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
window.spotifyLink = (tracks) -> | |
extractID = (track) -> | |
track.replace(/spotify.track./,"") | |
"spotify:trackset:Title:#{extractID(track) for track in tracks}" | |
window.playlistLink = (tracks) -> | |
"<b><a href='#{spotifyLink(tracks)}'>Spotify playlist</a></b> | |
<small>Found #{tracks.length}/#{HUNTED.chart.entities.length}</small> | |
" | |
getSpotifyTrack = (track, callback) -> | |
$.ajax | |
url: "http://ws.spotify.com/search/1/track.json?q=" + encodeURIComponent(track) | |
dataType: 'json' | |
success: callback | |
checkOnSpotify = (tracks, callback) -> | |
spotifyTracks = [] | |
for track, i in tracks | |
searchstr = "#{track.str} #{track.artist.str}" | |
do (spotifyTracks, searchstr, track, i) -> | |
window.setTimeout -> | |
getSpotifyTrack searchstr, (result) -> | |
track.spotifyTrack = result.tracks?[0]?.href | |
if track.spotifyTrack | |
spotifyTracks.push track.spotifyTrack | |
showLink spotifyTracks | |
callback(spotifyTracks) if i is tracks.length-1 | |
, i*1000 | |
el = $("<div />").appendTo("#user-container .user-profile") | |
showLink = (tracks) -> | |
el.html(window.playlistLink(tracks)) | |
checkOnSpotify HUNTED.chart.entities, (tracks) -> | |
window.open(spotifyLink(tracks)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment