gtzan_root = '../GTZAN'
import glob
glob.glob(gtzan_root + '/genres/*')
['../GTZAN/genres/metal', '../GTZAN/genres/reggae', '../GTZAN/genres/rock', '../GTZAN/genres/disco', '../GTZAN/genres/country', '../GTZAN/genres/blues', '../GTZAN/genres/jazz', '../GTZAN/genres/pop', '../GTZAN/genres/classical', '../GTZAN/genres/hiphop']
genres = [genre[genre.rindex('/') + 1:] for genre in glob.glob(gtzan_root + '/genres/*')]
genres
['metal', 'reggae', 'rock', 'disco', 'country', 'blues', 'jazz', 'pop', 'classical', 'hiphop']
gtzan_files = dict()
for genre in genres:
gtzan_files[genre] = glob.glob(gtzan_root + '/genres/' + genre + '/*.au')
from essentia.streaming import *
sr = 22050
frameSize = 1024
hopSize = 512
loader = MonoLoader(sampleRate=sr)
frameCutter = FrameCutter(frameSize = frameSize, hopSize = hopSize)
w = Windowing(type = 'hann')
spec = Spectrum()
centroid = Centroid()
rolloff = RollOff()
flux = Flux()
zcr = ZeroCrossingRate()
rms = RMS()
# Texture windows
textureTime = 1.0 # seconds
textureSize = int(textureTime * sr/float(hopSize))
textureWindowCutters = []
textureWindowMeans = []
textureWindowVars = []
for i in range(5):
textureWindowCutters.append(FrameCutter(frameSize = textureSize, hopSize = textureSize))
textureWindowMeans.append(Mean())
textureWindowVars.append(Variance())
pool = essentia.Pool()
Now connect the graph:
loader.audio >> frameCutter.signal
frameCutter.frame >> w.frame >> spec.frame
spec.spectrum >> centroid.array
spec.spectrum >> rolloff.spectrum
spec.spectrum >> flux.spectrum
frameCutter.frame >> zcr.signal
frameCutter.frame >> rms.array
# We won't store the individual features in the pool
#centroid.centroid >> (pool, 'lowlevel.centroid')
#rolloff.rollOff >> (pool, 'lowlevel.rolloff')
#flux.flux >> (pool, 'lowlevel.flux')
#zcr.zeroCrossingRate >> (pool, 'lowlevel.zcr')
#rms.rms >> (pool, 'lowlevel.rms')
<essentia.streaming._StreamConnector instance at 0x8f0acf8>
And insert the texture windows:
# We will keep the testure windows
centroid.centroid >> textureWindowCutters[0].signal
rolloff.rollOff >> textureWindowCutters[1].signal
flux.flux >> textureWindowCutters[2].signal
zcr.zeroCrossingRate >> textureWindowCutters[3].signal
rms.rms >> textureWindowCutters[4].signal
features = ['lowlevel.centroid', 'lowlevel.rolloff', 'lowlevel.flux', 'lowlevel.zcr', 'lowlevel.rms']
for i in range(5):
textureWindowCutters[i].frame >> textureWindowMeans[i].array
textureWindowCutters[i].frame >> textureWindowVars[i].array
# textureWindowMeans[i].mean >> (pool, '%s_mean'%features[i])
# textureWindowVars[i].variance >> (pool, '%s_var'%features[i])
gtzan_files.keys()
['reggae', 'classical', 'country', 'jazz', 'metal', 'pop', 'disco', 'hiphop', 'rock', 'blues']
gtzan_features = dict()
feature_pools = []
pool = essentia.Pool()
#for genre in gtzan_files.keys():
genre = 'metal'
for audiofile in gtzan_files[genre][:10]:
loader.configure(filename = audiofile)
pool = essentia.Pool()
for i in range(5):
textureWindowMeans[i].mean >> (pool, '%s_mean'%features[i])
textureWindowVars[i].variance >> (pool, '%s_var'%features[i])
essentia.run(loader)
feature_pools.append(pool)
for i in range(5):
textureWindowMeans[i].mean.disconnect( (pool, '%s_mean'%features[i]) )
textureWindowVars[i].variance.disconnect( (pool, '%s_mean'%features[i]) )
len(feature_pools)
10
pool1 = feature_pools[1]
pool1.containsKey('lowlevel.centroid_mean')
True
pool1['lowlevel.centroid_mean']
array([ 0.12349304, 0.12603827, 0.12369274, 0.12796623, 0.12545626, 0.12588733, 0.12907386, 0.12259493, 0.12012523, 0.12449955, 0.12821124, 0.12822448, 0.12692975, 0.12749869, 0.12953703, 0.07763326, 0.12349304, 0.12603827, 0.12369274, 0.12796623, 0.12545626, 0.12588733, 0.12907386, 0.12259493, 0.12012523, 0.12449955, 0.12821124, 0.12822448, 0.12692975, 0.12749869, 0.12953703, 0.07763326, 0.12349304, 0.12603827, 0.12369274, 0.12796623, 0.12545626, 0.12588733, 0.12907386, 0.12259493, 0.12012523, 0.12449955, 0.12821124, 0.12822448, 0.12692975, 0.12749869, 0.12953703, 0.07763326, 0.08938374, 0.09283585, 0.0810305 , 0.07962823, 0.10627975, 0.08838628, 0.0812446 , 0.07591175, 0.09368064, 0.0791645 , 0.07838911, 0.07688246, 0.09608771, 0.08181662, 0.0803022 , 0.0789201 , 0.09823932, 0.09202421, 0.08374907, 0.07282116, 0.08969576, 0.08655645, 0.09912755, 0.08663792, 0.08775774, 0.07895733, 0.11164059, 0.08674042, 0.08723493, 0.08756376, 0.07958999, 0.08260456, 0.07709543, 0.08702885, 0.13156229, 0.11765064, 0.08720219, 0.0796233 , 0.07631919, 0.07384487, 0.08006869, 0.07127871, 0.06470564, 0.07387184, 0.08057489, 0.10077029, 0.08958122, 0.08490527, 0.07346863, 0.10786097, 0.09761067, 0.10034223, 0.07842575, 0.07795314, 0.09386371, 0.08901539, 0.12124291, 0.08587018, 0.0993743 , 0.06416983], dtype=float32)
len(pool1['lowlevel.centroid_mean'])
108
From: http://echonest.com/jobs/ :
"We’ve built the biggest database about music in the world -- 34 million songs, almost 2.4 million artists, with detailed information on each down to the pitch of every note in a guitar solo. And we’ve done it with off-the-shelf mostly open source technologies such as Solr, Tokyo Tyrant, RabbitMQ and Python combined with the brainpower of a few dissertations, algorithms and hard work over the past few years."
Amazon EC2 Tornado Python, Lua, svn
What they do:
http://the.echonest.com/solutions/
Their API:
Read their license carefully, in particular:
Don't: (ii) crawl, spider, index or in any way store information obtained from the APIs (except ephemeral copies in a cache pursuant to industry norms)
You can do all the web calls yourself, or you can use: https://github.com/echonest/pyechonest
from keys import mykey
from pyechonest import config
config.ECHO_NEST_API_KEY=mykey
import json
json.loads('{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artist": {"hotttnesss": 0.941778, "id": "ARX6TAQ11C8A415850", "name": "Lady Gaga"}}}')
{u'response': {u'artist': {u'hotttnesss': 0.941778, u'id': u'ARX6TAQ11C8A415850', u'name': u'Lady Gaga'}, u'status': {u'code': 0, u'message': u'Success', u'version': u'4.2'}}}
from pyechonest import track
ls /home/andres/Music/Messiaen/Turangalila\ Symphonie
06 AudioTrack 06.mp3 08 AudioTrack 08.mp3
track_info = track.track_from_filename('/home/andres/Music/Messiaen/Turangalila Symphonie/08 AudioTrack 08.mp3')
track_info
<track - Turangalila Symphony: Developpement de l'amour>
dir(track_info)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_object_type', u'acousticness', 'analysis_url', u'analyzer_version', u'artist', u'artist_id', u'audio_md5', u'bitrate', 'cache', u'danceability', u'duration', u'energy', 'get_analysis', 'get_attribute', 'id', u'key', u'liveness', u'loudness', 'md5', u'mode', 'post_attribute', u'release', u'samplerate', u'song_id', u'speechiness', u'status', u'tempo', u'time_signature', u'title', u'valence']
track_info.analysis_url
u'http://echonest-analysis.s3.amazonaws.com/TR/TRIQQNI14499DEC798/3/full.json?AWSAccessKeyId=AKIAJRDFEY23UEVW42BQ&Expires=1394225367&Signature=oyOku5O%2Bb5mWj5oyT1DPsv0gsAo%3D'
import urllib2
import json
response = urllib2.urlopen(track_info.analysis_url)
data = json.load(response)
data.keys()
[u'bars', u'track', u'segments', u'beats', u'meta', u'sections', u'tatums']
data['track'].keys()
[u'mode_confidence', u'end_of_fade_in', u'key_confidence', u'synchstring', u'synch_version', u'duration', u'rhythm_version', u'rhythmstring', u'echoprintstring', u'sample_md5', u'time_signature_confidence', u'echoprint_version', u'start_of_fade_out', u'analysis_sample_rate', u'analysis_channels', u'tempo', u'offset_seconds', u'tempo_confidence', u'key', u'decoder', u'code_version', u'window_seconds', u'codestring', u'mode', u'time_signature', u'num_samples', u'loudness', u'decoder_version']
analysis = track_info.get_analysis()
type(analysis) # Not working?
NoneType
from pyechonest import song
results = song.search(artist="Led Zeppelin", title='Rock and Roll', results=3, buckets=['audio_summary'])
results
[<song - Rock And Roll>, <song - Rock And Roll>, <song - ROCK AND ROLL (Album Version)>]
version1 = results[0]
version1
<song - Rock And Roll>
dir(version1)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_object_type', 'artist_familiarity', 'artist_hotttnesss', 'artist_id', 'artist_location', 'artist_name', 'audio_summary', 'cache', 'get_artist_familiarity', 'get_artist_hotttnesss', 'get_artist_location', 'get_attribute', 'get_audio_summary', 'get_foreign_id', 'get_song_currency', 'get_song_discovery', 'get_song_hotttnesss', 'get_song_type', 'get_tracks', 'id', 'post_attribute', 'song_currency', 'song_discovery', 'song_hotttnesss', 'song_type', 'title']
for v in results:
print v.song_hotttnesss,
0.681588 0.681588 0.681588
version1.audio_summary
{u'acousticness': 0.000527, u'analysis_url': u'http://echonest-analysis.s3.amazonaws.com/TR/QG3fkwxmTF3idAnKRE6Te14zFFVdDnRdKilzVQIrxJQ7wr1p8bjgTdPt5zqqRSbrSRgy3ZAmJTRLROGhg%3D/3/full.json?AWSAccessKeyId=AKIAJRDFEY23UEVW42BQ&Expires=1394225642&Signature=97nqfqSJMz1VIy1hKODajbbNIqE%3D', u'audio_md5': u'58882547724329387e8e72e025b3ed00', u'danceability': 0.32473, u'duration': 219.79955, u'energy': 0.892717, u'key': 9, u'liveness': 0.106599, u'loudness': -7.432, u'mode': 1, u'speechiness': 0.037498, u'tempo': 169.22, u'time_signature': 4, u'valence': 0.90403}
f = urllib2.urlopen("http://api.7digital.com/1.2/track/preview?trackid={0}&oauth_consumer_key={1}".format(7digital_id,key))
fh = open('./test.mp3', 'wb')
fh.write(f.read())
fh.close()
f.close()
Other client libraries:
http://labrosa.ee.columbia.edu/millionsong/
From echonest, but downloadable for offline usage.
http://www.infochimps.com/collections/million-songs
Data for songs:
http://labrosa.ee.columbia.edu/millionsong/pages/example-track-description
Some existing code to work with the MSDB:
Python API:
https://musicbrainz.org/doc/python-musicbrainz2
However: Status: Deprecated as of May 2011. This library is not compatible with the current MusicBrainz schema, but will still work with older versions and with the ws/1 legacy web service.
You can actually add to this database!
Includes cover art.
# from http://bugs.musicbrainz.org/browser/python-musicbrainz2/trunk/examples/getartist.py
import sys
import logging
import musicbrainz2.webservice as ws
import musicbrainz2.model as m
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
artist_id = ''
q = ws.Query()
try:
# The result should include all official albums.
#
inc = ws.ArtistIncludes(
releases=(m.Release.TYPE_OFFICIAL, m.Release.TYPE_ALBUM),
tags=True, releaseGroups=True)
artist = q.getArtistById(artist_id, inc)
except ws.WebServiceError, e:
print 'Error:', e
sys.exit(1)
print "Id :", artist.id
print "Name :", artist.name
print "SortName :", artist.sortName
print "UniqueName :", artist.getUniqueName()
print "Type :", artist.type
print "BeginDate :", artist.beginDate
print "EndDate :", artist.endDate
print "Tags :", ', '.join([t.value for t in artist.tags])
print
DEBUG:<class 'musicbrainz2.webservice.WebService'>:GET http://musicbrainz.org/ws/1/artist/?type=xml&inc=sa-Official+tags+sa-Album+release-groups DEBUG:<class 'musicbrainz2.webservice.WebService'>:GET failed: HTTP Error 400: Bad Request
An exception has occurred, use %tb to see the full traceback. SystemExit: 1
Error: HTTP Error 400: Bad Request
To exit: use 'exit', 'quit', or Ctrl-D.
if len(artist.getReleases()) == 0:
print "No releases found."
else:
print "Releases:"
for release in artist.getReleases():
print
print "Id :", release.id
print "Title :", release.title
print "ASIN :", release.asin
print "Text :", release.textLanguage, '/', release.textScript
print "Types :", release.types
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-64-6be91767da51> in <module>() ----> 1 if len(artist.getReleases()) == 0: 2 print "No releases found." 3 else: 4 print "Releases:" 5 NameError: name 'artist' is not defined
if len(artist.getReleaseGroups()) == 0:
print
print "No release groups found."
else:
print
print "Release groups:"
for rg in artist.getReleaseGroups():
print
print "Id :", rg.id
print "Title :", rg.title
print "Type :", rg.type
print
#
# Using the release IDs and Query.getReleaseById(), you could now request
# those releases, including the tracks, release events, the associated
# DiscIDs, and more. The 'getrelease.py' example shows how this works.
#
# EOF
Look at the services supported by echonest. Also:
import pylast
#At http://code.google.com/p/pylast/
from keys import lastfm_api_key, lastfm_api_secret, lastfm_username, lastfm_password
# You have to have your own unique two values for API_KEY and API_SECRET
# Obtain yours from http://www.last.fm/api/account for Last.fm
API_KEY = lastfm_api_key
API_SECRET = lastfm_api_secret
# In order to perform a write operation you need to authenticate yourself
username = lastfm_username
password_hash = pylast.md5(lastfm_password)
network = pylast.LastFMNetwork(api_key = API_KEY, api_secret =
API_SECRET, username = username, password_hash = password_hash)
# now you can use that object every where
artist = network.get_artist("The Beatles")
#artist.shout("<3")
track = network.get_track("Iron Maiden", "The Nomad")
#track.love()
#track.add_tags(("awesome", "favorite"))
# type help(pylast.LastFMNetwork) or help(pylast) in a python interpreter to get more help
# about anything and see examples of how it works
artist.get_listener_count()
3192937
albums = artist.get_top_albums()
help(albums[0])
Help on TopItem in module pylast object: class TopItem(__builtin__.tuple) | TopItem(item, weight) | | Method resolution order: | TopItem | __builtin__.tuple | __builtin__.object | | Methods defined here: | | __getnewargs__(self) | Return self as a plain tuple. Used by copy and pickle. | | __repr__(self) | Return a nicely formatted representation string | | _asdict(self) | Return a new OrderedDict which maps field names to their values | | _replace(_self, **kwds) | Return a new TopItem object replacing specified fields with new values | | ---------------------------------------------------------------------- | Class methods defined here: | | _make(cls, iterable, new=<built-in method __new__ of type object>, len=<built-in function len>) from __builtin__.type | Make a new TopItem object from a sequence or iterable | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(_cls, item, weight) | Create new instance of TopItem(item, weight) | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | Return a new OrderedDict which maps field names to their values | | item | Alias for field number 0 | | weight | Alias for field number 1 | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | _fields = ('item', 'weight') | | ---------------------------------------------------------------------- | Methods inherited from __builtin__.tuple: | | __add__(...) | x.__add__(y) <==> x+y | | __contains__(...) | x.__contains__(y) <==> y in x | | __eq__(...) | x.__eq__(y) <==> x==y | | __ge__(...) | x.__ge__(y) <==> x>=y | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] | | Use of negative indices is not supported. | | __gt__(...) | x.__gt__(y) <==> x>y | | __hash__(...) | x.__hash__() <==> hash(x) | | __iter__(...) | x.__iter__() <==> iter(x) | | __le__(...) | x.__le__(y) <==> x<=y | | __len__(...) | x.__len__() <==> len(x) | | __lt__(...) | x.__lt__(y) <==> x<y | | __mul__(...) | x.__mul__(n) <==> x*n | | __ne__(...) | x.__ne__(y) <==> x!=y | | __rmul__(...) | x.__rmul__(n) <==> n*x | | __sizeof__(...) | T.__sizeof__() -- size of T in memory, in bytes | | count(...) | T.count(value) -> integer -- return number of occurrences of value | | index(...) | T.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present.
help(albums[0].item)
Help on Album in module pylast object: class Album(_BaseObject, _Taggable) | An album. | | Method resolution order: | Album | _BaseObject | _Taggable | __builtin__.object | | Methods defined here: | | __eq__(self, other) | | __init__(self, artist, title, network) | Create an album instance. | # Parameters: | * artist: An artist name or an Artist object. | * title: The album title. | | __ne__(self, other) | | __repr__(self) | | __str__ = r(*args) | | get_artist(self) | Returns the associated Artist object. | | get_cover_image(self, size=3) | Returns a uri to the cover image | size can be one of: | COVER_EXTRA_LARGE | COVER_LARGE | COVER_MEDIUM | COVER_SMALL | | get_id(self) | Returns the ID | | get_listener_count(self) | Returns the number of liteners on the network | | get_mbid(self) | Returns the MusicBrainz id of the album. | | get_name(self) | Returns the album title (alias to Album.get_title). | | get_playcount(self) | Returns the number of plays on the network | | get_release_date(self) | Retruns the release date of the album. | | get_title(self) | Returns the album title. | | get_top_tags(self, limit=None) | Returns a list of the most-applied tags to this album. | | get_tracks(self) | Returns the list of Tracks on this album. | | get_url(self, domain_name=0) | Returns the url of the album page on the network. | # Parameters: | * domain_name str: The network's language domain. Possible values: | o DOMAIN_ENGLISH | o DOMAIN_GERMAN | o DOMAIN_SPANISH | o DOMAIN_FRENCH | o DOMAIN_ITALIAN | o DOMAIN_POLISH | o DOMAIN_PORTUGUESE | o DOMAIN_SWEDISH | o DOMAIN_TURKISH | o DOMAIN_RUSSIAN | o DOMAIN_JAPANESE | o DOMAIN_CHINESE | | get_wiki_content(self) | Returns the content of the wiki. | | get_wiki_published_date(self) | Returns the date of publishing this version of the wiki. | | get_wiki_summary(self) | Returns the summary of the wiki. | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | artist = None | | title = None | | ---------------------------------------------------------------------- | Methods inherited from _BaseObject: | | __hash__(self) | | ---------------------------------------------------------------------- | Data descriptors inherited from _BaseObject: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Data and other attributes inherited from _BaseObject: | | network = None | | ---------------------------------------------------------------------- | Methods inherited from _Taggable: | | add_tag(self, tag) | Adds one tag. | * tag: a tag name or a Tag object. | | add_tags(self, tags) | Adds one or several tags. | * tags: A sequence of tag names or Tag objects. | | clear_tags(self) | Clears all the user-set tags. | | get_tags(self) | Returns a list of the tags set by the user to this object. | | remove_tag(self, tag) | Remove a user's tag from this object. | | remove_tags(self, tags) | Removes one or several tags from this object. | * tags: a sequence of tag names or Tag objects. | | set_tags(self, tags) | Sets this object's tags to only those tags. | * tags: a sequence of tag names or Tag objects.
for album in albums:
print album.item.get_title() + ', ',
Abbey Road, Revolver, Sgt. Pepper's Lonely Hearts Club Band, Rubber Soul, Magical Mystery Tour, Let It Be, Help!, Please Please Me, A Hard Day's Night, With The Beatles, Beatles for Sale, Yellow Submarine, Love, The Beatles, The Beatles (The White Album), The Beatles 1, Let It Be... Naked, The White Album, Past Masters, Vol. 2, 1962-1966, Hey Jude, Anthology 1, Anthology 2, Past Masters, Vol. 1, Anthology 3, 1967-1970, Live at the BBC, The Alternate Abbey Road, The Beatles (The White Album) (disc 1), In The Beginning, Love Songs, The Beatles (The White Album) (disc 2), The Decca Tapes, Live at the BBC (disc 1), Live at the BBC (disc 2), 1967-1970 (disc 1), The Beatles (disc 1), White Album (Disc 1), Free as a Bird, Yellow Submarine Songtrack, Anthology 1 (disc 1), Anthology 3 (disc 1), Get Back, Meet the Beatles, Anthology 3 (disc 2), Beatles VI, Anthology 2 (disc 1), Anthology 1 (disc 2), Love Me Do, Anthology 2 (disc 2),
Or you can get things yourself as XML or json
import urllib2
import json
artist = 'The Beatles'
ret_format = 'json'
query_url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getTags&artist=%s&user=%s&api_key=%s&format=json' % (artist, lastfm_username, lastfm_api_key)
response = urllib2.urlopen(query_url)
data = json.load(response)
--------------------------------------------------------------------------- BadStatusLine Traceback (most recent call last) <ipython-input-29-473972d9de80> in <module>() ----> 1 response = urllib2.urlopen(query_url) 2 data = json.load(response) /usr/lib/python2.7/urllib2.pyc in urlopen(url, data, timeout) 125 if _opener is None: 126 _opener = build_opener() --> 127 return _opener.open(url, data, timeout) 128 129 def install_opener(opener): /usr/lib/python2.7/urllib2.pyc in open(self, fullurl, data, timeout) 402 req = meth(req) 403 --> 404 response = self._open(req, data) 405 406 # post-process response /usr/lib/python2.7/urllib2.pyc in _open(self, req, data) 420 protocol = req.get_type() 421 result = self._call_chain(self.handle_open, protocol, protocol + --> 422 '_open', req) 423 if result: 424 return result /usr/lib/python2.7/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args) 380 func = getattr(handler, meth_name) 381 --> 382 result = func(*args) 383 if result is not None: 384 return result /usr/lib/python2.7/urllib2.pyc in http_open(self, req) 1212 1213 def http_open(self, req): -> 1214 return self.do_open(httplib.HTTPConnection, req) 1215 1216 http_request = AbstractHTTPHandler.do_request_ /usr/lib/python2.7/urllib2.pyc in do_open(self, http_class, req) 1185 else: 1186 try: -> 1187 r = h.getresponse(buffering=True) 1188 except TypeError: # buffering kw not supported 1189 r = h.getresponse() /usr/lib/python2.7/httplib.pyc in getresponse(self, buffering) 1043 response = self.response_class(*args, **kwds) 1044 -> 1045 response.begin() 1046 assert response.will_close != _UNKNOWN 1047 self.__state = _CS_IDLE /usr/lib/python2.7/httplib.pyc in begin(self) 407 # read until we get a non-100 response 408 while True: --> 409 version, status, reason = self._read_status() 410 if status != CONTINUE: 411 break /usr/lib/python2.7/httplib.pyc in _read_status(self) 371 # Presumably, the server closed the connection before 372 # sending a valid response. --> 373 raise BadStatusLine(line) 374 try: 375 [version, status, reason] = line.split(None, 2) BadStatusLine: ''
Hmm... what's wrong?
artist_enc = urllib2.quote(artist)
artist_enc
'The%20Beatles'
query_url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getTags&artist=%s&user=%s&api_key=%s&format=json' % (artist_enc, lastfm_username, lastfm_api_key)
response = urllib2.urlopen(query_url)
data = json.load(response)
data
{u'tags': {u'#text': u'\n', u'artist': u'The Beatles'}}
artist = 'Lady Gaga'
artist_enc = urllib2.quote(artist)
artist_enc
query_url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getTags&artist=%s&user=%s&api_key=%s&format=json' % (artist_enc, lastfm_username, lastfm_api_key)
response = urllib2.urlopen(query_url)
data = json.load(response)
data
{u'tags': {u'#text': u'\n', u'artist': u'Lady Gaga'}}
artist = 'The Beatles'
artist_enc = urllib2.quote(artist)
artist_enc
query_url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getTopTags&artist=%s&user=%s&api_key=%s&format=json' % (artist_enc, lastfm_username, lastfm_api_key)
response = urllib2.urlopen(query_url)
data = json.load(response)
data
{u'toptags': {u'@attr': {u'artist': u'The Beatles'}, u'tag': [{u'count': u'100', u'name': u'classic rock', u'url': u'http://www.last.fm/tag/classic%20rock'}, {u'count': u'71', u'name': u'rock', u'url': u'http://www.last.fm/tag/rock'}, {u'count': u'57', u'name': u'british', u'url': u'http://www.last.fm/tag/british'}, {u'count': u'54', u'name': u'60s', u'url': u'http://www.last.fm/tag/60s'}, {u'count': u'40', u'name': u'pop', u'url': u'http://www.last.fm/tag/pop'}, {u'count': u'12', u'name': u'psychedelic', u'url': u'http://www.last.fm/tag/psychedelic'}, {u'count': u'10', u'name': u'The Beatles', u'url': u'http://www.last.fm/tag/the%20beatles'}, {u'count': u'10', u'name': u'oldies', u'url': u'http://www.last.fm/tag/oldies'}, {u'count': u'5', u'name': u'beatles', u'url': u'http://www.last.fm/tag/beatles'}, {u'count': u'5', u'name': u'Psychedelic Rock', u'url': u'http://www.last.fm/tag/psychedelic%20rock'}, {u'count': u'4', u'name': u'british invasion', u'url': u'http://www.last.fm/tag/british%20invasion'}, {u'count': u'3', u'name': u'alternative', u'url': u'http://www.last.fm/tag/alternative'}, {u'count': u'3', u'name': u'britpop', u'url': u'http://www.last.fm/tag/britpop'}, {u'count': u'3', u'name': u'Rock and Roll', u'url': u'http://www.last.fm/tag/rock%20and%20roll'}, {u'count': u'3', u'name': u'pop rock', u'url': u'http://www.last.fm/tag/pop%20rock'}, {u'count': u'3', u'name': u'classic', u'url': u'http://www.last.fm/tag/classic'}, {u'count': u'2', u'name': u'70s', u'url': u'http://www.last.fm/tag/70s'}, {u'count': u'2', u'name': u'indie', u'url': u'http://www.last.fm/tag/indie'}, {u'count': u'2', u'name': u'UK', u'url': u'http://www.last.fm/tag/uk'}, {u'count': u'2', u'name': u'rock n roll', u'url': u'http://www.last.fm/tag/rock%20n%20roll'}, {u'count': u'2', u'name': u'legend', u'url': u'http://www.last.fm/tag/legend'}, {u'count': u'1', u'name': u'liverpool', u'url': u'http://www.last.fm/tag/liverpool'}, {u'count': u'1', u'name': u'favorites', u'url': u'http://www.last.fm/tag/favorites'}, {u'count': u'1', u'name': u'experimental', u'url': u'http://www.last.fm/tag/experimental'}, {u'count': u'1', u'name': u'male vocalists', u'url': u'http://www.last.fm/tag/male%20vocalists'}, {u'count': u'1', u'name': u'singer-songwriter', u'url': u'http://www.last.fm/tag/singer-songwriter'}, {u'count': u'1', u'name': u'Progressive rock', u'url': u'http://www.last.fm/tag/progressive%20rock'}, {u'count': u'1', u'name': u'beat', u'url': u'http://www.last.fm/tag/beat'}, {u'count': u'1', u'name': u'john lennon', u'url': u'http://www.last.fm/tag/john%20lennon'}, {u'count': u'1', u'name': u'Love', u'url': u'http://www.last.fm/tag/love'}, {u'count': u'1', u'name': u"60's", u'url': u'http://www.last.fm/tag/60%27s'}, {u'count': u'1', u'name': u'indie rock', u'url': u'http://www.last.fm/tag/indie%20rock'}, {u'count': u'1', u'name': u'jecks', u'url': u'http://www.last.fm/tag/jecks'}, {u'count': u'1', u'name': u'english', u'url': u'http://www.last.fm/tag/english'}, {u'count': u'1', u'name': u'Awesome', u'url': u'http://www.last.fm/tag/awesome'}, {u'count': u'1', u'name': u'folk', u'url': u'http://www.last.fm/tag/folk'}, {u'count': u'1', u'name': u'genius', u'url': u'http://www.last.fm/tag/genius'}, {u'count': u'0', u'name': u'paul mccartney', u'url': u'http://www.last.fm/tag/paul%20mccartney'}, {u'count': u'0', u'name': u'groovy', u'url': u'http://www.last.fm/tag/groovy'}, {u'count': u'0', u'name': u'alternative rock', u'url': u'http://www.last.fm/tag/alternative%20rock'}, {u'count': u'0', u'name': u'george harrison', u'url': u'http://www.last.fm/tag/george%20harrison'}, {u'count': u'0', u'name': u'acoustic', u'url': u'http://www.last.fm/tag/acoustic'}, {u'count': u'0', u'name': u'soul', u'url': u'http://www.last.fm/tag/soul'}, {u'count': u'0', u'name': u'england', u'url': u'http://www.last.fm/tag/england'}, {u'count': u'0', u'name': u'Favorite', u'url': u'http://www.last.fm/tag/favorite'}, {u'count': u'0', u'name': u"rock'n'roll", u'url': u'http://www.last.fm/tag/rock%27n%27roll'}, {u'count': u'0', u'name': u'psychedelic pop', u'url': u'http://www.last.fm/tag/psychedelic%20pop'}, {u'count': u'0', u'name': u'merseybeat', u'url': u'http://www.last.fm/tag/merseybeat'}, {u'count': u'0', u'name': u'Best Band EVER', u'url': u'http://www.last.fm/tag/best%20band%20ever'}, {u'count': u'0', u'name': u'funk', u'url': u'http://www.last.fm/tag/funk'}, {u'count': u'0', u'name': u'hard rock', u'url': u'http://www.last.fm/tag/hard%20rock'}, {u'count': u'0', u'name': u'jazz', u'url': u'http://www.last.fm/tag/jazz'}, {u'count': u'0', u'name': u'british rock', u'url': u'http://www.last.fm/tag/british%20rock'}, {u'count': u'0', u'name': u'Pop-Rock', u'url': u'http://www.last.fm/tag/pop-rock'}, {u'count': u'0', u'name': u'legends', u'url': u'http://www.last.fm/tag/legends'}, {u'count': u'0', u'name': u'ringo starr', u'url': u'http://www.last.fm/tag/ringo%20starr'}, {u'count': u'0', u'name': u'blues', u'url': u'http://www.last.fm/tag/blues'}, {u'count': u'0', u'name': u'funky', u'url': u'http://www.last.fm/tag/funky'}, {u'count': u'0', u'name': u'overrated', u'url': u'http://www.last.fm/tag/overrated'}, {u'count': u'0', u'name': u'folk rock', u'url': u'http://www.last.fm/tag/folk%20rock'}, {u'count': u'0', u'name': u'British Psychedelia', u'url': u'http://www.last.fm/tag/british%20psychedelia'}, {u'count': u'0', u'name': u'classics', u'url': u'http://www.last.fm/tag/classics'}, {u'count': u'0', u'name': u'Favorite Artists', u'url': u'http://www.last.fm/tag/favorite%20artists'}, {u'count': u'0', u'name': u'classic pop', u'url': u'http://www.last.fm/tag/classic%20pop'}, {u'count': u'0', u'name': u'Favourites', u'url': u'http://www.last.fm/tag/favourites'}, {u'count': u'0', u'name': u'the best', u'url': u'http://www.last.fm/tag/the%20best'}, {u'count': u'0', u'name': u'metal', u'url': u'http://www.last.fm/tag/metal'}, {u'count': u'0', u'name': u'rock & roll', u'url': u'http://www.last.fm/tag/rock%2B%2526%2Broll'}, {u'count': u'0', u'name': u'amazing', u'url': u'http://www.last.fm/tag/amazing'}, {u'count': u'0', u'name': u'1960s', u'url': u'http://www.last.fm/tag/1960s'}, {u'count': u'0', u'name': u'emo', u'url': u'http://www.last.fm/tag/emo'}, {u'count': u'0', u'name': u'Pop/Rock', u'url': u'http://www.last.fm/tag/pop%252Frock'}, {u'count': u'0', u'name': u'electronic', u'url': u'http://www.last.fm/tag/electronic'}, {u'count': u'0', u'name': u'favourite', u'url': u'http://www.last.fm/tag/favourite'}, {u'count': u'0', u'name': u'Progressive', u'url': u'http://www.last.fm/tag/progressive'}, {u'count': u'0', u'name': u'80s', u'url': u'http://www.last.fm/tag/80s'}, {u'count': u'0', u'name': u'instrumental', u'url': u'http://www.last.fm/tag/instrumental'}, {u'count': u'0', u'name': u'punk', u'url': u'http://www.last.fm/tag/punk'}, {u'count': u'0', u'name': u'folk-rock', u'url': u'http://www.last.fm/tag/folk-rock'}, {u'count': u'0', u'name': u'brit pop', u'url': u'http://www.last.fm/tag/brit%20pop'}, {u'count': u'0', u'name': u'sixties', u'url': u'http://www.last.fm/tag/sixties'}, {u'count': u'0', u'name': u'soft rock', u'url': u'http://www.last.fm/tag/soft%20rock'}, {u'count': u'0', u'name': u'fun', u'url': u'http://www.last.fm/tag/fun'}, {u'count': u'0', u'name': u'influential', u'url': u'http://www.last.fm/tag/influential'}, {u'count': u'0', u'name': u'60s rock', u'url': u'http://www.last.fm/tag/60s%20rock'}, {u'count': u'0', u'name': u'rock-n-roll', u'url': u'http://www.last.fm/tag/rock-n-roll'}, {u'count': u'0', u'name': u'best', u'url': u'http://www.last.fm/tag/best'}, {u'count': u'0', u'name': u'art rock', u'url': u'http://www.last.fm/tag/art%20rock'}, {u'count': u'0', u'name': u'guitar', u'url': u'http://www.last.fm/tag/guitar'}, {u'count': u'0', u'name': u'power pop', u'url': u'http://www.last.fm/tag/power%20pop'}, {u'count': u'0', u'name': u'good music', u'url': u'http://www.last.fm/tag/good%20music'}, {u'count': u'0', u'name': u'indie pop', u'url': u'http://www.last.fm/tag/indie%20pop'}, {u'count': u'0', u'name': u'baroque pop', u'url': u'http://www.last.fm/tag/baroque%20pop'}, {u'count': u'0', u'name': u'great', u'url': u'http://www.last.fm/tag/great'}, {u'count': u'0', u'name': u'happy', u'url': u'http://www.last.fm/tag/happy'}, {u'count': u'0', u'name': u'chillout', u'url': u'http://www.last.fm/tag/chillout'}, {u'count': u'0', u'name': u'Favorite Bands', u'url': u'http://www.last.fm/tag/favorite%20bands'}, {u'count': u'0', u'name': u'brit rock', u'url': u'http://www.last.fm/tag/brit%20rock'}, {u'count': u'0', u'name': u'Britrock', u'url': u'http://www.last.fm/tag/britrock'}, {u'count': u'0', u'name': u'band', u'url': u'http://www.last.fm/tag/band'}]}}
for tag in data['toptags']['tag']:
print tag
{u'count': u'100', u'url': u'http://www.last.fm/tag/classic%20rock', u'name': u'classic rock'} {u'count': u'71', u'url': u'http://www.last.fm/tag/rock', u'name': u'rock'} {u'count': u'57', u'url': u'http://www.last.fm/tag/british', u'name': u'british'} {u'count': u'54', u'url': u'http://www.last.fm/tag/60s', u'name': u'60s'} {u'count': u'40', u'url': u'http://www.last.fm/tag/pop', u'name': u'pop'} {u'count': u'12', u'url': u'http://www.last.fm/tag/psychedelic', u'name': u'psychedelic'} {u'count': u'10', u'url': u'http://www.last.fm/tag/the%20beatles', u'name': u'The Beatles'} {u'count': u'10', u'url': u'http://www.last.fm/tag/oldies', u'name': u'oldies'} {u'count': u'5', u'url': u'http://www.last.fm/tag/beatles', u'name': u'beatles'} {u'count': u'5', u'url': u'http://www.last.fm/tag/psychedelic%20rock', u'name': u'Psychedelic Rock'} {u'count': u'4', u'url': u'http://www.last.fm/tag/british%20invasion', u'name': u'british invasion'} {u'count': u'3', u'url': u'http://www.last.fm/tag/alternative', u'name': u'alternative'} {u'count': u'3', u'url': u'http://www.last.fm/tag/britpop', u'name': u'britpop'} {u'count': u'3', u'url': u'http://www.last.fm/tag/rock%20and%20roll', u'name': u'Rock and Roll'} {u'count': u'3', u'url': u'http://www.last.fm/tag/pop%20rock', u'name': u'pop rock'} {u'count': u'3', u'url': u'http://www.last.fm/tag/classic', u'name': u'classic'} {u'count': u'2', u'url': u'http://www.last.fm/tag/70s', u'name': u'70s'} {u'count': u'2', u'url': u'http://www.last.fm/tag/indie', u'name': u'indie'} {u'count': u'2', u'url': u'http://www.last.fm/tag/uk', u'name': u'UK'} {u'count': u'2', u'url': u'http://www.last.fm/tag/rock%20n%20roll', u'name': u'rock n roll'} {u'count': u'2', u'url': u'http://www.last.fm/tag/legend', u'name': u'legend'} {u'count': u'1', u'url': u'http://www.last.fm/tag/liverpool', u'name': u'liverpool'} {u'count': u'1', u'url': u'http://www.last.fm/tag/favorites', u'name': u'favorites'} {u'count': u'1', u'url': u'http://www.last.fm/tag/experimental', u'name': u'experimental'} {u'count': u'1', u'url': u'http://www.last.fm/tag/male%20vocalists', u'name': u'male vocalists'} {u'count': u'1', u'url': u'http://www.last.fm/tag/singer-songwriter', u'name': u'singer-songwriter'} {u'count': u'1', u'url': u'http://www.last.fm/tag/progressive%20rock', u'name': u'Progressive rock'} {u'count': u'1', u'url': u'http://www.last.fm/tag/beat', u'name': u'beat'} {u'count': u'1', u'url': u'http://www.last.fm/tag/john%20lennon', u'name': u'john lennon'} {u'count': u'1', u'url': u'http://www.last.fm/tag/love', u'name': u'Love'} {u'count': u'1', u'url': u'http://www.last.fm/tag/60%27s', u'name': u"60's"} {u'count': u'1', u'url': u'http://www.last.fm/tag/indie%20rock', u'name': u'indie rock'} {u'count': u'1', u'url': u'http://www.last.fm/tag/jecks', u'name': u'jecks'} {u'count': u'1', u'url': u'http://www.last.fm/tag/english', u'name': u'english'} {u'count': u'1', u'url': u'http://www.last.fm/tag/awesome', u'name': u'Awesome'} {u'count': u'1', u'url': u'http://www.last.fm/tag/folk', u'name': u'folk'} {u'count': u'1', u'url': u'http://www.last.fm/tag/genius', u'name': u'genius'} {u'count': u'0', u'url': u'http://www.last.fm/tag/paul%20mccartney', u'name': u'paul mccartney'} {u'count': u'0', u'url': u'http://www.last.fm/tag/groovy', u'name': u'groovy'} {u'count': u'0', u'url': u'http://www.last.fm/tag/alternative%20rock', u'name': u'alternative rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/george%20harrison', u'name': u'george harrison'} {u'count': u'0', u'url': u'http://www.last.fm/tag/acoustic', u'name': u'acoustic'} {u'count': u'0', u'url': u'http://www.last.fm/tag/soul', u'name': u'soul'} {u'count': u'0', u'url': u'http://www.last.fm/tag/england', u'name': u'england'} {u'count': u'0', u'url': u'http://www.last.fm/tag/favorite', u'name': u'Favorite'} {u'count': u'0', u'url': u'http://www.last.fm/tag/rock%27n%27roll', u'name': u"rock'n'roll"} {u'count': u'0', u'url': u'http://www.last.fm/tag/psychedelic%20pop', u'name': u'psychedelic pop'} {u'count': u'0', u'url': u'http://www.last.fm/tag/merseybeat', u'name': u'merseybeat'} {u'count': u'0', u'url': u'http://www.last.fm/tag/best%20band%20ever', u'name': u'Best Band EVER'} {u'count': u'0', u'url': u'http://www.last.fm/tag/funk', u'name': u'funk'} {u'count': u'0', u'url': u'http://www.last.fm/tag/hard%20rock', u'name': u'hard rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/jazz', u'name': u'jazz'} {u'count': u'0', u'url': u'http://www.last.fm/tag/british%20rock', u'name': u'british rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/pop-rock', u'name': u'Pop-Rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/legends', u'name': u'legends'} {u'count': u'0', u'url': u'http://www.last.fm/tag/ringo%20starr', u'name': u'ringo starr'} {u'count': u'0', u'url': u'http://www.last.fm/tag/blues', u'name': u'blues'} {u'count': u'0', u'url': u'http://www.last.fm/tag/funky', u'name': u'funky'} {u'count': u'0', u'url': u'http://www.last.fm/tag/overrated', u'name': u'overrated'} {u'count': u'0', u'url': u'http://www.last.fm/tag/folk%20rock', u'name': u'folk rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/british%20psychedelia', u'name': u'British Psychedelia'} {u'count': u'0', u'url': u'http://www.last.fm/tag/classics', u'name': u'classics'} {u'count': u'0', u'url': u'http://www.last.fm/tag/favorite%20artists', u'name': u'Favorite Artists'} {u'count': u'0', u'url': u'http://www.last.fm/tag/classic%20pop', u'name': u'classic pop'} {u'count': u'0', u'url': u'http://www.last.fm/tag/favourites', u'name': u'Favourites'} {u'count': u'0', u'url': u'http://www.last.fm/tag/the%20best', u'name': u'the best'} {u'count': u'0', u'url': u'http://www.last.fm/tag/metal', u'name': u'metal'} {u'count': u'0', u'url': u'http://www.last.fm/tag/rock%2B%2526%2Broll', u'name': u'rock & roll'} {u'count': u'0', u'url': u'http://www.last.fm/tag/amazing', u'name': u'amazing'} {u'count': u'0', u'url': u'http://www.last.fm/tag/1960s', u'name': u'1960s'} {u'count': u'0', u'url': u'http://www.last.fm/tag/emo', u'name': u'emo'} {u'count': u'0', u'url': u'http://www.last.fm/tag/pop%252Frock', u'name': u'Pop/Rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/electronic', u'name': u'electronic'} {u'count': u'0', u'url': u'http://www.last.fm/tag/favourite', u'name': u'favourite'} {u'count': u'0', u'url': u'http://www.last.fm/tag/progressive', u'name': u'Progressive'} {u'count': u'0', u'url': u'http://www.last.fm/tag/80s', u'name': u'80s'} {u'count': u'0', u'url': u'http://www.last.fm/tag/instrumental', u'name': u'instrumental'} {u'count': u'0', u'url': u'http://www.last.fm/tag/punk', u'name': u'punk'} {u'count': u'0', u'url': u'http://www.last.fm/tag/folk-rock', u'name': u'folk-rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/brit%20pop', u'name': u'brit pop'} {u'count': u'0', u'url': u'http://www.last.fm/tag/sixties', u'name': u'sixties'} {u'count': u'0', u'url': u'http://www.last.fm/tag/soft%20rock', u'name': u'soft rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/fun', u'name': u'fun'} {u'count': u'0', u'url': u'http://www.last.fm/tag/influential', u'name': u'influential'} {u'count': u'0', u'url': u'http://www.last.fm/tag/60s%20rock', u'name': u'60s rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/rock-n-roll', u'name': u'rock-n-roll'} {u'count': u'0', u'url': u'http://www.last.fm/tag/best', u'name': u'best'} {u'count': u'0', u'url': u'http://www.last.fm/tag/art%20rock', u'name': u'art rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/guitar', u'name': u'guitar'} {u'count': u'0', u'url': u'http://www.last.fm/tag/power%20pop', u'name': u'power pop'} {u'count': u'0', u'url': u'http://www.last.fm/tag/good%20music', u'name': u'good music'} {u'count': u'0', u'url': u'http://www.last.fm/tag/indie%20pop', u'name': u'indie pop'} {u'count': u'0', u'url': u'http://www.last.fm/tag/baroque%20pop', u'name': u'baroque pop'} {u'count': u'0', u'url': u'http://www.last.fm/tag/great', u'name': u'great'} {u'count': u'0', u'url': u'http://www.last.fm/tag/happy', u'name': u'happy'} {u'count': u'0', u'url': u'http://www.last.fm/tag/chillout', u'name': u'chillout'} {u'count': u'0', u'url': u'http://www.last.fm/tag/favorite%20bands', u'name': u'Favorite Bands'} {u'count': u'0', u'url': u'http://www.last.fm/tag/brit%20rock', u'name': u'brit rock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/britrock', u'name': u'Britrock'} {u'count': u'0', u'url': u'http://www.last.fm/tag/band', u'name': u'band'}
for tag in data['toptags']['tag']:
print tag['name'], ':', int(tag['count'])
classic rock : 100 rock : 71 british : 57 60s : 54 pop : 40 psychedelic : 12 The Beatles : 10 oldies : 10 beatles : 5 Psychedelic Rock : 5 british invasion : 4 alternative : 3 britpop : 3 Rock and Roll : 3 pop rock : 3 classic : 3 70s : 2 indie : 2 UK : 2 rock n roll : 2 legend : 2 liverpool : 1 favorites : 1 experimental : 1 male vocalists : 1 singer-songwriter : 1 Progressive rock : 1 beat : 1 john lennon : 1 Love : 1 60's : 1 indie rock : 1 jecks : 1 english : 1 Awesome : 1 folk : 1 genius : 1 paul mccartney : 0 groovy : 0 alternative rock : 0 george harrison : 0 acoustic : 0 soul : 0 england : 0 Favorite : 0 rock'n'roll : 0 psychedelic pop : 0 merseybeat : 0 Best Band EVER : 0 funk : 0 hard rock : 0 jazz : 0 british rock : 0 Pop-Rock : 0 legends : 0 ringo starr : 0 blues : 0 funky : 0 overrated : 0 folk rock : 0 British Psychedelia : 0 classics : 0 Favorite Artists : 0 classic pop : 0 Favourites : 0 the best : 0 metal : 0 rock & roll : 0 amazing : 0 1960s : 0 emo : 0 Pop/Rock : 0 electronic : 0 favourite : 0 Progressive : 0 80s : 0 instrumental : 0 punk : 0 folk-rock : 0 brit pop : 0 sixties : 0 soft rock : 0 fun : 0 influential : 0 60s rock : 0 rock-n-roll : 0 best : 0 art rock : 0 guitar : 0 power pop : 0 good music : 0 indie pop : 0 baroque pop : 0 great : 0 happy : 0 chillout : 0 Favorite Bands : 0 brit rock : 0 Britrock : 0 band : 0
artist = 'The Beatles'
artist_enc = urllib2.quote(artist)
artist_enc
query_url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getTopTracks&artist=%s&user=%s&api_key=%s&format=json' % (artist_enc, lastfm_username, lastfm_api_key)
response = urllib2.urlopen(query_url)
data = json.load(response)
#data
data.keys()
[u'toptracks']
data['toptracks'].keys()
[u'track', u'@attr']
len(data['toptracks']['track'])
50
for track in data['toptracks']['track']:
print "'%s', "%track['name']
'Come Together', 'Here Comes the Sun', 'Let It Be', 'Yesterday', 'Something', 'Eleanor Rigby', 'Help!', 'Hey Jude', 'Yellow Submarine', 'All You Need Is Love', 'Lucy in the Sky with Diamonds', 'While My Guitar Gently Weeps', 'Love Me Do', 'Can't Buy Me Love', 'A Day in the Life', 'Strawberry Fields Forever', 'Get Back', 'A Hard Day's Night', 'I Want to Hold Your Hand', 'Blackbird', 'Ticket to Ride', 'Penny Lane', 'Oh! Darling', 'With a Little Help from My Friends', 'In My Life', 'Octopus's Garden', 'Across the Universe', 'Twist and Shout', 'Because', 'All My Loving', 'Ob-La-Di, Ob-La-Da', 'I Am the Walrus', 'Drive My Car', 'Golden Slumbers', 'And I Love Her', 'Eight Days a Week', 'Girl', 'Michelle', 'She Loves You', 'I Want You (She's So Heavy)', 'Maxwell's Silver Hammer', 'You Never Give Me Your Money', 'I Saw Her Standing There', 'Carry That Weight', 'Happiness Is a Warm Gun', 'I'm Only Sleeping', 'Sgt. Pepper's Lonely Hearts Club Band', 'Nowhere Man', 'Taxman', 'Dear Prudence',
data['toptracks']['track'][0].keys()
[u'streamable', u'@attr', u'name', u'artist', u'url', u'image', u'listeners', u'mbid', u'duration', u'playcount']
No tags here, must fetch them
track_name = data['toptracks']['track'][0]['name']
track_enc = urllib2.quote(track_name)
query_url = 'http://ws.audioscrobbler.com/2.0/?method=track.getTopTags&track=%s&user=%s&api_key=%s&format=json' % (track_enc, lastfm_username, lastfm_api_key)
response = urllib2.urlopen(query_url)
tags_data = json.load(response)
tags_data
{u'error': 6, u'links': [], u'message': u'You must supply either a track & artist name or a track mbid.'}
track_name = data['toptracks']['track'][0]['name']
track_enc = urllib2.quote(track_name)
query_url = 'http://ws.audioscrobbler.com/2.0/?method=track.getTopTags&artist=%s&track=%s&user=%s&api_key=%s&format=json' % (artist_enc, track_enc, lastfm_username, lastfm_api_key)
response = urllib2.urlopen(query_url)
tags_data = json.load(response)
tags_data
{u'toptags': {u'@attr': {u'artist': u'The Beatles', u'track': u'Come Together'}, u'tag': [{u'count': u'100', u'name': u'classic rock', u'url': u'http://www.last.fm/tag/classic%20rock'}, {u'count': u'66', u'name': u'rock', u'url': u'http://www.last.fm/tag/rock'}, {u'count': u'63', u'name': u'The Beatles', u'url': u'http://www.last.fm/tag/the%20beatles'}, {u'count': u'60', u'name': u'60s', u'url': u'http://www.last.fm/tag/60s'}, {u'count': u'45', u'name': u'british', u'url': u'http://www.last.fm/tag/british'}, {u'count': u'20', u'name': u'pop', u'url': u'http://www.last.fm/tag/pop'}, {u'count': u'16', u'name': u'beatles', u'url': u'http://www.last.fm/tag/beatles'}, {u'count': u'12', u'name': u'oldies', u'url': u'http://www.last.fm/tag/oldies'}, {u'count': u'11', u'name': u'psychedelic', u'url': u'http://www.last.fm/tag/psychedelic'}, {u'count': u'6', u'name': u'classic', u'url': u'http://www.last.fm/tag/classic'}, {u'count': u'6', u'name': u'come together', u'url': u'http://www.last.fm/tag/come%20together'}, {u'count': u'5', u'name': u'john lennon', u'url': u'http://www.last.fm/tag/john%20lennon'}, {u'count': u'5', u'name': u'Rock and Roll', u'url': u'http://www.last.fm/tag/rock%20and%20roll'}, {u'count': u'5', u'name': u'favorites', u'url': u'http://www.last.fm/tag/favorites'}, {u'count': u'4', u'name': u'1969', u'url': u'http://www.last.fm/tag/1969'}, {u'count': u'4', u'name': u'Psychedelic Rock', u'url': u'http://www.last.fm/tag/psychedelic%20rock'}, {u'count': u'4', u'name': u'britpop', u'url': u'http://www.last.fm/tag/britpop'}, {u'count': u'4', u'name': u'blues', u'url': u'http://www.last.fm/tag/blues'}, {u'count': u'4', u'name': u'rock n roll', u'url': u'http://www.last.fm/tag/rock%20n%20roll'}, {u'count': u'4', u'name': u'Abbey Road', u'url': u'http://www.last.fm/tag/abbey%20road'}, {u'count': u'4', u'name': u'british invasion', u'url': u'http://www.last.fm/tag/british%20invasion'}, {u'count': u'3', u'name': u'Awesome', u'url': u'http://www.last.fm/tag/awesome'}, {u'count': u'3', u'name': u'cool', u'url': u'http://www.last.fm/tag/cool'}, {u'count': u'3', u'name': u'UK', u'url': u'http://www.last.fm/tag/uk'}, {u'count': u'2', u'name': u'chill', u'url': u'http://www.last.fm/tag/chill'}, {u'count': u'2', u'name': u'best songs ever', u'url': u'http://www.last.fm/tag/best%20songs%20ever'}, {u'count': u'2', u'name': u'favorite songs', u'url': u'http://www.last.fm/tag/favorite%20songs'}, {u'count': u'2', u'name': u'experimental', u'url': u'http://www.last.fm/tag/experimental'}, {u'count': u'2', u'name': u'Favorite', u'url': u'http://www.last.fm/tag/favorite'}, {u'count': u'2', u'name': u'sexy', u'url': u'http://www.last.fm/tag/sexy'}, {u'count': u'2', u'name': u'Mellow', u'url': u'http://www.last.fm/tag/mellow'}, {u'count': u'1', u'name': u'70s', u'url': u'http://www.last.fm/tag/70s'}, {u'count': u'1', u'name': u'groovy', u'url': u'http://www.last.fm/tag/groovy'}, {u'count': u'1', u'name': u'classics', u'url': u'http://www.last.fm/tag/classics'}, {u'count': u'1', u'name': u'hard rock', u'url': u'http://www.last.fm/tag/hard%20rock'}, {u'count': u'1', u'name': u'male vocalists', u'url': u'http://www.last.fm/tag/male%20vocalists'}, {u'count': u'1', u'name': u'Love', u'url': u'http://www.last.fm/tag/love'}, {u'count': u'1', u'name': u'fip', u'url': u'http://www.last.fm/tag/fip'}, {u'count': u'1', u'name': u'British Psychedelia', u'url': u'http://www.last.fm/tag/british%20psychedelia'}, {u'count': u'1', u'name': u'pop rock', u'url': u'http://www.last.fm/tag/pop%20rock'}, {u'count': u'1', u'name': u'blues rock', u'url': u'http://www.last.fm/tag/blues%20rock'}, {u'count': u'1', u'name': u'legends', u'url': u'http://www.last.fm/tag/legends'}, {u'count': u'1', u'name': u'guitar', u'url': u'http://www.last.fm/tag/guitar'}, {u'count': u'1', u'name': u'paul mccartney', u'url': u'http://www.last.fm/tag/paul%20mccartney'}, {u'count': u'1', u'name': u'timeless', u'url': u'http://www.last.fm/tag/timeless'}, {u'count': u'1', u'name': u'Best Band EVER', u'url': u'http://www.last.fm/tag/best%20band%20ever'}, {u'count': u'1', u'name': u'liverpool', u'url': u'http://www.last.fm/tag/liverpool'}, {u'count': u'1', u'name': u'amazing', u'url': u'http://www.last.fm/tag/amazing'}, {u'count': u'1', u'name': u'alternative', u'url': u'http://www.last.fm/tag/alternative'}, {u'count': u'1', u'name': u'drums', u'url': u'http://www.last.fm/tag/drums'}, {u'count': u'1', u'name': u'favourite', u'url': u'http://www.last.fm/tag/favourite'}, {u'count': u'1', u'name': u'funky', u'url': u'http://www.last.fm/tag/funky'}, {u'count': u'1', u'name': u'catchy', u'url': u'http://www.last.fm/tag/catchy'}, {u'count': u'1', u'name': u'Favourites', u'url': u'http://www.last.fm/tag/favourites'}, {u'count': u'1', u'name': u'Rolling Stone 500 Greatest Songs of All Time', u'url': u'http://www.last.fm/tag/rolling%20stone%20500%20greatest%20songs%20of%20all%20time'}, {u'count': u'1', u'name': u'beat', u'url': u'http://www.last.fm/tag/beat'}, {u'count': u'0', u'name': u'Favorite Artists', u'url': u'http://www.last.fm/tag/favorite%20artists'}, {u'count': u'0', u'name': u'Rock Roll', u'url': u'http://www.last.fm/tag/rock%20%20roll'}, {u'count': u'0', u'name': u"60's", u'url': u'http://www.last.fm/tag/60%27s'}, {u'count': u'0', u'name': u'Porieux-loved', u'url': u'http://www.last.fm/tag/porieux-loved'}, {u'count': u'0', u'name': u'Energetic', u'url': u'http://www.last.fm/tag/energetic'}, {u'count': u'0', u'name': u'old school', u'url': u'http://www.last.fm/tag/old%20school'}, {u'count': u'0', u'name': u'the best', u'url': u'http://www.last.fm/tag/the%20best'}, {u'count': u'0', u'name': u'classic pop', u'url': u'http://www.last.fm/tag/classic%20pop'}, {u'count': u'0', u'name': u'poprock', u'url': u'http://www.last.fm/tag/poprock'}, {u'count': u'0', u'name': u'album rock', u'url': u'http://www.last.fm/tag/album%20rock'}, {u'count': u'0', u'name': u'groove', u'url': u'http://www.last.fm/tag/groove'}, {u'count': u'0', u'name': u'england', u'url': u'http://www.last.fm/tag/england'}, {u'count': u'0', u'name': u'perfect', u'url': u'http://www.last.fm/tag/perfect'}, {u'count': u'0', u'name': u'funk', u'url': u'http://www.last.fm/tag/funk'}, {u'count': u'0', u'name': u'indie', u'url': u'http://www.last.fm/tag/indie'}, {u'count': u'0', u'name': u'english', u'url': u'http://www.last.fm/tag/english'}, {u'count': u'0', u'name': u'brit rock', u'url': u'http://www.last.fm/tag/brit%20rock'}, {u'count': u'0', u'name': u'drugs', u'url': u'http://www.last.fm/tag/drugs'}, {u'count': u'0', u'name': u'great lyrics', u'url': u'http://www.last.fm/tag/great%20lyrics'}, {u'count': u'0', u'name': u'alternative rock', u'url': u'http://www.last.fm/tag/alternative%20rock'}, {u'count': u'0', u'name': u'best song ever', u'url': u'http://www.last.fm/tag/best%20song%20ever'}, {u'count': u'0', u'name': u'mpsvdloved', u'url': u'http://www.last.fm/tag/mpsvdloved'}, {u'count': u'0', u'name': u'male vocalist', u'url': u'http://www.last.fm/tag/male%20vocalist'}, {u'count': u'0', u'name': u'drug-influenced', u'url': u'http://www.last.fm/tag/drug-influenced'}, {u'count': u'0', u'name': u'george harrison', u'url': u'http://www.last.fm/tag/george%20harrison'}, {u'count': u'0', u'name': u'jecks', u'url': u'http://www.last.fm/tag/jecks'}, {u'count': u'0', u'name': u'Let It Be', u'url': u'http://www.last.fm/tag/let%20it%20be'}, {u'count': u'0', u'name': u'electric piano', u'url': u'http://www.last.fm/tag/electric%20piano'}, {u'count': u'0', u'name': u'Masterpiece', u'url': u'http://www.last.fm/tag/masterpiece'}, {u'count': u'0', u'name': u'Funk Rock', u'url': u'http://www.last.fm/tag/funk%20rock'}, {u'count': u'0', u'name': u'9 of 10 stars', u'url': u'http://www.last.fm/tag/9%20of%2010%20stars'}, {u'count': u'0', u'name': u'fun', u'url': u'http://www.last.fm/tag/fun'}, {u'count': u'0', u'name': u'Makes Me Smile', u'url': u'http://www.last.fm/tag/makes%20me%20smile'}, {u'count': u'0', u'name': u'1960s', u'url': u'http://www.last.fm/tag/1960s'}, {u'count': u'0', u'name': u'sixties', u'url': u'http://www.last.fm/tag/sixties'}, {u'count': u'0', u'name': u'Legendary', u'url': u'http://www.last.fm/tag/legendary'}, {u'count': u'0', u'name': u'amayzes loved', u'url': u'http://www.last.fm/tag/amayzes%20loved'}, {u'count': u'0', u'name': u'blues influences', u'url': u'http://www.last.fm/tag/blues%20influences'}, {u'count': u'0', u'name': u'the word come', u'url': u'http://www.last.fm/tag/the%20word%20come'}, {u'count': u'0', u'name': u'soft rock', u'url': u'http://www.last.fm/tag/soft%20rock'}, {u'count': u'0', u'name': u'together', u'url': u'http://www.last.fm/tag/together'}, {u'count': u'0', u'name': u'other', u'url': u'http://www.last.fm/tag/other'}, {u'count': u'0', u'name': u'sing', u'url': u'http://www.last.fm/tag/sing'}, {u'count': u'0', u'name': u'the word together', u'url': u'http://www.last.fm/tag/the%20word%20together'}]}}