-
-
Save rebhichaouki/bbfe8868d3ae467d291098bfc89f8bf2 to your computer and use it in GitHub Desktop.
Soundcloud playlist download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$baseDir = "mp3s"; | |
$soundcloudURL = "http://soundcloud.com/ninja-tune/sets/solid-steel-radio-shows/"; | |
$urlBits = str_replace('http://', '', $soundcloudURL); | |
$urlBits = preg_replace('@/$@', '', $urlBits); | |
$urlBits = explode("/", $urlBits); | |
/** | |
* http://soundcloud.com/ninja-tune/sets/solid-steel-radio-shows/ | |
* Array | |
* ( | |
* [0] => soundcloud.com | |
* [1] => ninja-tune | |
* [2] => sets | |
* [3] => solid-steel-radio-shows | |
* ) | |
*/ | |
array_shift($urlBits); | |
$storage_dir = $baseDir . "/" . implode("/", $urlBits); | |
if (!file_exists($storage_dir)) | |
{ | |
mkdir($storage_dir, 0755, true); | |
} | |
$playListFile = $storage_dir . "/" . $urlBits[count($urlBits) - 1] . ".m3u"; | |
if (file_exists($playListFile)) | |
{ | |
unlink($playListFile); | |
} | |
$playList = array(); | |
$file = getPage($soundcloudURL); | |
if (!$file) die ("Can't get SoundCloud page\n"); | |
$rows = explode("\n", $file); | |
$uris = array(); | |
$pattern = '@"/.+download"@i'; | |
$baseURL = 'http://soundcloud.com'; | |
foreach ($rows as $row) | |
{ | |
preg_match($pattern, $row, $matches); | |
if (count($matches) > 0) | |
{ | |
$uris[] = str_replace("\"", '', $matches[0]); | |
} | |
} | |
$i = 0; | |
$uriCount = count($uris); | |
foreach ($uris as $uri) | |
{ | |
$i++; | |
echo "\nFetching file " . $i . " of " . $uriCount . "\n"; | |
$uriHeaders = get_headers($baseURL . $uri); | |
foreach ($uriHeaders as $header) | |
{ | |
preg_match("/^Content\-Disposition\: attachment\;filename=\"(.*)\"$/", $header, $matches); | |
if (isset($matches[1])) | |
{ | |
$outputFilename = $matches[1]; | |
break; | |
} | |
} | |
$playList[] = $outputFilename; | |
if (file_exists($storage_dir . "/" . $outputFilename)) | |
{ | |
echo "File exists, skipping.\n"; | |
continue; | |
} | |
$command = "wget \"$baseURL$uri\" -O \"$storage_dir/$outputFilename\""; | |
`$command`; | |
} | |
echo "\nWriting play list\n"; | |
file_put_contents($playListFile, implode("\n", array_reverse($playList)), FILE_APPEND); | |
echo "\nDone\n"; | |
function getPage($url, $referer = false, $timeout = 30, $header = false) | |
{ | |
$curl = curl_init(); | |
if(strstr($referer,"://")){ | |
curl_setopt ($curl, CURLOPT_REFERER, $referer); | |
} | |
curl_setopt ($curl, CURLOPT_URL, $url); | |
curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout); | |
curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5))); | |
curl_setopt ($curl, CURLOPT_HEADER, (int)$header); | |
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0); | |
$html = curl_exec ($curl); | |
curl_close ($curl); | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment