1

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'}
1
  • From your code, artist_name is something you passed in from cmd line. It cannot be a JSON response. So you probably still need to edit your question to show the actual output. Commented Nov 22, 2016 at 3:30

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your reply. I've edited the answer upon your answer.
@data_garden I think the output you posted is still wrong because you showed a JSON object for something that's intended to be a cmd line argument. But I can see the artist name is already there and it shouldn't take too much effort to get it out and feed it to lastfm_artist_to_tags. Good luck.
0

based on @Cyker 's answer, and since get_artist() returns jsonresponse, 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)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.