Ruby: Search Spotify API


/ Published in: Ruby
Save to your folder(s)

Simple test showing how to make a request to Spotify's API.


Copy this code and paste it in your HTML
  1. =begin
  2. http://developer.spotify.com/en/metadata-api/overview/
  3.  
  4. Requests: http://ws.spotify.com/service/version/method[.format]?parameters
  5.  
  6. http://ws.spotify.com/search/1/track.json?q=kaizers+orchestra
  7.  
  8. Track Lookup
  9. http://ws.spotify.com/lookup/1/.json?uri=spotify:track:6NmXV4o6bmp704aPGyTVVG
  10.  
  11. Artist lookup
  12. http://ws.spotify.com/lookup/1/.json?uri=spotify:artist:4YrKBkKSVeqDamzBPWVnSJ
  13. http://ws.spotify.com/lookup/1/.json?uri=spotify:artist:4YrKBkKSVeqDamzBPWVnSJ&extras=album
  14. http://ws.spotify.com/lookup/1/.json?uri=spotify:artist:4YrKBkKSVeqDamzBPWVnSJ&extras=albumdetail
  15.  
  16. Album lookup
  17. http://ws.spotify.com/lookup/1/.json?uri=spotify:album:6G9fHYDCoyEErUkHrFYfs4
  18. http://ws.spotify.com/lookup/1/.json?uri=spotify:album:6G9fHYDCoyEErUkHrFYfs4&extras=track
  19. http://ws.spotify.com/lookup/1/.json?uri=spotify:album:6G9fHYDCoyEErUkHrFYfs4&extras=trackdetail
  20.  
  21. Search Example
  22. http://ws.spotify.com/search/1/artist?q=Bj%C3%B6rk.
  23.  
  24. =end
  25.  
  26. class SpotifyAPIRequest
  27. require 'open-uri'
  28.  
  29. def initialize
  30.  
  31. end
  32.  
  33. def lookup( keyword )
  34. open("http://ws.spotify.com/search/1/track.json?q=" + keyword){
  35. |f|
  36. #Get the Full response which should be the full HTML
  37. @req = f.read
  38. #Find the first place where "No match is found", if nothing is found, it will return 'nil'
  39. puts @req
  40.  
  41. =begin
  42.   @txt = @req.index("No match")
  43.   puts @txt
  44.  
  45.   if @txt.nil?
  46.   puts "Domain " + word + ".com is available"
  47.   else
  48.   puts "Domain " + word + ".com is taken"
  49.   end
  50. =end
  51. }
  52. end
  53. end
  54.  
  55. spotify = SpotifyAPIRequest.new
  56. spotify.lookup("beethoven")

URL: http://developer.spotify.com/en/metadata-api/overview/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.