Archive for the ‘Uncategorized’ Category

Uncategorized Obtaining Last.fm data

0 Comments

An enterprising student decided to use Processing to visualize her Last.fm listening history. To make that happen, she can use the Last.fm API. One way to do so is to use the Python binding for this API, developed by Amr Hassan. I obtained it by saying

 svn checkout http://pylast.googlecode.com/svn/ 

at a terminal prompt. Then I said

cd svn/trunk
python setup.py install

and put the following into a file called recent01

import pylast
API_KEY = '12345'
print API_KEY
API_SECRET = '54321'
print API_SECRET
import atexit
import os
import readline
import rlcompleter
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)
if os.path.exists(historyPath):
    readline.read_history_file(historyPath)
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
username = raw_input("Please enter your username: ")
md5_password = pylast.md5(raw_input("Please enter your password: "))
session_key = pylast.SessionKeyGenerator(API_KEY, API_SECRET).get_session_key(username, md5_password)
print session_key

Bear in mind that I’ve previously joined Last.fm so I have a username. I registered at Last.fm to try the API and, in the course of doing so, I received an API key and an API secret. I copied this locally and pasted them into the above script in place of the numbers 12345 and 54321.

One annoyance was that I did not know much about Python, such as how to save and reload commands given interactively. The above lines from atexit to del os … serve to save history and let me copy it to another file between Python sessions. There are undoubtedly better ways. The lines after that serve to get the Session key, the third item we’ll need to get our recently played tracks. I ran this script by saying

execfile('recent01')

at a Python prompt. I then cut and pasted the Session key into the following script.

import pylast
API_KEY = '12345'
print API_KEY
API_SECRET = '54321'
print API_SECRET
session_key = 'abcde'
print session_key
import atexit
import os
import readline
import rlcompleter
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)
if os.path.exists(historyPath):
    readline.read_history_file(historyPath)
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
curruser = pylast.User('mickmcq', API_KEY, API_SECRET, session_key)
rtracks = pylast.User.get_recent_tracks(curruser)
print rtracks

As before, the part from import atexit to the line beginning del os, is just a quick and dirty way to get a command history. The payload of this script is rtracks, a listing of recently played tracks, along with the time they were played. Obviously, you need to substitute your user name in the line beginning curruser. The contents of rtracks looks as follows when you print it. You can format it, but this is what I got by default:

[Cyanotic - Transhuman played at 8 Mar 2009, 00:18, My
Life with the Thrill Kill Kult - Hottest Party in Town
played at 8 Mar 2009, 00:16, Hanzel und Gretyl - Fukken
Uber Death Party played at 8 Mar 2009, 00:11, Flesh
Field - This Broken Dream played at 8 Mar 2009,
00:04]