Advertisement
Papadopolis

Untitled

Jun 2nd, 2012
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.11 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * --------------- ShoutCast Radio Stations Search API ---------------
  5.  *
  6.  * @author Pedro P. L. Papadópolis (a.k.a Mandrakke).
  7.  * @copyright There is no copyright, you can use it as you want, you can also show it to you mother to her see how much you are smart.
  8.  *
  9.  * If you speak Portuguese, try out our server;
  10.  * http://brazucas-server.com
  11.  *
  12.  * If you dont, try out our translated-to-english tool (SA-MP Online Objects Viewer);
  13.  * http://brazucas-server.com/$objetos
  14.  *
  15.  *
  16.  * This script is also hosted on http://api.brazucas-dev.com/ShoutCastAPI.php but it can be turned offline at any time if the number of requests increase too much.
  17.  *
  18.  * More information about how to integrate this script with a SA-MP server can be find at the oficial topic on SA-MP forums;
  19.  * http://forum.sa-mp.com/showthread.php?t=347016
  20.  *
  21.  *
  22.  *
  23.  * Version: R1-1 Beta (released in 02/06/2012)
  24.  *
  25.  * -------------------------------------------------------------------
  26.  */
  27.  
  28. if(!function_exists("curl_init")) die("cURL library is required.");
  29.  
  30. //Disable error messages, if you have issues with this script, comment this line below to see which error occurs.
  31. error_reporting("E_ALL");
  32.  
  33. final class ShoutCastAPI {
  34.     private $forbidden = array('-','!','"','#','$','%','&','\'','(',')','*','+',',','.','/',':',';','<','=','>','?','@','[','\\',']','_','`','{','|','}','~');
  35.    
  36.     function __construct() {        
  37.         $RequestValues = array(
  38.             "ajax"      => true,
  39.             "count"     => 10,
  40.             "order"     => "desc",
  41.             "strIndex"  => 0
  42.         );
  43.        
  44.         if(!isset($_GET["search"]) || strlen($_GET["search"]) == 0) {
  45.             $url = "http://www.shoutcast.com/ajax_dir";
  46.            
  47.             if(!isset($_GET["field"]))
  48.                 $RequestValues["criteria"] = "listenershead";
  49.             else
  50.                 $RequestValues["criteria"] = $_GET["field"]."head";
  51.         } else {
  52.             $url = "http://www.shoutcast.com/search-ajax/". urlencode($_GET["search"]);
  53.            
  54.             if(!isset($_GET["field"]))
  55.                 $RequestValues["mode"] = "listenershead2";
  56.             else
  57.                 $RequestValues["mode"] = $_GET["field"]."head2";
  58.         }
  59.        
  60.         $params = null;
  61.         foreach($RequestValues as $ind => $val) {
  62.             if($_GET[$ind] !== null)
  63.                 $RequestValues[$ind] = $_GET[$ind];
  64.                    
  65.             $params .= $ind. "=". $RequestValues[$ind]. "&";
  66.         }
  67.        
  68.         $stations = array();
  69.        
  70.         $req = $this->Request($url, 1, null, $params, "http://www.shoutcast.com/");
  71.  
  72.         $filter = new DOMDocument;
  73.         @$filter->loadHTML($req["data"][1]);
  74.        
  75.         $divs = $filter->getElementsByTagName("div");
  76.         $i = 0;
  77.         foreach($divs as $div) {
  78.             if($div->getAttribute("class") == "dirlist") {
  79.                 $a = $div->getElementsByTagName("a")->item(0);
  80.                
  81.                 $stations["stations"]["s".$i]["stationid"]  = $a->getAttribute("id");
  82.                
  83.                 $tmp = $a->getAttribute("name");
  84.                 for($y = 0; $y < strlen($tmp); ++$y)
  85.                     if(!in_array($tmp[$y], $this->forbidden))
  86.                         $stations["stations"]["s".$i]["name"] .= $tmp[$y];
  87.                        
  88.                 $stations["stations"]["s".$i]["id"]        = preg_replace("/^.*?id./i", "", $a->getAttribute("href"));
  89.                
  90.                 $childDivs = $div->getElementsByTagName("div");
  91.                 foreach($childDivs as $childDiv)
  92.                     switch($childDiv->getAttribute("class")) {
  93.                         case "playingtext":
  94.                             $tmp = $childDiv->getAttribute("title");
  95.                            
  96.                             if($tmp == null && $childDiv->getElementsByTagName("span")->item(0) !== null)
  97.                                 $tmp = $childDiv->getElementsByTagName("span")->item(0)->getAttribute("title");
  98.                            
  99.                             for($a = 0; $a < strlen($tmp); ++$a)
  100.                                 if(!in_array($tmp[$a], $this->forbidden))
  101.                                     $stations["stations"]["s".$i]["nowplaying"] .= $tmp[$a];
  102.                            
  103.                             if(strlen($stations["stations"]["s".$i]["nowplaying"]) > 13)
  104.                                 $stations["stations"]["s".$i]["nowplaying"] = substr($stations["stations"]["s".$i]["nowplaying"], 0, 13). "...";
  105.                         break;
  106.                         case "dirgenre":    $stations["stations"]["s".$i]["genre"]      = $childDiv->nodeValue;                                         break;
  107.                         case "dirlistners": $stations["stations"]["s".$i]["listeners"]  = $childDiv->nodeValue;                                         break;
  108.                         case "dirbitrate":  $stations["stations"]["s".$i]["bitrate"]    = $childDiv->nodeValue;                                         break;
  109.                         case "dirtype":     $stations["stations"]["s".$i]["type"]       = $childDiv->nodeValue;                                         break;
  110.                     }
  111.                
  112.                 ++$i;
  113.             }
  114.         }
  115.        
  116.         $stations["stations"]["count"] = $i;
  117.        
  118.         if(@$vars = $_GET["show"]) {
  119.             $showOnly = explode("|", $vars);
  120.            
  121.             foreach($stations["stations"] as $stationId => $stationData)
  122.                 foreach($stationData as $ind => $val)
  123.                     if(!in_array($ind, $showOnly))
  124.                         unset($stations["stations"][$stationId][$ind]);
  125.         }
  126.        
  127.         switch(strtolower($_GET["format"])) {
  128.             case "json":
  129.                 $output .= json_encode($stations);
  130.             break;
  131.             case "dini":
  132.             default:
  133.                 $output = null;
  134.                 foreach($stations["stations"] as $stationId => $stationData)
  135.                     if(is_array($stationData))
  136.                         foreach($stationData as $ind => $val)
  137.                             $output .= $stationId. $ind. "=". $val. "\r\n";
  138.                     else
  139.                         $output .= $stationId. "=". $stationData. "\r\n";
  140.         }
  141.        
  142.         die($output);
  143.     }
  144.    
  145.     private function Request($a, $b, $c, $d, $e) {
  146.         $ch = curl_init();
  147.        
  148.         $exec = curl_setopt_array($ch, array(
  149.                 CURLOPT_URL => $a,
  150.                 CURLOPT_RETURNTRANSFER => $b,
  151.                 CURLOPT_HEADER => 1,
  152.                 CURLOPT_NOBODY => 0,
  153.                 CURLOPT_COOKIE => $c,
  154.                 CURLOPT_CUSTOMREQUEST => ($d)?"POST":"GET",
  155.                 CURLOPT_POSTFIELDS => $d,
  156.                 CURLOPT_FOLLOWLOCATION => 1,
  157.                 CURLOPT_TIMEOUT => 6,
  158.                 CURLOPT_REFERER => $e
  159.             )
  160.         );
  161.        
  162.         return array(
  163.             "data"      => explode("\r\n\r\n", curl_exec($ch), 2),
  164.             "httpcode"  => (int)curl_getinfo($ch, CURLINFO_HTTP_CODE)
  165.         );
  166.     }
  167. } new ShoutCastAPI();
  168. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement