Advertisement
Papadopolis

Untitled

May 30th, 2012
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.13 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://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 Beta (released in 30/05/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.     function __construct() {        
  35.         $RequestValues = array(
  36.             "ajax"      => true,
  37.             "count"     => 10,
  38.             "order"     => "desc",
  39.             "strIndex"  => 0
  40.         );
  41.        
  42.         if(!isset($_GET["search"]) || strlen($_GET["search"]) == 0) {
  43.             $url = "http://www.shoutcast.com/ajax_dir";
  44.            
  45.             if(!isset($_GET["field"]))
  46.                 $RequestValues["criteria"] = "listenershead";
  47.             else
  48.                 $RequestValues["criteria"] = $_GET["field"]."head";
  49.         } else {
  50.             $url = "http://www.shoutcast.com/search-ajax/". $_GET["search"];
  51.            
  52.             if(!isset($_GET["field"]))
  53.                 $RequestValues["mode"] = "listenershead2";
  54.             else
  55.                 $RequestValues["mode"] = $_GET["field"]."head2";
  56.         }
  57.        
  58.         $params = null;
  59.         foreach($RequestValues as $ind => $val) {
  60.             if($_GET[$ind] !== null)
  61.                 $RequestValues[$ind] = $_GET[$ind];
  62.                    
  63.             $params .= $ind. "=". $RequestValues[$ind]. "&";
  64.         }
  65.        
  66.         $stations = array();
  67.        
  68.         $req = $this->Request($url, 1, null, $params, "http://www.shoutcast.com/");
  69.  
  70.         $filter = new DOMDocument;
  71.         @$filter->loadHTML($req["data"][1]);
  72.        
  73.         $divs = $filter->getElementsByTagName("div");
  74.        
  75.         $i = 0;
  76.         foreach($divs as $div) {
  77.             if($div->getAttribute("class") == "dirlist") {
  78.                 $a = $div->getElementsByTagName("a")->item(0);
  79.                
  80.                 $stations["stations"]["s".$i]["stationid"]  = $a->getAttribute("id");
  81.                 $stations["stations"]["s".$i]["name"]       = stripslashes(strip_tags($a->getAttribute("name"))); //Slashes can cause a bug with Draco Blue's DJson and DIni, tags have no need to be displayed.
  82.                 $stations["stations"]["s".$i]["id"]        = preg_replace("/^.*?id./i", "", $a->getAttribute("href"));
  83.                
  84.                 $childDivs = $div->getElementsByTagName("div");
  85.                 foreach($childDivs as $childDiv)
  86.                     switch($childDiv->getAttribute("class")) {
  87.                         case "playingtext": $stations["stations"]["s".$i]["nowplaying"] = stripslashes(strip_tags($childDiv->getAttribute("title")));   break;
  88.                         case "dirgenre":    $stations["stations"]["s".$i]["genre"]      = $childDiv->nodeValue;                                         break;
  89.                         case "dirlistners": $stations["stations"]["s".$i]["listeners"]  = $childDiv->nodeValue;                                         break;
  90.                         case "dirbitrate":  $stations["stations"]["s".$i]["bitrate"]    = $childDiv->nodeValue;                                         break;
  91.                         case "dirtype":     $stations["stations"]["s".$i]["type"]       = $childDiv->nodeValue;                                         break;
  92.                     }
  93.                
  94.                 ++$i;
  95.             }
  96.         }
  97.        
  98.         $stations["stations"]["count"] = $i;
  99.        
  100.         if(@$vars = $_GET["show"]) {
  101.             $showOnly = explode("|", $vars);
  102.            
  103.             foreach($stations["stations"] as $stationId => $stationData)
  104.                 foreach($stationData as $ind => $val)
  105.                     if(!in_array($ind, $showOnly))
  106.                         unset($stations["stations"][$stationId][$ind]);
  107.         }
  108.        
  109.        
  110.         switch(strtolower($_GET["format"])) {
  111.             case "json":
  112.                 $output .= json_encode($stations);
  113.             break;
  114.             case "dini":
  115.             default:
  116.                 $output = null;
  117.                 foreach($stations["stations"] as $stationId => $stationData)
  118.                     if(is_array($stationData))
  119.                         foreach($stationData as $ind => $val)
  120.                             $output .= $stationId. $ind. "=". $val. "\r\n";
  121.                     else
  122.                         $output .= $stationId. "=". $stationData. "\r\n";
  123.         }
  124.        
  125.         die($output);
  126.     }
  127.    
  128.     private function Request($a, $b, $c, $d, $e) {
  129.         $ch = curl_init();
  130.        
  131.         $exec = curl_setopt_array($ch, array(
  132.                 CURLOPT_URL => $a,
  133.                 CURLOPT_RETURNTRANSFER => $b,
  134.                 CURLOPT_HEADER => 1,
  135.                 CURLOPT_NOBODY => 0,
  136.                 CURLOPT_COOKIE => $c,
  137.                 CURLOPT_CUSTOMREQUEST => ($d)?"POST":"GET",
  138.                 CURLOPT_POSTFIELDS => $d,
  139.                 CURLOPT_FOLLOWLOCATION => 1,
  140.                 CURLOPT_TIMEOUT => 6,
  141.                 CURLOPT_REFERER => $e
  142.             )
  143.         );
  144.        
  145.         return array(
  146.             "data"      => explode("\r\n\r\n", curl_exec($ch), 2),
  147.             "httpcode"  => (int)curl_getinfo($ch, CURLINFO_HTTP_CODE)
  148.         );
  149.     }
  150. } new ShoutCastAPI();
  151. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement