LastFM Support

Discussion about my Wifi Radio project at http://mightyohm.com/wifiradio/ or my WL-520gU talk at NOTACON.
Post Reply
iggyCeltic
Posts: 6
Joined: Sun Nov 06, 2011 7:45 pm

LastFM Support

Post by iggyCeltic »

Using the WebService::LastFM CPAN module I have been able to get URLs that working with mpd to stream last.fm playlists. Issue I am running into is that I have to copy the next track url into mpc after every song. Tried to PAR package the perl script that I have to play on my laptop, but the space requirements are about double what is left on the router. I believe that it is possible to get the Last.FM 1.2 api working from a shell script, which should require less space than installing a full perl installation. Has anyone been able to get this to work? Willing to attempt to break down the LastFM.pm module for what web services need to be hit, but don't want to do the work if it has already been done.


Have some working code below. You will need to change the username, password and station variables. Other than that it works like a charm. No issues with this since I got it working a couple days ago.

Code: Select all

#!/bin/ash
SESSIONFILE=/tmp/session.xml;
PLAYLISTFILE=/tmp/playlist.xml;
username=${USERNAME};
password=${MD5PASSWORDHASH}; 
url="http://ws.audioscrobbler.com/radio/handshake.php?username=${username}&version=1.1.1&platform=linux&passwordmd5=${password}";
wget -O ${SESSIONFILE} ${url};
status=`grep FAILED ${SESSIONFILE}`;
if [ "${status}" != "" ]
then
  echo "Authentication Failed\n";
  exit 0;
fi
station=${STATION};
session=`grep session ${SESSIONFILE} | cut -d= -f2`;
url="http://ws.audioscrobbler.com/radio/adjust.php?session=${session}&url=globaltags/${station}";
wget -O /dev/null ${url};
while [ 1 ]
do
  url="http://ws.audioscrobbler.com/radio/xspf.php?sk=${session}&discovery=0&desktop=0";
  wget -O ${PLAYLISTFILE} ${url};
  for tag in location title album creator duration
  do
    OUT=`grep "<${tag}>.*</${tag}>" ${PLAYLISTFILE} | sed -e "s/^.*<${tag}/<${tag}/" | cut -d">" -f2 | cut -d"<" -f1 | sed -e "s/^.*$/\{&\}/" | sed -e "s/ /+/g" | sed -e "s/'//g" | sed -e "s/\&//g"`;
    eval ${tag}='$(echo ${OUT})';
  done
  
  i=1
  while [ ${i} -lt 6 ] 
  do
    j=$(expr ${i} + 1);
    curLoc=$(echo "${location}" | cut -d'}' -f${i} | cut -d'{' -f2);
    curDur=$(echo "${duration}" | cut -d'}' -f${i} | cut -d'{' -f2);
    curTitle=$(echo "${title}" | cut -d'}' -f${j} | cut -d'{' -f2);
    curArt=$(echo "${creator}" | cut -d'}' -f${j} | cut -d'{' -f2);
    mpc del 1;
    mpc add ${curLoc};
    mpc play;
    dur=$(echo ${curDur} | sed 's/...$//');
    echo "Title: ${curTitle}  Artist: ${curArt} Time: ${curDur} Duration: ${dur}";
    k=0;
    gr=1;
    while [ ${gr} -eq 1 ] 
    do
      sleep 1;
      gr=`mpc | grep playing | wc -l`;
      k=$(expr ${k} + 1);
    done
    i=$(expr ${i} + 1);
  done
done
exit;
done
Last edited by iggyCeltic on Thu Nov 10, 2011 4:11 pm, edited 2 times in total.
User avatar
gerben
Posts: 164
Joined: Sat Oct 16, 2010 8:41 am
Location: Netherlands

Re: LastFM Support

Post by gerben »

You could run the code on a webserver and have the router only wget the current/next song. The shell script only has to update the playlist if the song in the wget changed, or something like that. Just a thought.
iggyCeltic
Posts: 6
Joined: Sun Nov 06, 2011 7:45 pm

Re: LastFM Support

Post by iggyCeltic »

If I can't get anything else working, that might be the route that I have to take. But currently want to try to get everything self contained on one machine. Especially since I know that once I get it done I will have someone request that I build one for them and don't want to force someone else to have a webserver just to get the radio to work.

Thank you for the suggestion though.

Edit:
Think I have a working solution just under the space requirements that I had for myself. Once I can test it I will post the result here if successful.
iggyCeltic
Posts: 6
Joined: Sun Nov 06, 2011 7:45 pm

Re: LastFM Support

Post by iggyCeltic »

Got the following working with bash not on the router. Looks like there is something that I am forgetting between switching from bash to ash. Any tips?


Fixed that issue.

Slight issue with calculating song durations (Last.fm duration tag isn't correct). Once I get that issue fixed will post the result here
iggyCeltic
Posts: 6
Joined: Sun Nov 06, 2011 7:45 pm

Re: LastFM Support

Post by iggyCeltic »

First post updated with working code. If you use it let me know if you had to make any tweaks to it or any suggestions for updates.

Currently I am looking for a way to be able to change the station on the fly. Also need it to scrobble the actual plays to last.fm. Figured I would share the success so far though.
User avatar
gerben
Posts: 164
Joined: Sat Oct 16, 2010 8:41 am
Location: Netherlands

Re: LastFM Support

Post by gerben »

That is some good looking code. Thanks for sharing. I'm not using LastFM, but who knows.
Post Reply