Skip to content

Instantly share code, notes, and snippets.

@tito10047
Created May 12, 2017 11:32
Show Gist options
  • Select an option

  • Save tito10047/cae9eca71b614355d03e86a5584fc66a to your computer and use it in GitHub Desktop.

Select an option

Save tito10047/cae9eca71b614355d03e86a5584fc66a to your computer and use it in GitHub Desktop.
download all songs from ANDREW HUANG https://www.youtube.com/user/songstowearpantsto
<?php
/**
* Created by PhpStorm.
* User: Jozef Môstka
* Date: 12.5.2017
* Time: 10:37
*/
ini_set('max_execution_time', 0);
set_time_limit(0);
$content = file_get_contents("http://www.suturesound.com/aim/huang/ex/");
$re = '/<a href="([^?\/].*)">.*<\/a>\s*(\d{2}-[A-z]{3}-\d{4} )/m';
preg_match_all($re, $content, $matches);
$fileNames = $matches[1];
$dates = $matches[2];
$months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
$fh=null;
register_shutdown_function(function() use ($fh){
if ($fh!==null){
fclose($fh);
}
});
foreach($fileNames as $pos=>$fileName){
echo $fileName.PHP_EOL;
$destZipPath = __DIR__ . "/zipFiles/{$fileName}";
$baseName = basename($fileName,'.zip');
$dateParts=explode("-",$dates[$pos]);
$month = array_search($dateParts[1],$months);
if ($month===false){
echo "ERROR: Not found month ({$dateParts[1]})".PHP_EOL;
exit;
}
$month++;
if ($month<10){
$month="0{$month}";
}
$year = trim($dateParts[2]);
$extractDir = __DIR__ . "/unziped/{$year}.{$month}.{$dateParts[0]}-{$baseName}/";
if (file_exists($destZipPath)){
echo "\texist - skiping".PHP_EOL;
continue;
}
if (strpos($fileName,"Mac-only")!==false){
echo "\tskiping".PHP_EOL;
continue;
}
$fh = fopen($destZipPath, 'w');
$ch = curl_init();
$lasNow=-200;
$dots=0;
curl_setopt($ch, CURLOPT_URL, "http://www.suturesound.com/aim/huang/ex/{$fileName}");
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,
function ($curl, $dltotal, $dlnow, $ultotal, $ulnow) use(&$lasNow, &$dots){
$mb=round(($dlnow/1024)/1024,2);
if ($lasNow+1<$mb){
$lasNow=$mb;
echo ".";
if ($dots++>120){
echo PHP_EOL;
$dots=0;
}
}
});
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
echo "\t.";
curl_exec($ch);
curl_close($ch);
fclose($fh);
$fh=null;
echo "".PHP_EOL;
$zip = new ZipArchive;
$res = $zip->open($destZipPath);
if ($res === TRUE) {
$zip->extractTo($extractDir);
$zip->close();
echo "\tUnziping to {$baseName}".PHP_EOL;
} else {
echo "\tERROR:cant unzip: ".$baseName.PHP_EOL;
}
tmMacOsDir($extractDir);
}
function tmMacOsDir($dir){
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir.DIRECTORY_SEPARATOR.$object) && $object=="__MACOSX") {
echo "\tremoving macosx dir".PHP_EOL;
rrmdir($dir . DIRECTORY_SEPARATOR . $object);
}
}
}
}
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object))
rrmdir($dir."/".$object);
else
unlink($dir."/".$object);
}
}
rmdir($dir);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment