Volkova

PHP Soundcloud Downloader V0.9

Jun 29th, 2011
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit(0);
  4. //error_reporting(0);
  5.  
  6. function get_title($soundcloud_link, $data) {
  7.     $doc=new DOMDocument();
  8.     $doc->loadHTML($data);
  9.     $xml=simplexml_import_dom($doc);
  10.  
  11.     $title=$xml->xpath('//em');
  12.     return $title[0];
  13. }
  14.  
  15. function get_id($soundcloud_link, $data) {
  16.     $doc=new DOMDocument();
  17.     $doc->loadHTML($data);
  18.     $xml=simplexml_import_dom($doc);
  19.     $images=$xml->xpath('//img');
  20.  
  21.     foreach ($images as $img) {
  22.         if ($img['class'] == "waveform") {
  23.             $final_id = $img['src'];
  24.             break;
  25.         }
  26.     }
  27.  
  28.     $final_id = end(explode('/', $final_id));
  29.     $final_id = current(explode('_', $final_id));
  30.  
  31.     return "http://media.soundcloud.com/stream/" . $final_id;
  32. }
  33.  
  34. function savefile($id, $title) {
  35.     $dir = "files";
  36.  
  37.     $file = file_get_contents($id);
  38.  
  39.     $thetitle = "";
  40.  
  41.     $titlelen = strlen($title) - 2;
  42.  
  43.     $thetitle = substr($title, 1, $titlelen);
  44.  
  45.     $thetitle = str_replace(" ", "_", $thetitle);
  46.  
  47.     $filename = $dir . "/" . $thetitle . ".mp3";
  48.  
  49.     file_put_contents("$filename", $file);
  50.  
  51.     header('Pragma: public');
  52.     header('Expires: 0');
  53.     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  54.     header('Cache-Control: public');
  55.     header('Content-type: application/force-download');
  56.     header('Content-Disposition: attachment; filename='.basename($filename).";" );
  57.     header("Content-Transfer-Encoding: binary");
  58.     header('Content-Length: '.filesize("$filename"));
  59.     readfile("$filename");
  60.  
  61. }
  62.  
  63. if ( $_POST ) {
  64.     $soundcloud_link = $_POST['l'];
  65.  
  66.     $data = file_get_contents($soundcloud_link);
  67.  
  68.     $id = get_id($soundcloud_link,$data);
  69.     $title = get_title($soundcloud_link,$data);
  70.  
  71.  
  72.     savefile($id, $title);
  73. } else {
  74.     print <<<SOUND_FORM
  75. <form method="post" ">
  76.  
  77.     <label for="name">Soundcloud Link: </label><br />
  78.     <input name="l" id="l" type="text" maxlength="150" /><br>
  79.  
  80.     <input type="submit" value="Submit" />
  81. </form>
  82. SOUND_FORM;
  83. }
  84.  
  85. ?>
Advertisement
Add Comment
Please, Sign In to add comment