↑ zurück

mp3blstr2dscrbblr.sh

Beschreibung

Ein Scrobble-Script für audioscrobbler/last.fm. Das Script mp3blstr2dscrbblr.sh (mp3blaster to audioscrobbler) analysiert die Statusdatei von mp3blaster und übergibt die entsprechenden Informationen einem Perl-Script.

Da mp3blaster keine native Pluginschnittstelle besitzt, müssen die benötigten Informationen über die Statusdatei (mp3blaster -f <Datei>) gewonnen werden. Dazu führt Cron alle 30 Sekunden dieses Script aus. Das Script prüft anschließend, ob das gerade gespielte Lied mindestens 30 Sekunden läuft (damit der User genügend Zeit hat, es zu skippen) sowie maximal 60 Sekunden, damit es nicht mehrmals eingeschickt wird (diese Überprüfung kann durch den Parameter --force umgangen werden).

Benötigt

Code

#!/bin/sh
 
# version: 0.3
 
# mp3blaster status file (mp3blaster -f <file>)
MP3="/tmp/mp3blaster"
# perl audioscrobbler plugin directory
# (http://search.cpan.org/~roam/Audio-Scrobbler-0.01/lib/Audio/Scrobbler.pm)
#DIR="/home/scytheman/Audio-Scrobbler-0.01/lib"
# plugin binary/script
#BIN="../bin/scrobbler-helper"
BIN="scrobbler-helper"
 
# maximum retries if first submission failed
MAX_RETRIES=10
# seconds to wait before second submission
# this value increases during further retries
WAIT=10
 
# check for existing status file
if ! test -e $MP3
then
	echo "$MP3 doesn't exist"
	exit
fi
 
# first, check if the time of last modification is greather than 30s and less than 60s
# because this script will be executed every 30s, so we don't submit the same song twice
# and make sure we don't submit skipped tracks (user has 30s to skip the track)
# 
# we calculate the difference between last modification time (-> stat) and current time
# (-> date), both are seconds since 1970-01-01 00:00:00 UTC
 
# seconds since last modification
SSLM=$(echo "$(date "+%s") - $(stat -c "%Y" $MP3)" | bc)
if [ "$SSLM" -lt 30 ] || [ "$SSLM" -gt 59 ]
then
	echo "last modification time <30 or >60"
 
	# user may skip this test by passing '--force' to script
	if [ "$1" = "--force" ]
	then
		echo "ignoring."
	else
		exit
	fi
fi
 
# check if song is playing otherwise there is the risc of submitting a song twice
# because every time a song is paused, mp3blaster updates it's status file, thus
# changing also time of last modification
if ! grep -q "^status playing" $MP3
then
	echo "no song playing"
	exit
fi
 
# we need 2 + 7 arguments:
#  -P <client> -V <clientversion>
#  title, artist, album, year, comment, genre, length
# note:	- comment will be left empty
#	- genre will be specified globally
 
# unfortunately mp3blaster isn't recognized
CLIENT="mpd"
CLIVER="0.11"
 
# mp3blaster doesn't submit genre
GENRE="metal"
 
# now get all information from mp3blaster
title=$(grep "^title " $MP3 | cut -d ' ' -f2-)
artist=$(grep "^artist " $MP3 | cut -d ' ' -f2-)
album=$(grep "^album " $MP3 | cut -d ' ' -f2-)
year=$(grep "^year " $MP3 | cut -d ' ' -f2-)
length=$(grep "^length " $MP3 | cut -d ' ' -f2-)
 
# check if we have artist and title
# else print error message to stderr
if [ "$title" = "" ] || [ "$artist" = "" ]
then
	echo "error: $(grep "^path " $MP3 | cut -d ' ' -f2-) has no/invalid ID tag" 1>&2
	exit
fi
 
# and finally execute plugin
if ! [ "$DIR" == "" ]
then
	cd "$DIR"
fi
 
# creating alias and variable for multiple use
alias SUBMIT="$BIN -P $CLIENT -V $CLIVER \"$title\" \"$artist\" \"$album\" \"$year\" \"\" \"$GENRE\" \"$length\""
      SUBMIT="$BIN -P $CLIENT -V $CLIVER \"$title\" \"$artist\" \"$album\" \"$year\" \"\" \"$GENRE\" \"$length\""
 
echo
echo "executing $SUBMIT"
echo
SUBMIT
 
retval="$?"
# return values:
#    9 ?
#   22 bad hostname
#  114 couldn't complete handshake
#  115 connection timeout
#  255 couldn't complete handshake
 
# last submission failed, resubmit
if [ "$retval" == "9" ] || [ "$retval" == "22" ] || [ "$retval" == "114" ] || [ "$retval" == "115" ] || [ "$retval" == "255" ]
then
    for ((i = 1; i <= MAX_RETRIES; i++))
    do             
        sleep $WAIT
        echo "retry #$i: executing $SUBMIT"
        SUBMIT
 
        if [ "$?" == "0" ]
        then
            exit 0
        fi
 
        # doubling next wait, server may be down for any length of time
        let "WAIT = $WAIT * 2"
    done
    echo "couldn't submit song after $MAX_RETRIES retries: $?" 1>&2
elif [ "$retval" != "0" ]
then
 echo "return value: $retval" 1>&2
fi

Notwendige Anpassungen

  • Variable MP3 ändern, falls der Pfad zur Statusdatei anders lautet
  • DIR setzen, und BIN ändern, falls das Script von der Website genutzt wird (und nicht das Debian-Paket)
  • GENRE ändern, falls sich der Musikgeschmack von meinem unterscheidet :)
  • ~/.scrobbler-helper.conf erstellen, Inhalt wie folgt:
# ~/.scrobbler_helper.conf example file
# see `man scrobbler-helper' for detailed
# information
[global]
username=last.fm-user
password=password
# Optional (the default is UTF-8)
default_encoding=iso885915
# Optional (the default is "no")
fix_track_name=yes

Nutzung

Da dieses Script kein echtes Plugin darstellt und damit nicht von mp3blaster selber ausgeführt wird, muss es per Cron (siehe man 5 crontab) gestartet werden. Man führt als normaler Benutzer crontab -e aus und fügt die folgenden Zeilen hinzu:

# m h dom mon dow command
# cron runs only every minute, thus we need sleep here
*    *  *  *  *   nice -19 ~/scripts/mp3blstr2dscrbblr.sh > /dev/null & sleep 30s && nice -19 ~/scripts/mp3blstr2dscrbblr.sh > /dev/null
#

oder aber

# m h dom mon dow command
# cron runs only every minute, thus we need sleep here
*    *  *  *  *                nice -19 ~/scripts/mp3blstr2dscrbblr.sh > /dev/null
*    *  *  *  *   sleep 30s && nice -19 ~/scripts/mp3blstr2dscrbblr.sh > /dev/null
#

Lizenz

Dieses Script ist unter der GPL 3.0 lizensiert.

http://www.gnu.org/licenses/gpl-3.0.html

Changelog

v0.3 2007-09-25

  • retry specified number of times if submission fails

v0.2 2006-11-06

  • support for debian package libaudio-scrobbler-perl

v0.1 2006-06-16

  • initial release
scytheman/zeugs/scripte/mp3blstr2dscrbblr.sh.txt · Zuletzt geändert: 2011/09/10 12:52 (Externe Bearbeitung)
 
Falls nicht anders bezeichnet, ist der Inhalt dieses Wikis unter der folgenden Lizenz veröffentlicht: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki