pythonapispotifylast.fmpylast

Is there a way to use pylast to get the top tracks?


I need to use the pylast module and last.fm api to get the top tracks (https://www.last.fm/api/show/chart.getTopTracks) but I can't find how to do this in python.

API_KEY = "my key"
API_SECRET = "my secret"

network = pylast.LastFMNetwork(api_key = API_KEY)

print(network.chart_get_top_tracks())

But the chart_get_top_tracks() method doesn't exist. how do I use this? If it helps, my end result will hopefully be a live Spotify playlist of the top 100 songs. It will hopefully update every 10 mins or so. I've got the playlist_add_items() working with Spotify and now all I need is to find a way to get the top 100 and use them both together.

I tried pylast.LastFMNetwork(api_key = API_KEY).network.chart_get_top_tracks() and I was hoping for it to return 50 top tracks but this method is not the real one. Am I doing something completely wrong or do I just not know the method name?


Solution

  • You're looking for network.get_top_tracks(), not network.chart_get_top_tracks():

    import pylast
    
    API_KEY = "TODO"
    
    network = pylast.LastFMNetwork(api_key=API_KEY)
    tracks = network.get_top_tracks()
    
    for track in tracks[:10]:
        print(track.item)
    

    Outputs:

    Taylor Swift - Anti-Hero
    Drake - Rich Flex
    Taylor Swift - Lavender Haze
    Taylor Swift - Snow on the Beach (feat. Lana Del Rey)
    Taylor Swift - Midnight Rain
    Taylor Swift - Karma
    Taylor Swift - Maroon
    Taylor Swift - You're On Your Own, Kid
    Steve Lacy - Bad Habit
    Taylor Swift - Bejeweled