Last.fm python wordle. Fun with pylast

mitsmb-lastfm-wordle

That’s sort of a visualization of the top tracks I’ve listened to and reported back to Last.fm. 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 Last.fm’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 Last.fm account with some data)
Last.fm API Account you’ll need you API Key, Secret Key, and Last.fm password
Last.fm API documentation

Here is the source code. to extract your top tracks from last.fm. (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 Last.fm’s API.


#!/usr/bin/python

import time
import pylast
import re

from md5 import md5

user_name = 'mitsmb'
user_password = 'password_you_login_to_lastfm_with'
api_key = 'YOUR_LASTFM_API_KEY_GOES_HERE'
api_secret = 'YOUR_LASTFM_SECRET_GOES_HERE'
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_12MONTHS', 'PERIOD_6MONTHS', 'PERIOD_3MONTHS', 'PERIOD_OVERALL']
# 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]
        top_tracks.append(track)
    # be nice to last.fm's servers
    time.sleep(5)
    
top_tracks = "\n".join(top_tracks)
top_tracks_file.write(top_tracks)
top_tracks_file.close()

Advertisements

One thought on “Last.fm python wordle. Fun with pylast

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s