Last active
September 30, 2016 16:46
-
-
Save bytezen/4daa20347c38440c6c9edd65b4886313 to your computer and use it in GitHub Desktop.
This is the spotify example
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
| // This is the last example that we did in class where we get the artist ID for | |
| // Billy Joel. | |
| // In order for this to work you need to have the code from the spotify-sandbox.js gist below | |
| // loaded into the console. | |
| // The documentation for the search endpoint is here: https://developer.spotify.com/web-api/search-item/ | |
| var searchURL = "https://api.spotify.com/v1/search"; | |
| //Note we could have intialized this params object all at once like so: | |
| // params = {q: "'Billy Joel'", type: "artist" } | |
| // | |
| // | |
| var params = {q: "", type:""} | |
| params.q = "'billy joel'" | |
| params.type = 'artist' | |
| $.get(searchURL,params).done(allGood).fail(allBad) | |
| var artistId = result.artists.items[0].id // shoudl be equal to: 6zFYqv1mOsgBRQbae3JJ9e | |
| $.get("https://api.spotify.com/v1/artists/" + artistId).done(allGood).fail(allBad) |
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
| // This is our good result handler | |
| function allGood( data ) { | |
| console.log(" got some good 'ol data..mmmm , mmm, good!!"); | |
| result = data; | |
| } | |
| // This is our error handler | |
| function allBad( err ) { | |
| console.log("ooooooops!!! Error = " ); | |
| console.log( err ); | |
| result = undefined; | |
| } | |
| //our URL | |
| var url = "https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj"; | |
| //A variable to allow us to see the result in the console | |
| var result = undefined; | |
| //make the connection to Spotify URL and pass the results to the | |
| // handler automagically | |
| $.get(url).done(allGood).fail(allBad) | |
| // Now lets parse the results... | |
| if( result != undefined) { | |
| // get the artist name | |
| var artistName = result.artists[0].name | |
| //get the second album image | |
| var albumimg = result.images[1] | |
| //create an image tag | |
| var $img = $('<img></img>') | |
| //add the image source url | |
| $img.attr("src",albumimg.url) | |
| // get the body element and add all of our goodies | |
| var $body = $("body") | |
| $body.append("<h1>"+artistName+"</h1>") | |
| $body.append($img) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment