scrobbat is a batch script which will submit information about music you are listening to the Last.fm community server. This process is called scrobbling and it uses web service described here.
The script can be useful if you use a music player which is not supported by the official Last.fm Scrobbler or by other scrobblers.
Copy and paste the script below or download and unpack scrobbat.zip.
In order to run scrobbat.php you will need PHP
installed on your PC. On Windows be sure to uncomment the statement
extension=php_curl.dll in the PHP.INI configuration file.
You will also need ID3Tags library
which can be downloaded here.
Unpack the library files somewhere in your PHP installation.
Open the script scrobbat.php with a text editor and edit the four definitions
in its header. You can get the MD5 hash of your last.fm password using the PHP function:
Run php-cli in console window and (providing the password of your last.fm account is bar)
type <?php echo md5('bar');?> and press Enter.
Then close the console input of php-cli with Ctrl-Z and press Enter again.
Now you should see the 32 characters long MD5 hash echoed on console.
The script requires one MP3 file as the first and only argument. The file must have at least the fields Artist and Title set in ID3 tag. Example:
php-cli scrobbat.php "/Music/Grunge/Creed - Weathered/Weathered.mp3"
The script exits with errorlevel 1 when the scrobbling succeeded, otherwise it dies with errorlevel 0. You may want to check the errorlevel and repeat the attempt several times if it has failed.
<?php // Script scrobbat.php submits one mp3 file to last.fm
define ("LASTFMNAME","foo"); // Login name of your account on last.fm
define ("MD5PWD","37b51d194a7513e45b56f6524f2d51f2"); // MD5 hash of password
define ("CODEPAGE","CP1250"); // Code page used in ID3 tags of your music
define ("GETID3LIB","../getid3/getid3.php"); // Path to getid3.php file
$mp3file=@$_SERVER['argv'][1];
if (!$mp3file) die("No input file given\r\n");
include GETID3LIB;
$getID3 = new getID3;
$info=$getID3->analyze($mp3file);
$tagsv1=@$info['tags']['id3v1']; $tagsv2=@$info['tags']['id3v2'];
$artist=@$tagsv2['artist'][0]; if (!$artist) $artist=@$tagsv1['artist'][0];
if (!$artist) die("Id3tag 'Artist' not set\r\n");
$uart=urlencode(iconv(CODEPAGE,"UTF-8",trim($artist)));
$album=@$tagsv2['album'][0]; if (!$album) $album=@$tagsv1['album'][0];
$ualb=urlencode(iconv(CODEPAGE,"UTF-8",trim($album)));
$track=@$tagsv2['title'][0]; if (!$track) $track=@$tagsv1['title'][0];
if (!$track) die("Id3tag 'Title' not set\r\n");
$utit=urlencode(iconv(CODEPAGE,"UTF-8",trim($track)));
$trnr=@$tagsv2['track_number'][0]; if (!$trnr) $trnr=@$tagsv1['track_number'][0];
$utrnr=(integer)$trnr;
$secs=(integer)@$info['playtime_seconds'];
$time=time();
$token=md5(MD5PWD.$time);
$Url="http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=bat&v=1.0&u=".
LASTFMNAME."&t=$time&a=$token";
$CurlHandle=curl_init();
curl_setopt($CurlHandle, CURLOPT_URL, $Url);
curl_setopt($CurlHandle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($CurlHandle, CURLOPT_TIMEOUT, 15);
curl_setopt($CurlHandle, CURLOPT_RETURNTRANSFER, true);
$response=(curl_exec($CurlHandle));
if (curl_errno($CurlHandle)) die("CURL error: ".curl_error($CurlHandle)."\r\n");
$resparr=explode("\n",$response);
if ($resparr[0]!="OK") die($resparr[0]);
$sid=$resparr[1]; $SubUrl=$resparr[3];
$post="s=$sid&a[0]=$uart&t[0]=$utit&i[0]=$time&o[0]=P&r[0]=&l[0]=$secs&b[0]=$ualb&n[0]=$utrnr&m[0]=";
curl_setopt($CurlHandle, CURLOPT_URL, $SubUrl);
curl_setopt($CurlHandle, CURLOPT_POST, 1);
curl_setopt($CurlHandle, CURLOPT_POSTFIELDS, $post);
$response=(curl_exec($CurlHandle));
if (curl_errno($CurlHandle)) die("Curl error:".curl_error($CurlHandle)."\r\n");
curl_close($CurlHandle);
if (trim($response)!="OK") die ($response);
else echo "Submitted to last.fm\r\n"; exit(1);
?>