yet another script for song change event

Forums:

Having testet the script for long time, I've added some lines to make configuration a little bit easier and here it is. The script should work with any version. It does not poll for song changes, but is cacluating the end of the current played song.

Following things are already in the script: submitting to lastfm, status changes to gaim, setting xroot with cover, updating ~/.np:

------------------------------------------------------------------------------------------------------------------
#!/bin/bash -x

lasttitle=""
lastalbum=""

# Command to display cover
displayCover=1
updateNowPlaying=0
updateGaim=1
updateLastFm=1
mocIsOnFingerPort=1

LOGFILE=~/.np

if (( $displayCover )); then
XSETROOT="gconftool-2 -t str -s /desktop/gnome/background/picture_filename"
fi

# finger stream tcp nowait.1000 mocp /home/mocp/mocp_as_finger.sh mocp_as_finger.sh

# This is the mocp_as_finger.sh:
# #!/bin/sh
# export HOME=/home/mocp
# /usr/bin/mocp -i

while [[ 1 ]]; do

if (( $updateNowPlaying )); then
lastline=`tail -1 $LOGFILE`
fi

SongTitle=""
Artist=""
Album=""
actualtime=""
currenttime=""
starttime=""
timeleft=""
State=""

if (( $mocIsOnFingerPort )); then
eval $(finger @localhost|perl -n -e 'print if s/(^.*?): (.*$)/$1="$2"/;'|perl -p -e "s/\`\'\"/ /;")
else
eval $(mocp -i|perl -p -e 's/(^.*?): (.*$)/$1="$2"/;'|perl -p -e "s/\`\'\"/ /;")
fi

if [[ "$State" != "PLAY" ]]; then
if [[ "$idleset" == "false" ]]; then
gaim-remote "setstatus?status=available&message="
idleset=true
fi
sleep 30;
continue;
fi

idleset=false

newline="$SongTitle - $Artist - $Album"

if [[ "$SongTitle" = "" ]]; then
newline="$Title"
fi

if [[ "$newline" = "" ]]; then
newline="$File"
fi

# Nothing is played at the moment, so we'll try again later
if [[ "$newline" = "" ]]; then
sleep 5;
continue;
fi

let actualtime=`date +%s`%86400+`date +%:z|cut -d":" -f1`*3600

let currenttime=`echo $CurrentTime|cut -s -d":" -f1,2 --output-delimiter="*60+"`
tlMinutes=`echo $TimeLeft|cut -s -d":" -f1|sed -e 's/^0//'`
tlSeconds=`echo $TimeLeft|cut -s -d":" -f2|sed -e 's/^0//'`

let timeleft=tlMinutes*60+tlSeconds

let starttime=actualtime-currenttime

let hours=starttime/3600
let minutes=starttime%3600/60
let seconds=starttime%60

start=`printf "%02d:%02d:%02d" $hours $minutes $seconds`

let timeleft=timeleft+5 # Wait for pause is over

# Something wrong, but don't know exactly what
if (($timeleft<=0)); then
timeleft=20
fi

if [[ $updateNowPlaying && "$lastline" != "$newline" ]];then
# xrootconsole can't handle utf-8, so we convert
newline_latin=`echo $newline |recode utf8..latin1`
echo $newline_latin >>$LOGFILE
fi

newtitle="$SongTitle - $Artist"
if [[ "$lasttitle" != "$newtitle" ]]; then

if (( $updateLastFm )); then
/usr/lib/lastfmsubmitd/lastfmsubmit --artist "$Artist" \
--album "$Album" --title "$SongTitle" --length $TotalSec
fi

if (( $updateGaim )); then
message=`echo $newtitle|sed -e "s/&/And/"`
status=`gaim-remote getstatus`
gaim-remote "setstatus?status=$status&message=♫ $message"
fi

lasttitle=$newtitle

if (( $displayCover )); then
if [[ "$lastalbum" != "$Album" ]]; then
olddir=`pwd`
newdir=`dirname "$File"`
cd "$newdir"
cover=`ls *.jpg|head -n1`
if [[ "$cover" != "" ]]; then
$XSETROOT "`pwd`/$cover"
fi
cd "$olddir"
lastalbum=$Album
fi
fi
fi

sleep $timeleft

done

# An idea for using ~/.np for desktops where xrootconsole is still working:
# put into: ~/.xsession, or similar:
# xrootconsole -geometry 132x1+135-35 -fn -adobe-courier-bold-r-normal-*-*-140-75-75-m-*-iso8859-15 ~/.np &

You should set the mocIsOnFingerPort to 0 for first:

mocIsOnFingerPort=0

What's about the finger thing:

I've installed mocp player on the finger port, see comments in the script for how to do that. So everybody in the room is able look what music is playing at them moment, or would even be able to use the script over the network.

To get this working I created a user account which name is e.g. mocp:

mocp:x:1000:1000:,,,:/home/mocp:/bin/bash

This users ~/.bash_profile looks like this:

--------------------------------------------------------------------------------
# ~/.bash_profile: executed by bash(1) for login shells.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# include .bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

exec /usr/bin/mocp
--------------------------------------------------------------------------------

I for myself has to start the mocp with "su" or "su -" or could do "ssh mocp@localhost" and so on.

Sorry for the typos, realized to late that's possible the edit the last comment, but just the last.

Sorry for the late response and thanks for the script!

Can you send me it as an attachment (daper at daper dot net)? I suspect something can be broken by pasting. I want to put the script in the contrib section.

--
Damian Pietras - MOC developer