Continuing my Roku Soundbridge Radio mt-daapd project, I decided I wanted to add audioscrobbler support to submit plays to my Last.fm profile. With some help from a thread on the Firefly forums I've been able to achieve this using lastfmsubmitd and a fairly simple script. Here's how to do it if you're using Ubuntu (Hardy Heron).Everything apart from the installation of lastfmsubmitd should be the same with most other distros.
Firstly install lastfmsubmitd:
sudo apt-get install lastfmsubmitd
You'll be presented with a wizard which will automatically configure your /etc/lastfmsubmitd.conf file. It will ask for your Last.fm login credentials.
Next create ~/lastfmsubmit.sh:
#!/bin/bash
# fetch newly played songs from fireflydb and write
# into lastfmsubmitd readable format
# config
SQLITE=sqlite3
DATABASE=/usr/var/cache/mt-daapd/songs3.db
LASTFILE=./lastfmsubmit.date
DBLSFILE=./lastfmsubmit.ls
# get last run time
if [ -e $LASTFILE ]
then
. $LASTFILE
else
LASTRUN=0
fi
# get last database file date
if [ -e $DBLSFILE ]
then
. $DBLSFILE
else
DBLSRUN=
fi
# exit when database file unchanged
DBLSNOW=`ls -l $DATABASE`
if [ "$DBLSRUN" == "$DBLSNOW" ]
then
exit
fi
# log file date
echo "DBLSRUN=\"$DBLSNOW\"" > $DBLSFILE
# query database
OUTFILE=$(mktemp /tmp/mt-daapd-XXXXXXXX)
$SQLITE $DATABASE 'SELECT artist,album,title,track,song_length,time_played FROM songs where time_played > '$LASTRUN' ORDER BY time_played ASC;' | gawk -F '|' '{ printf "---\nartist: \"%s\"\nalbum: \"%s\"\ntitle: \"%s\"\ntrack: %s\nlength: %d\ntime: !timestamp %s\n",$1,$2,$3,$4,$5/1000,strftime("%Y-%m-%d %T",$6) }' > $OUTFILE
# Move the file to the daemon spool directory if it is not empty
if [ -s $OUTFILE ]
then
mv $OUTFILE /var/spool/lastfm
fi
# log query date
echo "LASTRUN="`date +%s` > $LASTFILE
# make lastfmsubmitd files readable
chmod 664 /var/spool/lastfm/*
You may need to change the SQLITE and DATABASE properties. This script will query your mt-daapd database and retrieve a list of songs that have been played since the last time the script was run. The results are formatted according to the expectations of lastfmsubmitd and then droped into the spool directory ready for submission. The script is largely based on the script in this thread with some minor modifications for my system and to fix a couple of issues.
Make sure the script is executable:
chmod +x lastfmsubmit.sh
At this point you should call the script manually to check it works. Make sure you listen to something first and then check the /var/spool/lastfm directory to make sure the file has been created.Once lastfmsubmitd has processed the file it should disappear and the play(s) should appear on your Last.fm profile.
If the file isn't being created then try running the SQL query by hand to make sure you get some results.
If the file is being created but doesn't seem to get submitted then check /var/log/lastfm/lastfm.log to see if there are any errors. On one occasion the audioscrobber service seemed to be rejecting my submissions for no known reason. If this happens then lastfmsubmitd should keep trying at increasingly long intervals, so if the audioscrobbler service or your connection are temporarily down you shouldn't lose any plays.
Now we need to automate the script. Create a cron entry using 'crontab -e' as below:
*/5 * * * * ./lastfmsubmit.sh
This will call the script every 5 minutes, which is a suitable interval if you want your plays to appear on your profile in close to real time.Feel free to change this to suit your own listening habits. Listen to something and then wait for the next 5 minute interval to see if it works. If you have any problems run through the previous checks and also look at your user mail in case the cron task failed to execute.
The only drawback to this approach is that lastfmsubmitd only supports (as far as I know) a single configuration. If you're running multiple mt-daapd processes it's not possible to submit them to different profiles. If anyone finds a way around this then please let me know.
Posts RSS 2.0