pylast: get_userplaycount() returning None

What did you do?

i am trying to create a detailed “nowplaying” command on my bot

What did you expect to happen?

i expected the user’s artist & album plays on the nowplaying track would be easier to get after i figured out the track play count, but i have been struggling with this.

What actually happened?

image image

they just keep showing up as “None”, im sure i have the wrong code though, and it may be a simple fix but I can’t figure it out.

What versions are you using?

  • OS:
  • Python: 3.11
  • pylast:

Please include code that reproduces the issue. the codes that are giving me the issue is in:

artist_play_count = now_playing.artist.get_userplaycount()
album_play_count = now_playing.get_album().get_userplaycount()

The best reproductions are self-contained scripts with minimal dependencies.

usernames = {}
@bot.command(name='s')
async def setlastfm(ctx, username):
    usernames[ctx.author.id] = username
    await ctx.send(embed=discord.Embed(description=f'<:check:1159989656994193439> {ctx.author.mention} - set {username} as your **last.fm**', color=16776958))

@bot.command(name='t')
async def track(ctx):
    user = ctx.author
    username = usernames.get(user.id)
    if username: 
        network = pylast.LastFMNetwork(api_key=LASTFM_API_KEY, api_secret=LASTFM_API_SECRET)
        user_lastfm = network.get_user(username)
        now_playing = user_lastfm.get_now_playing()
        
        if now_playing:
            artist_name = now_playing.artist.get_name()
            user_lastfm = network.get_user(username)
            
            track_title = now_playing.get_name()
            track_play_count = now_playing.get_userplaycount()
            artist_name = now_playing.artist.get_name()
            artist_play_count = now_playing.artist.get_userplaycount()
            
            album_name = now_playing.get_album().title
            album_play_count = now_playing.get_album().get_userplaycount()
            
            album_cover_image = now_playing.get_album().get_cover_image(pylast.SIZE_EXTRA_LARGE)               


            track_url = f'https://www.last.fm/music/{artist_name.replace(" ", "+")}/_/{track_title.replace(" ", "+")}'
            artist_url = f'https://www.last.fm/music/{artist_name.replace(" ", "+")}'
            album_url = f'https://www.last.fm/music/{album_name.replace(" ", "+")}'
            user_url = f'https://www.last.fm/user/{username.replace(" ", "+")}'
            total_play_count = user_lastfm.get_playcount()

            embed = discord.Embed(title=f"last.fm - {username} | {total_play_count} total plays ", color=16776958,
                description=f"**artist plays**: {artist_play_count}\n**album plays**: {album_play_count} ")
            embed.set_author(name=user.name, icon_url=user.avatar, url=user_url)
            embed.add_field(name=f'playing', value=f'[{track_title}]({track_url})\n**{track_play_count} plays**')
            embed.add_field(name=f'album', value=f'[{album_name}]({album_url})\n{album_play_count} plays')
            embed.add_field(name=f'artist', value=f'[{artist_name}]({artist_url})\n{artist_play_count} plays')
            embed.set_thumbnail(url=album_cover_image)
            await ctx.send(embed=embed)
        else:
            await ctx.send("You are not currently playing anything.")
    else:
        await ctx.send(embed=discord.Embed(description=f'<:whitealert:1146101911297208471> {ctx.author.mention} - {user.name} has not **set** their __Last.fm__ profile yet. Use `{ctx.prefix}set`', color=16776958))

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 17 (6 by maintainers)

Most upvoted comments

Ah, right, if we add a username like &username=bbc6music to the end of the URLs in https://github.com/pylast/pylast/issues/441#issuecomment-1841733393 we do get userplaycount:

<stats>
<listeners>1623190</listeners>
<playcount>25614479</playcount>
<userplaycount>28</userplaycount>
</stats>

With pylast, you can set a username setting up the network object:

network = pylast.LastFMNetwork(api_key=LASTFM_API_KEY, api_secret=LASTFM_API_SECRET, username= username)

Also, you may need to set a username on the artist or album objects.

For example, instead of:

artist_play_count = now_playing.artist.get_userplaycount()

Check if there’s a username set on the artist object:

artist = now_playing.artist
print(f"{artist=}")
artist_play_count = artist.get_userplaycount()

And you can set it like this:

artist = now_playing.artist
artist.username = username
print(f"{artist=}")
artist_play_count = artist.get_userplaycount()

Does that help?

It might be an idea to allow passing a username to get_userplaycount(), but would you like to try the above first?