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()

Randomizing a list of 25 things using Python

I got tagged in the oh-so popular “25 Random Things” meme on Facebook. To make a point that I sometimes think in code and I don’t think its weird, I wrote the script below to randomize my list before I posted it.


import random

f = open('25-things.txt')
list = f.readlines()
"""Magnus L Hetland's solution http://bit.ly/uN4iq"""
result = []

for i in range(len(list)):
    element = random.choice(list)
    list.remove(element)
    result.append(element)

i = 1
for line in result:
    print "%s. %s" % (i, line)
    i += 1

Save PDF Web Receipts Folder – Change Default – 10.5

This hint provides information on modifying the OS X “Save PDF Web Receipts Folder” work-flow that’s attached to the print dialog, but it is for 10.4 only.

I never like having a “Web Receipts” folder at the top of my “~/Documents”, I’d prefer they we kept in a sub-folder, e.g. (“~/Documents/Personal/Web \Receipts/”). Well, the above hint was about something different that doesn’t happen in 10.5 but the comment by “Mechcozmo” provided the necessary clue.

If you want change the default location for storing “Web Receipts”, open the file “tools” in this location:
/Library/PDF Services/Save PDF to Web Receipts Folder.pdfworkflow/Contents and change the line 25 from:

destDirectory = os.path.expanduser("~/Documents/Web Receipts/")

to whatever you prefer i.e.:

destDirectory = os.path.expanduser("~/Documents/Personal/Web Receipts/")