Forum » Development Discussion

Bashfm: a collection of shell scripts to play and control your last.fm radio

 
  • Bashfm: a collection of shell scripts to play and control your last.fm radio

    Hi everybody,

    I have written some BASH shell scripts that can be used to play Last.fm radio. I wrote it using the information kindly provided in someone's blog: I use it under Cygwin to be able to use Winamp for listening to Last.fm. Other media players can be used. Of course, it also works under Linux. For communicating with the Last.fm server it uses Lynx, the text-based web browser, so you have to make sure that this is installed. I dubbed this collection of shell scripts "bashfm". It is so small, that I will simply post the source code here. First of all, bashfm needs a .bashfm file in your home directory in which the username, password and location of the media player are stored. The .bashfm file needs to be edited by the user. It looks like this:

    username=gandalf
    password=rivendell07
    mediaplayer=C:/Program Files/Winamp/winamp.exe

    ------------------------------------------------
    Here follows the fmstart script:
    ------------------------------------------------

    #!/usr/bin/bash

    # "fmstart" starts playing last.fm radio. "fmstart"
    # should be used before any other bashfm commands are
    # given. It obtains the username, password, and
    # mediaplayer from the ".bashfm" file. If the password
    # present in the file as plain text, it will be md5
    # encoded and the plain text version will be removed.
    # It then logs in into Last.fm, obtains the audio
    # stream URL, and launches the media player. "fmstart"
    # takes no command arguments.

    username=`cat ~/.bashfm | grep "^username="`
    username=`expr "$username" : 'username=\(.*\)'`

    password=`cat ~/.bashfm | grep "^password="`
    password=`expr "$password" : 'password=\(.*\)'`
    password=`echo -n "$password" | sed -e 's/[ ]*$//'`

    mediaplayer=`cat ~/.bashfm | grep "^mediaplayer="`
    mediaplayer=`expr "$mediaplayer" : 'mediaplayer=\(.*\)'`
    mediaplayer=`echo -n "$mediaplayer" | tr '\' '/'`

    if [ ! -z "$password" ]
    then
    passwordmd5=`echo -n $password | md5sum`
    passwordmd5=`expr "$passwordmd5" : '\([0-9A-Fa-f]*\)\.*'`
    else
    passwordmd5=`cat ~/.bashfm | grep "^passwordmd5="`
    passwordmd5=`expr "$passwordmd5" : 'passwordmd5=\(.*\)'`
    fi

    echo 'username='$username > ~/.bashfm
    echo 'password=' >> ~/.bashfm
    echo 'mediaplayer='"$mediaplayer" >> ~/.bashfm

    echo 'passwordmd5='$passwordmd5 >> ~/.bashfm

    login_url='http://ws.audioscrobbler.com/radio/handshake.php?'
    login_url="$login_url"'version=1.1.1&platform=linux&username='
    login_url="$login_url"$username'&passwordmd5='$passwordmd5
    login_url="$login_url"'&debug=0&partner='

    echo "login_url=$login_url" >> ~/.bashfm

    lynx -source "$login_url" >> ~/.bashfm
    echo >> ~/.bashfm

    stream_url=`cat ~/.bashfm | grep "^stream_url="`
    stream_url=`expr "$stream_url" : 'stream_url=\(.*\)'`

    "$mediaplayer" "$stream_url" &

    ---------------------------------------------
    Here follows the "fmtag" script
    ---------------------------------------------

    #!/usr/bin/bash

    # "fmtag" plays the music corresponding to the tag given
    # as command argument. The tag needs to be quoted when
    # it contains spaces.
    #
    # Example:
    # fmtag 'trip hop'

    tag=`echo $1 | sed 's/ /%20/g'`

    session=`cat ~/.bashfm | grep "^session="`
    session=`expr "$session" : 'session=\(.*\)'`

    tuning_url='http://ws.audioscrobbler.com/radio/adjust.php?'
    tuning_url="$tuning_url"'session='$session'&url=lastfm://'
    tuning_url="$tuning_url"'globaltags/'$tag'&debug=0'

    lynx -source "$tuning_url" > /dev/null

    --------------------------------------------
    Here follows the "fmart" script.
    --------------------------------------------

    #!/usr/bin/bash

    # "fmart" plays the radio station corresponding to a
    # certain artist. It takes the artist's name as command
    # line argument. Use quotes when the artist's name
    # contains spaces.
    #
    # Example:
    # fmart 'faith no more'

    artist=`echo $1 | sed 's/ /%20/g'`

    session=`cat ~/.bashfm | grep "^session="`
    session=`expr "$session" : 'session=\(.*\)'`

    tuning_url='http://ws.audioscrobbler.com/radio/adjust.php?'
    tuning_url="$tuning_url"'session='$session'&url=lastfm://artist/'
    tuning_url="$tuning_url"$artist'/similarartists&debug=0'

    lynx -source "$tuning_url" > /dev/null

    -------------------------------------------------
    Here follows the "fmbor" script.
    -------------------------------------------------

    #!/usr/bin/bash

    # "fmbor" plays your neighbourhood radio. "fmbor" takes
    # no command line arguments.

    username=`cat ~/.bashfm | grep "^username="`
    username=`expr "$username" : 'username=\(.*\)'`

    session=`cat ~/.bashfm | grep "^session="`
    session=`expr "$session" : 'session=\(.*\)'`

    tuning_url='http://ws.audioscrobbler.com/radio/adjust.php?'
    tuning_url="$tuning_url"'session='$session'&url=lastfm://user/'
    tuning_url="$tuning_url"$username'/neighbours&debug=0'

    lynx -source "$tuning_url" > /dev/null

    ---------------------------------------
    Here follows the "fminfo" script
    ---------------------------------------

    #!/usr/bin/bash

    # "fminfo" returns the name of the artist, name
    # of the song and name of the album of the music
    # that is currently playing. "fminfo" takes no
    # command line arguments.

    session=`cat ~/.bashfm | grep "^session="`
    session=`expr "$session" : 'session=\(.*\)'`

    info_url='http://ws.audioscrobbler.com/radio/np.php?'
    info_url="$info_url"'session='$session'&debug=0'

    lynx -source "$info_url" | egrep '(^artist=|^track=|^album=)'

    ---------------------------------------
    Here follows the "fmskip" script
    ---------------------------------------

    #!/usr/bin/bash

    # "fmskip" skips the song that is currently playing.
    # It takes no command line arguments.

    session=`cat ~/.bashfm | grep "^session="`
    session=`expr "$session" : 'session=\(.*\)'`

    skip_url='http://ws.audioscrobbler.com/radio/control.php?'
    skip_url="$skip_url"'session='$session'&command=skip&debug=0'

    lynx -source "$skip_url"
    echo

    -----------------------------------------
    Here follows the "fmlove" script
    -----------------------------------------

    #!/usr/bin/bash

    # "fmlove" adds the song that is currently playing to
    # the list of loved songs. "fmlove" takes no command
    # line arguments.

    session=`cat ~/.bashfm | grep "^session="`
    session=`expr "$session" : 'session=\(.*\)'`

    love_url='http://ws.audioscrobbler.com/radio/control.php?'
    love_url="$love_url"'session='$session'&command=love&debug=0'

    lynx -source "$love_url"
    echo

    --------------------------------------------
    Here follows the "fmban" script.
    --------------------------------------------

    #!/usr/bin/bash

    # "fmban" bans the current song from your radio. "fmban"
    # takes no command line arguments.

    session=`cat ~/.bashfm | grep "^session="`
    session=`expr "$session" : 'session=\(.*\)'`

    ban_url='http://ws.audioscrobbler.com/radio/control.php?'
    ban_url="$ban_url"'session='$session'&command=ban&debug=0'

    lynx -source "$ban_url"
    echo

  • Alternatives for "lynx"

    When "lynx" is not installed on your system, one can try the "curl" or "wget" utilities instead (provided that one of these is available). Simply replace all occurences of:

    lynx -source

    with:

    wget --quiet --output-document=-

    or:

    curl --silent

    Cheers,
    Illustrissime

    p.s. There is a superficial similarity between these scripts and the shell.fm project. However, the two projects are entirely unrelated. I would not even dare to call this a "project"; it is a hack, but a useful hack, at least for me ;-)

Anonyma användare kan inte skriva inlägg. Vänligen logga in eller skapa ett konto för att göra inlägg i forumen.