Skip to content

Instantly share code, notes, and snippets.

@fhats
Created February 7, 2011 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhats/814587 to your computer and use it in GitHub Desktop.
Save fhats/814587 to your computer and use it in GitHub Desktop.
Last.FM Tag Cloud Retriever
lastfm_api_key = "295e485894bdf30203b53607f1d94e34"
lastfm_api_root = "http://ws.audioscrobbler.com/2.0/"
result_limit = 15
import urllib
import urllib2
import json
import string
def getLastFmJson(args):
arguments = {}
for key in args:
arguments[key] = args[key]
cache_key = "autocomplete_%s" % json.dumps(arguments)
arguments["format"] = "json"
arguments["api_key"] = lastfm_api_key
arguments["limit"] = result_limit
#Check the cache for stored results
api_result = ""
values = urllib.urlencode(arguments)
api_string = lastfm_api_root + "?" + values
api_request = urllib2.Request(api_string)
response = urllib2.urlopen(api_request)
api_result = response.read()
result = json.loads(api_result)
return result
def main():
file_location = "data.txt"
f = open(file_location)
raw_artists = f.readlines()
artists = []
for artist in raw_artists:
artist = artist.rstrip()
if string.find(artist, ',') > -1:
splitted = string.split(artist, ',', 1)
artist = splitted[1] + " " + splitted[0]
artist = artist.strip()
artists.append(artist)
for artist in artists:
args = {
"method": "artist.getTopTags",
"artist": artist
}
response = getLastFmJson(args)
print artist + ": ",
if not "error" in response:
if 'tag' in response['toptags']:
for tag in response['toptags']['tag']:
try:
print tag['name'] + " ",
except:
continue
print ""
args = {
"method": "artist.getInfo",
"artist": artist
}
response = getLastFmJson(args)
print "Listeners: ",
if not "error" in response:
if "stats" in response["artist"]:
if "listeners" in response["artist"]["stats"]:
print response["artist"]["stats"]["listeners"]
else:
print "???"
else:
print "????"
else:
print "?"
print ""
return
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment