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 plain text editor and edit the five parameters in its header.
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
$username="foo"; // Your username on last.fm
$password="bar"; // Password to your account on last.fm
$getid3lib="../getid3/getid3.php"; // Path to getid3.php file
$sessionFile="C:\\Documents and Settings\\foo\\AppData\\lastfm.sid"; // Filename where session key will be kept
$CP="CP1250"; // Code page used in ID3 tags of your music.
$api_key="5cc2552420a26f71f9343e34f0783d54"; // scrobbat API key. Do not change.
$secret="b286614cea2d1621ea96fb8021f6ae7b"; // scrobbat autentization string
$mp3file=@$_REQUEST['file'];
if (!$mp3file) $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");
$art=iconv($CP,"UTF-8",trim($artist));
$album=@$tagsv2['album'][0]; if (!$album) $album=@$tagsv1['album'][0];
$alb=iconv($CP,"UTF-8",trim($album));
$track=@$tagsv2['title'][0]; if (!$track) $track=@$tagsv1['title'][0];
if (!$track) die("Id3tag 'Title' not set\r\n");
$tit=iconv($CP,"UTF-8",trim($track));
$trnr=@$tagsv2['track_number'][0]; if (!$trnr) $trnr=@$tagsv1['track_number'][0];
$trnr=(integer)$trnr;
$secs=(integer)@$info['playtime_seconds'];
$CurlHandle=curl_init();
curl_setopt($CurlHandle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($CurlHandle, CURLOPT_TIMEOUT, 15);
curl_setopt($CurlHandle, CURLOPT_RETURNTRANSFER, true);
if (file_exists($sessionFile)) $sessionkey=file_get_contents($sessionFile);
else {$api_sig=md5("api_key".$api_key."methodauth.getMobileSessionpassword".$password."username".$username.$secret);
$post="username=".urlencode($username)."&password=".urlencode($password)."&api_key=$api_key&api_sig=$api_sig";
curl_setopt($CurlHandle, CURLOPT_URL, "https://ws.audioscrobbler.com/2.0/?method=auth.getMobileSession");
curl_setopt($CurlHandle, CURLOPT_POST, 1);
curl_setopt($CurlHandle, CURLOPT_POSTFIELDS, $post);
$response=curl_exec($CurlHandle);
$MobileSession=simplexml_load_string($response);
$status=@$MobileSession['status'];
$error=@$MobileSession['error'];
if ($status=="ok"){$sessionkey=$MobileSession->session->key; file_put_contents($sessionFile,$sessionkey); }
else die("Session failed: $error");
} curl_setopt($CurlHandle, CURLOPT_URL, "https://ws.audioscrobbler.com/2.0/?method=track.scrobble");
date_default_timezone_set("UTC"); $timestamp=time();
$api_sig=md5("album".$alb."api_key".$api_key."artist".$art."duration".$secs."methodtrack.scrobblesk".$sessionkey.
"timestamp".$timestamp."track".$tit."trackNumber".$trnr.$secret);
$post="album=".urlencode($alb)."&api_key=$api_key&api_sig=$api_sig&artist=".urlencode($art).
"&duration=$secs&sk=$sessionkey×tamp=$timestamp&track=".urlencode($tit)."&trackNumber=$trnr";
curl_setopt($CurlHandle, CURLOPT_POSTFIELDS, $post);
$response=(curl_exec($CurlHandle));
curl_close($CurlHandle);
$scrobbles=simplexml_load_string($response);
$status=@$scrobbles['status'];
$error =@$scrobbles->error;
if ($status!="ok") die("Scrobble failed: $error");
else echo "Submitted to last.fm\r\n";
exit(1);
?>