Navarr's Tech Side The Technical Side of my Life

27Oct/090

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.

Creative Commons License

class::Scrobbler by 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 mailto:navarr@koneko-chan.net.

Source available at codepad and continued after the break.

   1: <?php

   2:     /* 

   3:         Audioscrobbler Realtime Submission Protocol v1.2.1

   4:         Written by: Navarr T. Barnier

   5:         Version: 0.1

   6:         NOT FOR WEB SERVICES ACCOUNTS (maybe a later version)

   7: 

   8:     Time Log

   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:     */

  17:  

  18: class Scrobbler

  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,null,null,null,null,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; }

  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; }

  53:         if ($clientVersion != null)

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

  55:         if ($dir != null)

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

  57:         if ($protocol != null)

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

  59:         return true;

  60:         }

  61:  

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

  63:         {

  64:         return $this->setInfo(null,null,$user,$pass);

  65:         }

  66:  

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

  68:         {

  69:         return $this->setInfo(null,null,null,null,$id,$ver);

  70:         }

  71:  

  72:     public function handshake()

  73:         {

  74:         $time = time();

  75:         $vars = array();

  76:         $vars["hs"] = "true";

  77:         $vars["c"] = $this->clientID;

  78:         $vars["v"] = $this->clientVersion;

  79:         $vars["p"] = $this->scrobblerVersion;

  80:         $vars["u"] = $this->user;

  81:         $vars["t"] = $time;

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

  83:         $response = $this->callMethod($vars,null,0,true);

  84:  

  85:         if ($response === false)

  86:             { return false; }

  87:         

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

  89:         $this->nowPlayingUri = $response[2];

  90:         $this->submissionUri = $response[3];

  91:         

  92:         return true;

  93:  

  94:         }

  95:  

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

  97:         { /* $trackID = MusicBrainz Track ID */

  98:         $vars = array();

  99:         $vars["s"] = $this->session;

 100:         $vars["a"] = $artist;

 101:         $vars["t"] = $track;

 102:         $vars["b"] = $album;

 103:         $vars["l"] = $length;

 104:         $vars["n"] = $trackNumber;

 105:         $vars["m"] = $trackID;

 106:         if ($this->callMethod($vars,$this->nowPlayingUri,null,false,true))

 107:             { return true; }

 108:         else

 109:             { return false; }

 110:         }

 111:  

 112:     public function submission($artist,$track,$time,$source,$length = "",$rating = "",$album = "",$trackNumber = "",$trackID = "")

 113:         { /* $trackID = MusicBrainz Track ID */

 114:         $source = strtoupper($source);

 115:         if ($source == "P" && $length == "")

 116:             { trigger_error("Length must be specified if source is from user");return false; }

 117:         if ($source != "P" && $source != "R" && $source != "E" && substr($source,0,1) != "L")

 118:             { $source = "U"; }

 119:         $vars = array();

 120:         $vars["s"] = $this->session;

 121:         $vars["a[0]"] = $artist;

 122:         $vars["t[0]"] = $track;

 123:         $vars["i[0]"] = $time;

 124:         $vars["o[0]"] = $source;

 125:         $vars["r[0]"] = $rating;

 126:         $vars["l[0]"] = $length;

 127:         $vars["b[0]"] = $album;

 128:         $vars["n[0]"] = $trackNumber;

 129:         $vars["m[0]"] = $trackID;

 130:         if ($this->callMethod($vars,$this->submissionUri,null,false,true))

 131:             { return true; }

 132:         else

 133:             { return false; }

 134:         }

 135:         

 136:  

 137:     protected function callMethod($vars,$method = null,$errorRun = null,$handshake = false,$uri = false)

 138:         {

 139:  

 140:         if ($errorRun == null) { $errorRun = 0; }

 141:  

 142:         if ($errorRun == 1 && $handshake)

 143:             { return false; }

 144:  

 145:         if ($errorRun == 3)

 146:             {

 147:                 if (!$this->handshake())

 148:                     {

 149:                     return false;

 150:                     }

 151:                 else

 152:                     {

 153:                     return $this->callMethod($vars,$method,$errorRun,$handshake,$uri);

 154:                     }

 155:             }

 156:  

 157:         $errorRun++;

 158:  

 159:         if (!is_array($vars)) 

 160:             { return false; }

 161:  

 162:         if ($method == null) 

 163:             { $method = ""; }

 164:  

 165:         if (!$uri)

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

 167:         else

 168:             { $url = $method; }

 169:  

 170:         $i = false;

 171:  

 172:         foreach($vars as $k => $v)

 173:             {

 174:             if ($i == false) { $start = "?";$i = true; } else { $start = "&"; }

 175:             $url .= $start.$k."=".urlencode($v);

 176:             }

 177:  

 178:         $this->debug["lastURI"] = $url;

 179:  

 180:     //    if ($this->prefMethod != "curl")

 181:     //        {

 182:     //        if (!$response = file_get_contents($url)) 

 183:     //            { $response = false; }

 184:     //        }

 185:     //

 186:     //    if ($this->prefMethod == "curl" || $response === false)

 187:     //        {

 188:             $ch = curl_init();

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

 190:             curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

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

 192:             curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);

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

 194:             curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);

 195:             if ($uri)

 196:                 {

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

 198:                 }

 199:             $response = curl_exec($ch);

 200:     //        }

 201:  

 202:         $response = explode("\n",$response);

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

 204:  

 205:         if ($response[0] == "OK")

 206:             { return $response; }

 207:         else

 208:             {

 209:             if (substr($response[0],0,6) == "FAILED" || $response[0] == "BADSESSION")

 210:                 {

 211:                 if (!$return = $this->callMethod($vars,$method,$errorRun,$handshake,$uri))

 212:                     { return false; }

 213:                 else

 214:                     { return $return; }

 215:                 }

 216:  

 217:             $this->debug["error"] = true;

 218:             $this->debug["errorMsg"] = $response[0];

 219:             return false;

 220:             }

 221:         }

 222:     }

 223: ?>

blog comments powered by Disqus