data_garden data_garden - 24 days ago 9
Python Question

Python - retrieve all genre tags from Last.fm API

I have a function that uses

get_top_tags()
method to fecth top tags in
last.fm API
:

def lastfm_tags(tag):
tag_weight = {}

result = last.get_top_tags()

for tag in result:
tag_weight[str(tag.item.get_name())] = str(tag.weight)

tag_weight = {k: int(v) for k, v in tag_weight.items()}


return tag_weight

print lastfm_tags('pop')


prints:

{'industrial': 533792, 'punk': 849254, 'indie': 1972289, 'metal': 1214142, 'heavy metal': 652768, 'japanese': 428483, 'pop': 1874644, 'new wave': 399642, 'black metal': 772854, 'rap': 513369, 'ambient': 1030860, 'alternative': 2059622, 'hard rock': 821047, 'electronic': 2289385, 'blues': 531227, 'folk': 882684, 'classic rock': 1123889, 'alternative rock': 1123671, '90s': 447817, 'Progressive metal': 407423, 'indie rock': 850647, 'electronica': 614612, 'female vocalists': 1558073, 'Soundtrack': 529623, 'dance': 769325, 'psychedelic': 458873, '80s': 752035, 'piano': 410036, 'chillout': 636262, 'post-rock': 426621, 'punk rock': 518635, 'jazz': 1117396, 'seen live': 2098252, 'instrumental': 818216, 'singer-songwriter': 810484, 'acoustic': 461045, 'hardcore': 656385, 'funk': 399980, 'Classical': 539399, 'Hip-Hop': 814911, 'death metal': 671617, 'soul': 641317, 'british': 667770, 'thrash metal': 465438, 'hip hop': 395139, 'rock': 3879776, 'metalcore': 444620, 'german': 409117, 'Progressive rock': 693750, 'experimental': 1010752}


then I could also retrieve
tags
relative to artists:

def lastfm_artist_to_tags(artist):

tag_weight = {}

result = last.get_artist(artist).get_top_tags()

for tag in result:
tag_weight[str(tag.item.get_name())] = str(tag.weight)

tag_weight = {k: int(v) for k, v in tag_weight.items()}

return sorted(tag_weight.items(), key=lambda x: x[1], reverse=True)


which prints much more specific tagging:

[('shoegaze', 100), ('experimental', 72), ('psychedelic', 70), ('seen live', 61), ('indie', 56), ('indie rock', 53), ('ambient', 14), ('post-rock', 12), ('noise rock', 11), ('dream pop', 9), ('Psychedelic Rock', 8), ('Neo-Psychedelia', 7), ('american', 7), ('rock', 7), ('post-punk', 6), ('Experimental Rock', 4), ('alternative', 4), ('art rock', 4), ('noise', 4), ('atlanta', 4), ('00s', 3), ('alternative rock', 3), ('Garage Rock', 3), ('ambient punk', 3), ('georgia', 3), ('noise pop', 3), ('USA', 2), ('Lo-Fi', 2), ('Avant-Garde', 2), ('Dreamy', 2), ('Psychedelia', 2), ('shoegazing', 2), ('psychedelic pop', 2), ('space rock', 2), ('kranky', 2), ('indie pop', 2), ('male vocalists', 1), ('10s', 1), ('deerhunter', 1), ('avantgarde', 1), ('4ad', 1)]


is there a way or else a method -unknown to me, to fetch ALL possible tags?

Answer

According to the LastFM API, no there is not. You can only find tags associated to a specific entity (Artist, Album, Track, etc)

This is actually a common pattern that most companies use for APIs they provide. Imagine, if you could get all tags with all associated albums, artists and tags for free and stored them in a local copy. Would you be going back to their API / website for help anymore?