Tuesday, October 27, 2009

Old Code – class::Scrobbler

A long time ago I wrote some PHP code for the AudioScrobbler API.  I
wrote it as more of a proof-of-concept and never touched it again, so I don’t know how well it works. 
I may play around with it some more as part of a test just to make sure the thing still works, but I wanted
to share my code with you.

href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/" rel="license"> style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px"
alt="Creative Commons License" src="http://i.creativecommons.org/l/by-nc-sa/3.0/us/80x15.png" />



href="http://purl.org/dc/dcmitype/Text" xmlns:dc="http://purl.org/dc/elements/1.1/">class::Scrobbler
by property="cc:attributionName" xmlns:cc="http://creativecommons.org/ns#">Navarr T. Barnier is
licensed under a Creative
Commons Attribution-Noncommercial-Share Alike 3.0 United States License
.
Based on a
work at codepad.org.

Permissions beyond the scope of this license may be available at href="mailto:[email protected]" rel="cc:morePermissions" xmlns:cc="http://creativecommons.org/ns#">mailto:[email protected].

Source available at codepad and continued
after the break.

id="codeSnippetWrapper">
 1: <?php




 2: /* 



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 3: Audioscrobbler Realtime Submission Protocol v1.2.1



 4:  Written by: Navarr T. Barnier




 5:  Version: 0.1



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 6: NOT FOR WEB SERVICES ACCOUNTS (maybe a later version)



 7: 



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 8: Time Log


style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 9: -----------------------------------------------------------



 10:  Date Start End Duration Description




 11:  -----------------------------------------------------------




 12:  11/07/2008 10:42 11:19 37 mn A Good Bit of Work, lol.




 13:  11/07/2008 11:25 11:59 34 mn The rest of the preliminary Work - next is debugging




 14:  11/07/2008 12:00 12:22 22 mn Testing and Tweaks




 15:  ----------------------------- 93 mn Wow, 1.5 hr of work!




 16:  */



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 17: 



 18: class Scrobbler



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 19: {



 20:  




 21: public $protocol ="http://";




 22: protected $server = "post.audioscrobbler.com";




 23: protected $dir = "/";




 24: protected $port = 80;




 25: protected $user = "";




 26: protected $pass = "";




 27: public $clientID = "tst";




 28: public $clientVersion = "1.0";




 29: public $debug = "";




 30: protected $scrobblerVersion="1.2.1";




 31: protected $session = null;




 32: protected $nowPlayingUri = null;




 33: protected $submissionUri = null;




 34:  




 35: public function __construct($server
= null,$port = null,$protocol
= null)




 36: {




 37: $this->setInfo($server,$port,            style="color: #0000ff">null,null,            style="color: #0000ff">null,null,            style="color: #0000ff">null,$protocol);




 38: $this->debug =
array();




 39: }




 40:  




 41: public function setInfo($server =
null,$port = null,$user
= null,$pass = null,$clientID
= null,$clientVersion = null,$dir
= null,$protocol = null)




 42: {




 43: if ($server != null)




 44: { $this->server
= $server; }



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 45: if ($port != null)



 46: { $this->port =
$port; }



 47:
if ($user != null)




 48: { $this->user =
$user; }



 49:
if ($pass != null)




 50: { $this->pass =
$pass; }



 51:
if ($clientID != null)




 52: { $this->clientID
= $clientID; }



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 53: if ($clientVersion != null)



 54: { $this->clientVersion
= $clientVersion; }



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 55: if ($dir != null)



 56: { $this->dir =
$dir; }



 57:
if ($protocol != null)




 58: { $this->protocol
= $protocol; }



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 59: return true;



 60: }




 61:  




 62: public function setUserInfo($user =
null,$pass = null)




 63: {




 64: return $this->setInfo(null,            style="color: #0000ff">null,$user,$pass);




 65: }




 66:  




 67: public function setClientInfo($id =
null,$ver = null)




 68: {




 69: return $this->setInfo(null,            style="color: #0000ff">null,null,            style="color: #0000ff">null,$id,$ver);




 70: }




 71:  




 72: public function handshake()




 73: {




 74: $time = time();




 75: $vars =             style="color: #0000ff">array();




 76: $vars[            style="color: #006080">"hs"] = "true";




 77: $vars[            style="color: #006080">"c"] = $this->clientID;




 78: $vars[            style="color: #006080">"v"] = $this->clientVersion;




 79: $vars[            style="color: #006080">"p"] = $this->scrobblerVersion;




 80: $vars[            style="color: #006080">"u"] = $this->user;




 81: $vars[            style="color: #006080">"t"] = $time;




 82: $vars[            style="color: #006080">"a"] = md5(md5($this->pass).$time);




 83: $response = $this->callMethod($vars,            style="color: #0000ff">null,0,true);




 84:  




 85: if ($response === false)




 86: {             style="color: #0000ff">return false; }




 87: 




 88: $this->session
= $response[1];



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 89: $this->nowPlayingUri
= $response[2];


style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 90: $this->submissionUri
= $response[3];


style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 91:



 92: return true;




 93:  




 94: }




 95:  




 96: public function
nowPlaying($artist,$track,$album = "",$length
= "",$trackNumber = "",$trackID = "")




 97: {             style="color: #008000">/* $trackID = MusicBrainz Track ID */




 98: $vars =             style="color: #0000ff">array();




 99: $vars[            style="color: #006080">"s"] = $this->session;




 100: $vars[            style="color: #006080">"a"] = $artist;




 101: $vars[            style="color: #006080">"t"] = $track;




 102: $vars[            style="color: #006080">"b"] = $album;




 103: $vars[            style="color: #006080">"l"] = $length;




 104: $vars[            style="color: #006080">"n"] = $trackNumber;




 105: $vars[            style="color: #006080">"m"] = $trackID;




 106:             style="color: #0000ff">if ($this->callMethod($vars,$this->nowPlayingUri,            style="color: #0000ff">null,false,            style="color: #0000ff">true))




 107: {             style="color: #0000ff">return true; }




 108:             style="color: #0000ff">else




 109: {             style="color: #0000ff">return false; }




 110: }




 111:  




 112:             style="color: #0000ff">public function
submission($artist,$track,$time,$source,$length = "",$rating
= "",$album = "",$trackNumber = "",$trackID
= "")




 113: {             style="color: #008000">/* $trackID = MusicBrainz Track ID */




 114: $source =
strtoupper($source);



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 115: style="color: #0000ff">if ($source == "P"
&& $length == "")



 116: { trigger_error(            style="color: #006080">"Length must be specified if source is from user");            style="color: #0000ff">return false; }




 117:             style="color: #0000ff">if ($source != "P"
&& $source != "R" &&
$source != "E" &&
substr($source,0,1) != "L")




 118: { $source =             style="color: #006080">"U"; }




 119: $vars =             style="color: #0000ff">array();




 120: $vars[            style="color: #006080">"s"] = $this->session;




 121: $vars[            style="color: #006080">"a[0]"] = $artist;




 122: $vars[            style="color: #006080">"t[0]"] = $track;




 123: $vars[            style="color: #006080">"i[0]"] = $time;




 124: $vars[            style="color: #006080">"o[0]"] = $source;




 125: $vars[            style="color: #006080">"r[0]"] = $rating;




 126: $vars[            style="color: #006080">"l[0]"] = $length;




 127: $vars[            style="color: #006080">"b[0]"] = $album;




 128: $vars[            style="color: #006080">"n[0]"] = $trackNumber;




 129: $vars[            style="color: #006080">"m[0]"] = $trackID;




 130:             style="color: #0000ff">if ($this->callMethod($vars,$this->submissionUri,            style="color: #0000ff">null,false,            style="color: #0000ff">true))




 131: {             style="color: #0000ff">return true; }




 132:             style="color: #0000ff">else




 133: {             style="color: #0000ff">return false; }




 134: }




 135: 




 136:  




 137:             style="color: #0000ff">protected function
callMethod($vars,$method = null,$errorRun = style="color: #0000ff">null,$handshake = false,$uri
= false)




 138: {




 139:  




 140:             style="color: #0000ff">if ($errorRun == null)
{ $errorRun = 0; }



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 141: 



 142:             style="color: #0000ff">if ($errorRun == 1 && $handshake)




 143: {             style="color: #0000ff">return false; }




 144:  




 145:             style="color: #0000ff">if ($errorRun == 3)




 146: {




 147:             style="color: #0000ff">if (!$this->handshake())




 148: {




 149:             style="color: #0000ff">return false;




 150: }




 151:             style="color: #0000ff">else




 152: {




 153:             style="color: #0000ff">return $this->callMethod($vars,$method,$errorRun,$handshake,$uri);




 154: }




 155: }




 156:  




 157: $errorRun++;




 158:  




 159:             style="color: #0000ff">if (!is_array($vars)) 




 160: {             style="color: #0000ff">return false; }




 161:  




 162:             style="color: #0000ff">if ($method == null)



 163:
{ $method = ""; }




 164:  




 165:             style="color: #0000ff">if (!$uri)




 166: { $url = $this->protocol.$this->server.            style="color: #006080">":".$this->port.$this->dir.$method; }




 167:             style="color: #0000ff">else




 168: { $url = $method; }




 169:  




 170: $i =             style="color: #0000ff">false;




 171:  




 172:             style="color: #0000ff">foreach($vars as $k =>
$v)



 173:
{



 174:
if ($i == false)
{ $start = "?";$i = true; } else { $start = style="color: #006080">"&"; }




 175: $url .= $start.$k.            style="color: #006080">"=".urlencode($v);




 176: }




 177:  




 178: $this->debug[            style="color: #006080">"lastURI"] = $url;




 179:  




 180:             style="color: #008000">// if ($this->prefMethod != "curl")




 181:             style="color: #008000">// {




 182:             style="color: #008000">// if (!$response = file_get_contents($url)) 




 183:             style="color: #008000">// { $response = false; }




 184:             style="color: #008000">// }




 185:             style="color: #008000">//



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 186: style="color: #008000">// if ($this->prefMethod == "curl" || $response ===
false)


style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 187: style="color: #008000">// {



 188: $ch = curl_init();




 189:
curl_setopt($ch,CURLOPT_URL,$url);



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 190:
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);



 191:
curl_setopt($ch,CURLOPT_FRESH_CONNECT,1);



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 192:
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);



 193:
curl_setopt($ch,CURLOPT_FORBID_REUSE,1);



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 194:
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);



 195:             style="color: #0000ff">if ($uri)




 196: {




 197:
curl_setopt($ch,CURLOPT_POST,true);




 198: }




 199: $response =
curl_exec($ch);



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 200: style="color: #008000">// }



 201:  




 202: $response = explode(            style="color: #006080">"\n",$response);




 203: $response[0] =
strtoupper($response[0]);



style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt;
border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%;
padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr;
border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible;
padding-top: 0px"> 204: 



 205:             style="color: #0000ff">if ($response[0] == "OK")




 206: {             style="color: #0000ff">return $response; }




 207:             style="color: #0000ff">else




 208: {




 209:             style="color: #0000ff">if (substr($response[0],0,6) == "FAILED"
|| $response[0] == "BADSESSION")




 210: {




 211:             style="color: #0000ff">if (!$return = $this->callMethod($vars,$method,$errorRun,$handshake,$uri))




 212: {             style="color: #0000ff">return false; }




 213:             style="color: #0000ff">else




 214: {             style="color: #0000ff">return $return; }




 215: }




 216:  




 217: $this->debug[            style="color: #006080">"error"] = true;




 218: $this->debug[            style="color: #006080">"errorMsg"] = $response[0];




 219:             style="color: #0000ff">return false;




 220: }




 221: }




 222: }




 223: ?>


No comments:

Post a Comment