Reputation: 10033
I have this function defined in a module
called tags.py
:
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)
tags.py
, as main, is sucessfully called with with:
print lastfm_artist_to_tags('radiohead') //note string
but I am trying to import the above function into my playlist.py
, like so:
#playlist.py
from api.lastfm.seeds.tags import *
class hardrock(rock):
def __init__(self,name,user):
rock.__init__(self, name, user)
# get tags for this artist
tags = lastfm_artist_to_tags(str(artist))
and call it via command line:
if len(sys.argv) > 1:
artist_name = ' '.join(sys.argv[1:])
artist = get_artist(artist_name) //get_artist() returns json response from Spotify API
by : $ python playlist.py radiohead
but I get the following error:
Error: The artist you supplied could not be found
I've tried to pass lastfm_artist_to_tags(artist)
as well, to no avail.
folder structure:
playlist.py
api/
__init__.py
lastfm/
__init__.py
seeds/
__init__.py
tags.py
what am I missing here?
EDIT:
print (artist_name)
and print (str(artist_name))
return the same result form SPotify API
:
{u'genres': [u'alternative rock', u'britpop', u'indie rock', u'neo mellow', u'permanent wave', u'pop rock', u'rock'], u'name': u'Oasis', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/2DaxqgrOhkeH0fpeiQq2f4'}, u'popularity': 77, u'uri': u'spotify:artist:2DaxqgrOhkeH0fpeiQq2f4', u'href': u'https://api.spotify.com/v1/artists/2DaxqgrOhkeH0fpeiQq2f4', u'followers': {u'total': 1114541, u'href': None}, u'images': [{u'url': u'https://i.scdn.co/image/2a8c10fe954e2038fb74251cba601a5594cc5878', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/87d18c79bbfdb1905bb202d200e1c191afc46aa5', u'width': 320, u'height': 320}, {u'url': u'https://i.scdn.co/image/b4d024ebb4863438b92a1b029bff7f9737263a57', u'width': 160, u'height': 160}], u'type': u'artist', u'id': u'2DaxqgrOhkeH0fpeiQq2f4'}
{u'genres': [u'alternative rock', u'britpop', u'indie rock', u'neo mellow', u'permanent wave', u'pop rock', u'rock'], u'name': u'Oasis', u'external_urls': {u'spotify': u'https://open.spotify.com/artist/2DaxqgrOhkeH0fpeiQq2f4'}, u'popularity': 77, u'uri': u'spotify:artist:2DaxqgrOhkeH0fpeiQq2f4', u'href': u'https://api.spotify.com/v1/artists/2DaxqgrOhkeH0fpeiQq2f4', u'followers': {u'total': 1114541, u'href': None}, u'images': [{u'url': u'https://i.scdn.co/image/2a8c10fe954e2038fb74251cba601a5594cc5878', u'width': 640, u'height': 640}, {u'url': u'https://i.scdn.co/image/87d18c79bbfdb1905bb202d200e1c191afc46aa5', u'width': 320, u'height': 320}, {u'url': u'https://i.scdn.co/image/b4d024ebb4863438b92a1b029bff7f9737263a57', u'width': 160, u'height': 160}], u'type': u'artist', u'id': u'2DaxqgrOhkeH0fpeiQq2f4'}
Upvotes: 1
Views: 76
Reputation: 10033
based on @Cyker 's answer, and since get_artist()
returns json
response, I must fix it like this:
tags = lastfm_artist_to_tags(str(artist['name']))
then tags
will print:
[('britpop', 100), ('rock', 89), ('british', 61), ('alternative', 53), ('indie', 46), ('seen live', 21), ('alternative rock', 19), ('indie rock', 12), ('90s', 10), ('oasis', 9), ('pop', 7), ('Manchester', 4), ('UK', 4), ('classic rock', 3), ('Britrock', 3), ('male vocalists', 2), ('00s', 2), ('hard rock', 2), ('brit pop', 2), ('pop rock', 2), ('british rock', 2), ('english', 2), ('brit rock', 2), ('england', 1), ('favorites', 1), ('punk', 1)]
Upvotes: 0
Reputation: 10914
If this one works:
print lastfm_artist_to_tags('radiohead') //note string
It means the function lastfm_artist_to_tags
is working and you should seek problems elsewhere.
You probably should check whether you're indeed passing 'radiohead'
to the same function when called from another module. The easiest way is to print the parameter:
if len(sys.argv) > 1:
artist_name = ' '.join(sys.argv[1:])
print(artist_name) # Add this line
artist = get_artist(artist_name)
print(artist) # Add this line
print(str(artist)) # Add this line
tags = lastfm_artist_to_tags(str(artist))
This should give you some useful information about what's wrong with your code. If you find all printed variables are as expected then I'm happy to have a look back.
Upvotes: 2