Skip to content

Instantly share code, notes, and snippets.

@navarr
Created December 10, 2009 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navarr/253187 to your computer and use it in GitHub Desktop.
Save navarr/253187 to your computer and use it in GitHub Desktop.
A simple protocol for sending Audio UPdates to an Audioscrobbler Service using the Audioscrobbler Realtime Submission Protocol (v1.2.1)
<?php
/*
Audioscrobbler Realtime Submission Protocol v1.2.1
Written by: Navarr T. Barnier
Version: 0.1
NOT FOR WEB SERVICES ACCOUNTS (maybe a later version)
Time Log
-----------------------------------------------------------
Date Start End Duration Description
-----------------------------------------------------------
11/07/2008 10:42 11:19 37 mn A Good Bit of Work, lol.
11/07/2008 11:25 11:59 34 mn The rest of the preliminary Work - next is debugging
11/07/2008 12:00 12:22 22 mn Testing and Tweaks
----------------------------- 93 mn Wow, 1.5 hr of work!
*/
class Scrobbler
{
public $protocol ="http://";
protected $server = "post.audioscrobbler.com";
protected $dir = "/";
protected $port = 80;
protected $user = "";
protected $pass = "";
public $clientID = "tst";
public $clientVersion = "1.0";
public $debug = "";
protected $scrobblerVersion="1.2.1";
protected $session = null;
protected $nowPlayingUri = null;
protected $submissionUri = null;
public function __construct($server = null,$port = null,$protocol = null)
{
$this->setInfo($server,$port,null,null,null,null,null,$protocol);
$this->debug = array();
}
public function setInfo($server = null,$port = null,$user = null,$pass = null,$clientID = null,$clientVersion = null,$dir = null,$protocol = null)
{
if ($server != null)
{ $this->server = $server; }
if ($port != null)
{ $this->port = $port; }
if ($user != null)
{ $this->user = $user; }
if ($pass != null)
{ $this->pass = $pass; }
if ($clientID != null)
{ $this->clientID = $clientID; }
if ($clientVersion != null)
{ $this->clientVersion = $clientVersion; }
if ($dir != null)
{ $this->dir = $dir; }
if ($protocol != null)
{ $this->protocol = $protocol; }
return true;
}
public function setUserInfo($user = null,$pass = null)
{
return $this->setInfo(null,null,$user,$pass);
}
public function setClientInfo($id = null,$ver = null)
{
return $this->setInfo(null,null,null,null,$id,$ver);
}
public function handshake()
{
$time = time();
$vars = array();
$vars["hs"] = "true";
$vars["c"] = $this->clientID;
$vars["v"] = $this->clientVersion;
$vars["p"] = $this->scrobblerVersion;
$vars["u"] = $this->user;
$vars["t"] = $time;
$vars["a"] = md5(md5($this->pass).$time);
$response = $this->callMethod($vars,null,0,true);
if ($response === false)
{ return false; }
$this->session = $response[1];
$this->nowPlayingUri = $response[2];
$this->submissionUri = $response[3];
return true;
}
public function nowPlaying($artist,$track,$album = "",$length = "",$trackNumber = "",$trackID = "")
{ /* $trackID = MusicBrainz Track ID */
$vars = array();
$vars["s"] = $this->session;
$vars["a"] = $artist;
$vars["t"] = $track;
$vars["b"] = $album;
$vars["l"] = $length;
$vars["n"] = $trackNumber;
$vars["m"] = $trackID;
if ($this->callMethod($vars,$this->nowPlayingUri,null,false,true))
{ return true; }
else
{ return false; }
}
public function submission($artist,$track,$time,$source,$length = "",$rating = "",$album = "",$trackNumber = "",$trackID = "")
{ /* $trackID = MusicBrainz Track ID */
$source = strtoupper($source);
if ($source == "P" && $length == "")
{ trigger_error("Length must be specified if source is from user");return false; }
if ($source != "P" && $source != "R" && $source != "E" && substr($source,0,1) != "L")
{ $source = "U"; }
$vars = array();
$vars["s"] = $this->session;
$vars["a[0]"] = $artist;
$vars["t[0]"] = $track;
$vars["i[0]"] = $time;
$vars["o[0]"] = $source;
$vars["r[0]"] = $rating;
$vars["l[0]"] = $length;
$vars["b[0]"] = $album;
$vars["n[0]"] = $trackNumber;
$vars["m[0]"] = $trackID;
if ($this->callMethod($vars,$this->submissionUri,null,false,true))
{ return true; }
else
{ return false; }
}
protected function callMethod($vars,$method = null,$errorRun = null,$handshake = false,$uri = false)
{
if ($errorRun == null) { $errorRun = 0; }
if ($errorRun == 1 && $handshake)
{ return false; }
if ($errorRun == 3)
{
if (!$this->handshake())
{
return false;
}
else
{
return $this->callMethod($vars,$method,$errorRun,$handshake,$uri);
}
}
$errorRun++;
if (!is_array($vars))
{ return false; }
if ($method == null)
{ $method = ""; }
if (!$uri)
{ $url = $this->protocol.$this->server.":".$this->port.$this->dir.$method; }
else
{ $url = $method; }
$i = false;
foreach($vars as $k => $v)
{
if ($i == false) { $start = "?";$i = true; } else { $start = "&"; }
$url .= $start.$k."=".urlencode($v);
}
$this->debug["lastURI"] = $url;
// if ($this->prefMethod != "curl")
// {
// if (!$response = file_get_contents($url))
// { $response = false; }
// }
//
// if ($this->prefMethod == "curl" || $response === false)
// {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FRESH_CONNECT,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_FORBID_REUSE,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
if ($uri)
{
curl_setopt($ch,CURLOPT_POST,true);
}
$response = curl_exec($ch);
// }
$response = explode("\n",$response);
$response[0] = strtoupper($response[0]);
if ($response[0] == "OK")
{ return $response; }
else
{
if (substr($response[0],0,6) == "FAILED" || $response[0] == "BADSESSION")
{
if (!$return = $this->callMethod($vars,$method,$errorRun,$handshake,$uri))
{ return false; }
else
{ return $return; }
}
$this->debug["error"] = true;
$this->debug["errorMsg"] = $response[0];
return false;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment