Created
September 9, 2009 17:14
-
-
Save sotarok/183898 to your computer and use it in GitHub Desktop.
Download image from Tumblr
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 | |
/** | |
* Dowload image file from tumblr. | |
* | |
* @author sotarok | |
* @copyright 2009 sotarok | |
* @lisence The MIT License | |
*/ | |
define("BASE_DIR", dirname(__FILE__) . "/"); | |
if ($argc !== 3) { | |
echo <<<EOL | |
Usage: | |
% php TumblrDownload.php account_list size_list | |
account_list - list user id. comma separated. | |
size_lise - list sizes. 500, 250, 75 . comma separated. | |
Example: | |
% php TumblrDownload.php photo4u,nicepixs,carok,tooloot,lining,onanie 500 | |
EOL; | |
exit; | |
} | |
$account_list = explode(',', $argv[1]); | |
$download_max = 1000; | |
$download_num = 50; | |
$download_size = explode(',', $argv[2]); | |
echo "Tumblr download script start at " . date("Y-m-d H:i:s") . " ... \n\n"; | |
// Initialize Directory list | |
foreach ($download_size as $size) { | |
if (!is_dir(BASE_DIR . $size)) { | |
if (!mkdir(BASE_DIR . $size)) { | |
throw new Exception("directory make fault. " . BASE_DIR . $size ); | |
} | |
else { | |
echo "make directory. " . BASE_DIR . $size . "\n"; | |
} | |
} | |
else { | |
echo "directory exists. " . BASE_DIR . $size ."\n"; | |
} | |
} | |
foreach($account_list as $account) { | |
$url = "http://{$account}.tumblr.com/api/read?num={$download_num}&type=photo"; | |
foreach(range(0, $download_max / $download_num) as $start) { | |
$start_num = $start * $download_num; | |
$download_url = $url ."&start=" . $start_num; | |
$xml = simplexml_load_file($url ."&start=" . $start_num); | |
echo "xml download from $download_url \n"; | |
foreach ($xml->posts->post as $item) { | |
foreach ($item->{"photo-url"} as $img) { | |
$target = $img; | |
$psu = parse_url($target); | |
$psu = ltrim($psu['path'], "/"); | |
$save_dir = BASE_DIR . $img->attributes()->{"max-width"} ."/"; | |
$save_name = $save_dir . $psu; | |
if (file_exists($save_name)) { | |
echo " file exists. skip. $img -> " . basename($save_name) ." \n"; | |
} | |
else { | |
if (in_array($img->attributes()->{"max-width"}, $download_size)) { | |
$file = file_get_contents($target); | |
$fp = fopen($save_name, "wb"); | |
fwrite($fp, $file); | |
fclose($fp); | |
echo " save : $target \n"; | |
} | |
} | |
} | |
} | |
} | |
} | |
echo " ... finished at " . date("Y-m-d H:i:s") . " ... \n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment