Last.fm Firefox Extension
About

I have been using the Last.fm Firefox Extension to submit what I listen to on Pandora to Last.fm. Because Flash in Firefox on Linux can be a bit flaky, I wrote a script to display the log file so I could be sure that what I was listening to was being submitted to Last.fm. I also made the script parse the log file for the song that I most recently listened to. This could then be used as a status message in an instant messaging client like Pidgin.

The Script

This script finds the first log file for the Last.fm Firefox Extension. It then either outputs the contents of the log file if passed "log" as the first parameter. Otherwise, it simply outputs the last played song.

#! /bin/bash

# Set the separator to newlines
IFS='
'

# Location of Firefox profiles
FIREFOX_DIR=$HOME/.mozilla/firefox

DIRS=$( ls $FIREFOX_DIR )

# Use the first Last.fm Firefox Extension log file that we find
for DIR in $DIRS; do
    if [ -f $FIREFOX_DIR/$DIR/lastfm/lastfm.log ]; then
        LOG=$FIREFOX_DIR/$DIR/lastfm/lastfm.log
        break
    fi
done

# If no parameters passed, then just output the last song played. Otherwise,
# show the log file.
if [ -n "$LOG" ]; then
    if [ "$1" = "log" ]; then
        tail -f $LOG
    else
        grep "Now Playing data" $LOG | tail -n 1 | \
            awk -F 'Now Playing data for the ' '{print $2}' | \
            sed 's/has been sent to .*//'
    fi
fi