Posted By

chrisaiv on 02/08/12


Tagged

python lastfm


Versions (?)

Python: Last.Fm Advanced API Example


 / Published in: Python
 

URL: http://www.last.fm/api/show/geo.getTopTracks

I started messing around with the Last.fm API and somehow just started building more and more service requests. This should be enough to get anyone started on using Last.FM's API

  1. #
  2. # http://www.last.fm/api/show/geo.getTopTracks
  3. #
  4. from pprint import pprint
  5. import urllib, urllib2
  6. import inspect
  7. try:
  8. import json
  9. except ImportError:
  10. import simplejson as json
  11.  
  12. class LastFM:
  13. def __init__(self ):
  14. self.API_URL = "http://ws.audioscrobbler.com/2.0/"
  15. self.API_KEY = "LAST_FM_API_KEY"
  16.  
  17. def send_request(self, args, **kwargs):
  18. #Request specific args
  19. kwargs.update( args )
  20. #Global args
  21. kwargs.update({
  22. "api_key": self.API_KEY,
  23. "format": "json"
  24. })
  25. try:
  26. #Create an API Request
  27. url = self.API_URL + "?" + urllib.urlencode(kwargs)
  28. #Send Request and Collect it
  29. data = urllib2.urlopen( url )
  30. #Print it
  31. response_data = json.load( data )
  32. #Close connection
  33. data.close()
  34. return response_data
  35. except urllib2.HTTPError, e:
  36. print "HTTP error: %d" % e.code
  37. except urllib2.URLError, e:
  38. print "Network error: %s" % e.reason.args[1]
  39.  
  40. def get_top_artists(self, method, dict ):
  41. #find the key
  42. args = {
  43. "method": method,
  44. "limit": 3
  45. }
  46. for key in dict.keys():
  47. args[key] = dict[key]
  48.  
  49. response_data = self.send_request( args )
  50.  
  51. print "~~~~~~~~~~~~~~" + str( args["method"] ) + "~~~~~~~~~~~~~~"
  52.  
  53. #Get the first artist from the JSON response and print their name
  54. for artist in response_data["topartists"]["artist"]:
  55. print artist["name"]
  56.  
  57. def get_hyped_artists(self, method ):
  58. args = {
  59. "method": method,
  60. "limit": 3
  61. }
  62. response_data = self.send_request( args )
  63. print "~~~~~~~~~~~~~~" + str( args["method"] ) +"~~~~~~~~~~~~~~"
  64. #Get the first artist from the JSON response and print their name
  65. for artist in response_data["artists"]["artist"]:
  66. print artist["name"]
  67.  
  68. def get_similar_tracks(self, method, dict ):
  69. args = {
  70. "method": method,
  71. "limit": 3
  72. }
  73. for key in dict.keys():
  74. args[key] = dict[key]
  75. print key, dict[key]
  76.  
  77. response_data = self.send_request( args )
  78. print "~~~~~~~~~~~~~~" + str( args["method"] ) +"~~~~~~~~~~~~~~"
  79. #Get the first artist from the JSON response and print their name
  80.  
  81. for artist in response_data["similartracks"]["track"]:
  82. print artist["name"], artist["artist"]["name"]
  83.  
  84. def main():
  85. last_request = LastFM()
  86. last_request.get_top_artists( "tag.gettopartists", { "tag": "rock" } )
  87. last_request.get_top_artists( "geo.gettopartists", { "country": "spain" } )
  88. last_request.get_hyped_artists( "chart.getHypedArtists" )
  89. last_request.get_similar_tracks( "track.getsimilar", {
  90. "track": "Ray of Light",
  91. "artist": "Madonna"})
  92.  
  93. if __name__ == "__main__": main()

Report this snippet  

You need to login to post a comment.