Advertisement
Papadopolis

SA-MP Shoutcast API R2

Aug 4th, 2014
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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: R2 (released in 04/08/2014)
  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.     private $shoutcastUrlPrefix = 'http://www.shoutcast.com/';
  36.  
  37.     function __construct() {
  38.         $RequestValues = array(
  39.             "genre"     => ($_GET['genre'])?(int)$_GET['genre']:null,
  40.             "station"   => ($_GET['station'])?(int)$_GET['station']:null,
  41.             "type"      => ($_GET['type'])?(int)$_GET['type']:null,
  42.             "artist"    => ($_GET['artist'])?(int)$_GET['artist']:null,
  43.             "song"      => ($_GET['song'])?(int)$_GET['song']:null
  44.         );
  45.  
  46.         $validParams = false;
  47.         foreach($_GET as $ind => $val)
  48.             foreach($RequestValues as $searchInd => $searchVal)
  49.                 if($searchInd == $ind) {
  50.                     $validParams = true;
  51.                     break;
  52.                 }
  53.  
  54.         if(isset($_GET['getStreamUrl'])) {
  55.             $url = $this->shoutcastUrlPrefix. 'Player/GetStreamUrl';
  56.         } else if(count($_GET) > 0 && $validParams) {
  57.             $url = $this->shoutcastUrlPrefix. 'Search/UpdateAdvancedSearch';
  58.         } else {
  59.             $url = $this->shoutcastUrlPrefix. 'Home/Top';
  60.         }
  61.  
  62.         $count  = ($_GET['count'])?(int)$_GET['count']:10;
  63.         $start  = ($_GET['start'])?(int)$_GET['start']:0;
  64.         $show   = ($_GET['show'])?explode('|', $_GET['show']):false;
  65.  
  66.         $params = null;
  67.         foreach($RequestValues as $ind => $val) {
  68.             if($_GET[$ind] !== null)
  69.                 $RequestValues[$ind] = $_GET[$ind];
  70.  
  71.             $params .= $ind. "=". $RequestValues[$ind]. "&";
  72.         }
  73.  
  74.         $stations = array();
  75.  
  76.         $req = $this->Request($url, 1, null, $params, $this->shoutcastUrlPrefix);
  77.  
  78.         if(isset($_GET['getStreamUrl'])) die($req['data'][1]);
  79.  
  80.         $i = 0;
  81.  
  82.         $response = json_decode($req['data'][1]);
  83.  
  84.         foreach($response as $station) {
  85.             if(isset($_GET['streamUrl'])) {
  86.                 $stationUrlRequest = $this->Request($this->shoutcastUrlPrefix. 'Player/GetStreamUrl', 1, null, array('station' => $station->ID), $this->shoutcastUrlPrefix);
  87.                 preg_match('/"(.*?)"/', $stationUrlRequest['data'][2], $stationUrl);
  88.  
  89.                 $stations['stations']['s'. $i]['streamUrl'] = $stationUrl[1];
  90.             }
  91.  
  92.             foreach($station as $ind => $val)
  93.                 if($i >= $start && (in_array($ind, $show) || !$show))
  94.                     $stations['stations']['s'. $i][$ind] = $val;
  95.  
  96.             if($i > $count) break;
  97.             ++$i;
  98.         }
  99.  
  100.         $stations["stations"]["count"] = $i;
  101.  
  102.         if(@$vars = $_GET["show"]) {
  103.             $showOnly = explode("|", $vars);
  104.  
  105.             foreach($stations["stations"] as $stationId => $stationData)
  106.                 foreach($stationData as $ind => $val)
  107.                     if(!in_array($ind, $showOnly))
  108.                         unset($stations["stations"][$stationId][$ind]);
  109.         }
  110.  
  111.         switch(strtolower($_GET["format"])) {
  112.             case "json":
  113.                 $output = json_encode($stations);
  114.                 break;
  115.             case "dini":
  116.             default:
  117.                 $output = null;
  118.                 foreach($stations["stations"] as $stationId => $stationData)
  119.                     if(is_array($stationData))
  120.                         foreach($stationData as $ind => $val)
  121.                             $output .= $stationId. $ind. "=". $val. "\r\n";
  122.                     else
  123.                         $output .= $stationId. "=". $stationData. "\r\n";
  124.         }
  125.  
  126.         die($output);
  127.     }
  128.  
  129.     private function Request($a, $b, $c, $d, $e) {
  130.         $ch = curl_init();
  131.  
  132.         $exec = curl_setopt_array($ch, array(
  133.                 CURLOPT_URL => $a,
  134.                 CURLOPT_RETURNTRANSFER => $b,
  135.                 CURLOPT_HEADER => 1,
  136.                 CURLOPT_NOBODY => 0,
  137.                 CURLOPT_COOKIE => $c,
  138.                 CURLOPT_CUSTOMREQUEST => ($d)?"POST":"GET",
  139.                 CURLOPT_POSTFIELDS => $d,
  140.                 CURLOPT_FOLLOWLOCATION => 1,
  141.                 CURLOPT_TIMEOUT => 6,
  142.                 CURLOPT_REFERER => $e
  143.             )
  144.         );
  145.  
  146.         return array(
  147.             "data"      => explode("\r\n\r\n", curl_exec($ch), 3),
  148.             "httpcode"  => (int)curl_getinfo($ch, CURLINFO_HTTP_CODE)
  149.         );
  150.     }
  151. } new ShoutCastAPI();
  152. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement