Skip to content

Instantly share code, notes, and snippets.

@mircobabini
Last active December 18, 2015 17:28
Show Gist options
  • Select an option

  • Save mircobabini/5818345 to your computer and use it in GitHub Desktop.

Select an option

Save mircobabini/5818345 to your computer and use it in GitHub Desktop.
Download mp4 undownloadable from mixcloud. Not so lawful, use with care.
<?
/**
* Prints the 'wget' cmd you must run into a shell in order to direct download the mp4 track
* @author Mirco Babini <mirkolofioⓐgmail.com>
*/
$uris = array ('http://www.mixcloud.com/a-long-track/on-mixcloud/');
$cmd = '';
foreach ($uris as $uri) {
list (,,,, $filename) = explode ('/', $uri);
$dd = get_mixcloud_ddownload ($uri);
if ($dd === false)
continue;
$cmd .= "wget -c -O {$filename}.m4a {$dd} && ";
}
echo substr ($cmd, 0, -4) . "\n";
function get_mixcloud_ddownload ($uri) {
$parsed = parse_url ($uri);
$path = $parsed['path'];
$html = file_get_contents ($uri);
$ltarget = 'data-url="'. $path .'"';
$starget = 'data-preview-url="';
$etarget = '"';
$lpos = strpos ($html, $ltarget);
$spos = strpos ($html, $starget, $lpos) + strlen ($starget);
$epos = strpos ($html, $etarget, $spos);
$preview = substr ($html, $spos, $epos-$spos);
$parsed = parse_url ($preview);
$pre = 'http://stream';
$post = '.mixcloud.com/cloudcasts/m4a/64/';
list (,, $path) = explode ('/', $parsed['path'], 3);
$path = str_replace ('.mp3', '.m4a', $path);
$found = false;
for ($streami = 0; $streami < 30; $streami++) {
$supposed = $pre.$streami.$post.$path;
$headers = @get_headers ($supposed);
if ($headers[0] === 'HTTP/1.1 200 OK') {
$found = true;
break;
}
}
if ($found) {
return $supposed;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment