class.soundcloud.php
 <?php
  /** 
  * Con esta clase puedes obtener enlaces directos desde souncloud usando su API.
  * Con solo asignar la URL ya sea una playlist o un song devolvera los links o solamente el link.
  * Tambien podemos descargar los archivos.
  * 
  * @author arthusu <arthusu@gmail.com> 
  * 
  * @package SoundCloud Downloader
  * 
  * @copyright 2015 arthusu
  */
 class Soundcloud 
 {
  const STREAM = 'http://media.soundcloud.com/stream/';
  const CLIENT_API = 'AquiPonesTuApi';
  /** @var string URL para sacar JSON */
  private $api = 'http://api.soundcloud.com/resolve.json?url=URL&client_id=';
  /** @var string El identificador del SONG */
  private $id;
  /** @var string La URL del SONG o PLAYLIST */
  private $url;
  /** @var array Nombres de los SONGS */
  private $names = array();
  /**  @var string Almacena el JSON retornado */
  private $json;
  /** @var array LINKS */
  private $playlist = array();
  /**
  * Obtiene el/los titulos del Song.
  * 
  * @return array Devuelve los titulos
  */
  public function getTitle(){
   return $this->names;
  } 
  /**
  * Obtiene el JSON de la URL que le asignes, Usamos la API para obtener la informacion.
  * 
  * @param string $url La URL a retornar.
  * 
  * @return void
  */
  public function getURL($url){
   $this->url = $url;
   $this->api = $this->api . self::CLIENT_API;
   $this->json = json_decode(file_get_contents(str_replace("URL", $this->url, $this->api)));
  }
  /**
  * Obtiene el ID del JSON arrojado por la URL, el ID del SONG
  * 
  * @return void
  */
  public function getID(){
   if(!empty($this->json)){
    $this->id = $this->json->{'waveform_url'};
    array_push($this->names,$this->json->{'title'});
    $this->id = substr(parse_url($this->id, PHP_URL_PATH),1,12);
   }
  }
  /**
  * Obtiene el LINK DIRECTO de la SONG 
  * 
  * @see Soundcloud::getID()
  * 
  * @return string Devuelve la URL directa del archivo.
  */
  public function getSong(){
   self::getID();
   return self::STREAM . $this->id;
  }
  /**
  * Obtiene los LINKS DIRECTOS de la PLAYLIST
  * 
  * @see Soundcloud::getID()
  * 
  * @return array devuelve un array de links.
  */
  public function getPlaylist(){
   foreach($this->json as $obj){
    if(is_array($obj)){
     foreach($obj as $arr){
      $this->json = $arr;
      self::getID();
      array_push($this->playlist,self::STREAM . $this->id);
     }
    }
   }
   return $this->playlist;
  }
  /**
  * Descarga el archivo de La URL dada 
  * 
  * @param string $url La URL para descargar el archivo.
  * 
  * @return void
  */
  public function downloadArchive($url){
    header("Content-type: audio/mpeg"); 
    header("Content-Disposition: attachment; filename=\"".uniqid(md5(rand())) .".mp3\""); 
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: 0"); // Fecha en el pasado
    header('Content-Description: File Transfer');
          header('Content-Transfer-Encoding: binary');
          header('Pragma: public');
    readfile($url); 
  }
 }
?>
El siguiente es un ejemplo de uso rapido:
soundcloud_downloader.php
 <?php
 set_time_limit(0);
 require_once('class.soundcloud.php');
 $soundcloud = new SoundCloud();
?>
<title>SoundCloud Downloader</title>
<style type="text/css">
 body{
  width:800px;
  margin-left: auto;
  margin-right: auto;
  text-align: center;
 }
 .right{
  width: 300px;
  padding-left: 10px;
  float:right;
  margin:10px;
  height: 30px;
 }
 .left{
  width: 300px;
  padding-right: 10px;
  float:left;
  margin:10px;
  height: 30px;
 }
 .clear{
  clear: both;
 }
</style>
<div class="clear">
<h1>SoundCloud Downloader</h1>
<form action="" method="post">
 <p>URL: <input type="text" name="url" size="100" /></p>
 <p><input type="submit" name="enviar" /></p>
</form>
</div>
<?php
 
 $playlist = "";
 if(isset($_GET["url"])){
  if(file_exists($_GET['url'])){
   echo "Tramposin";
  }else{
   $soundcloud->downloadArchive($_GET["url"]);
   exit();
  }
 }
 if(isset($_POST['enviar']) && filter_var($_POST['url'], FILTER_VALIDATE_URL)){
  
  $soundcloud->getURL($_POST['url']);
  if(stristr($_POST['url'],"sets")){
   $playlist = "on";
  }else{
   $playlist = "off";
  }
  echo '<table border="1">';
  echo '<tr>';
  switch ($playlist) {
   case 'on':
    echo '<div class="right">';
    foreach($soundcloud->getPlaylist() as $url){
      echo '<a href="?url='.$url.'">Download</a>';
      echo '<audio controls preload>
      <source src="'.$url.'" type="audio/ogg">
      Tu navegador no soporta la etiqueta audio.
      </audio><br /><hr />
      ';
    }
    echo '</div>'; 
    echo '<div class="left">';
    foreach ($soundcloud->getTitle() as $title) {
     echo $title .'<br /><hr />';
    }
    echo '</div>';
    break;
   case 'off':
    $url = $soundcloud->getSong();
    echo '<div class="right">';
    echo '<a href="?url='.$url.'">Download</a>';
    echo ' 
     <audio controls>
     <source src="'.$url.'" type="audio/ogg">
     Tu navegador no soporta la etiqueta audio.
     </audio><hr />
    ';
    echo '</div>'; 
    echo '<div class="left">';
    foreach ($soundcloud->getTitle() as $title) {
     echo $title ."<hr />";
    }
    echo '</div>';
    break;
   default:
     echo "ERROR";
    break;
  }
  echo '</table>';
 }
?>
Y bueno esta no es una entrada explicativa ni nada de eso solo dejare esto aqui y me iré, saludos!. 
No hay comentarios:
Publicar un comentario