python wordle. Fun with pylast


That’s sort of a visualization of the top tracks I’ve listened to and reported back to I’ve been wanting to do more research in the webservices area in anticipation of doing a larger collective intelligence type project, so when my friend Mark mentioned this python script called pylast I thought I’d try it out.

The pylast code is nicely done, but I didn’t see much in the way of documentation, so it took a little while to get this going, hopefully the links below will save you some time if you’re starting out hacking’s API with Python.

pylast module source
pylast installation
pylast documentation (from PyDoc, e.g. pydoc -w pylast pylast.html)

A few things you’ll need (in addition to a account with some data) API Account you’ll need you API Key, Secret Key, and password API documentation

Here is the source code. to extract your top tracks from (also below)

My top tracks overall
The wordle-ready file input file
Create your own Wordle

Please let me know how you’d improve this or if you have suggestions, links to / for cool things to do with’s API.


import time
import pylast
import re

from md5 import md5

user_name = 'mitsmb'
user_password = 'password_you_login_to_lastfm_with'
top_tracks_file = open('top_tracks_wordle.txt', 'w')

# to make the output more interesting for wordle viz. 
# run against all periods. if you just want one period, 
# delete the others from this list
# time_periods = ['PERIOD_OVERALL']
## shouldn't have to edit anything below here
md5_user_password = md5(user_password).hexdigest()
sg = pylast.SessionKeyGenerator(api_key, api_secret)
session_key = sg.get_session_key(user_name, md5_user_password)

user = pylast.User(user_name, api_key, api_secret, session_key)
top_tracks = []
for time_period in time_periods:
    # by default pylast returns a seq in the format:
    #  "Item: Andrew Bird - Fake Palindromes, Weight: 33"
    tracks = user.get_top_tracks(period=time_period)

    # regex that tries to pull out only the track name (
    # for the ex. above "Fake Palindromes"
    p = re.compile('.*[\s]-[\s](.*), Weight: [\d]+')

    for track in tracks:
        m = p.match(str(track))
        track = m.groups()[0]
    # be nice to's servers
top_tracks = "\n".join(top_tracks)


One thought on “ python wordle. Fun with pylast

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s