Created
August 9, 2011 03:11
-
-
Save take-cheeze/1133326 to your computer and use it in GitHub Desktop.
http://nanami-city.info/?e=885 http://mystia.tumblr.com/post/8614061413/tumblr の改良とか。s/Stauts/Status/とか存在するファイルを飛ばすのとか。quickモードの追加
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 | |
// ========================================================= | |
// | |
// * Lilium * | |
// | |
// Tumblrの画像を保存されている最大サイズで一括保存するためのアレ | |
// | |
// ========================================================= | |
// | |
// * Usage * | |
// | |
// php lilium.php -a [アカウント名] -d [保存ディレクトリ] (-q) | |
// - "-q"は任意オプション。quickの略。既にダウンロード済みのファイルをダウンロードしようとした場合スキップする代わりに終了する。 | |
// | |
// ■ 説明 ■ | |
// ■ アカウント名 | |
// http://ここんとこ.tumblr.com/ | |
// | |
// ■ 保存ディレクトリ | |
// 適当なPath | |
// | |
// ■ 参考例 ■ | |
// php lilium.php -a mystia -d ./download/ | |
// | |
// ========================================================= | |
// | |
// * History * | |
// | |
// 2011/08/08 - Release.(original) | |
// 2011/08/09 | |
// - Fix typo (Stauts -> Status) | |
// - Skip existing files in the output directory. | |
// - Add slash to the end of path though doesn't have it. | |
// 2011/08/10 | |
// - Add quick mode. | |
// | |
// ========================================================= | |
// | |
// * Etc * | |
// | |
// Developed by Setsuna Kamishiro. | |
// http://gplus.to/kamisetsu/ | |
// | |
// ========================================================= | |
// 基本設定 | |
ini_set ('error_reporting', E_ERROR); | |
ini_set ('user_agent', 'Lilium'); | |
// 個人設定 | |
$cmdopt = getopt ('a:d:q'); // コマンドライン引数を取得 | |
if ($cmdopt['a'] && $cmdopt['d']) { // 両方入力されていれば | |
$account = $cmdopt['a']; // アカウント名というかURLのアレ | |
$path = $cmdopt['d']; // 保存先のPATH | |
} else { // そうでなければエラーとヘルプを出して死ぬ | |
die ("Expects at least 2 parameter.\nUsase\t: lilium.php -a [Your Account] -d [Download destination path]\nExample\t: lilium.php -a mystia -d ./download/"); | |
} | |
// $pathの最後に'/'が無かった場合'/'を追加 | |
$path = ($path[strlen($path) - 1] != '/')? $path.'/' : $path; | |
// 基本APIコード生成 | |
$baseAPI = "http://{$account}.tumblr.com/api/read/json?type=photo"; | |
// ディレクトリチェック | |
if (!is_dir ($path)) { // ディレクトリが存在しなければ | |
if (!mkdir ($path, 777, true)) { // 作る | |
die ("ERROR : Save directory create failed.\n"); // 駄目だったら終わり | |
} | |
} | |
// 初回JSON読み込み | |
$jsonData = jsonFunc ($baseAPI); // 初回設定 | |
$saveFailedCnt = 0; // 取得失敗数初期化 | |
// 処理開始メッセージ | |
echo "Founded {$jsonData['posts-total']} images.\nGetting start!!\n"; | |
// 本処理 | |
for ($i = 0; $i < $jsonData['posts-total']; $i += 50) { // JSONのページング | |
$jsonData = jsonFunc ($baseAPI."&start={$i}&num=50"); // GET JSON | |
// JSON展開処理 | |
for ($j = 0; $j < 50; $j++) { | |
// ここでnullにしておくことで、file_get_contentsでstream errorが起きた時に$imgFileDataの値の使い回しが起きるのを防ぐ。 | |
$imgFileData = null; | |
// カウンターストップ(処理停止) | |
if ($i + $j >= $jsonData['posts-total']) { | |
$saveSuccusesCnt = $jsonData['posts-total'] - $saveFailedCnt; // 保存に成功した数を算出 | |
die ($saveSuccusesCnt." images saved!!\n".$saveFailedCnt." images save failed."); // 結果発表して死ぬ | |
} | |
// 収集状況をパーセンテージで表示(疑似的に+1して見栄え向上の処理を入れています) | |
$nowPercentage = sprintf ("%05.2f", (($i + $j + 1) / $jsonData['posts-total']) * 100); // 実際は0カウントから始まるけど、0カウントから始めると画像合計と処理合計がずれてアレなので1を加算しておく | |
echo "Status : ".$nowPercentage." % - (".($i + $j + 1)." / ".$jsonData['posts-total'].")\n"; // 上記理由で1を加算 | |
// URLのパース(1280サイズは拡張子がファイル名にないので付け足す、そうでないものはファイル名に拡張子が含まれるので付け足さない) | |
preg_match ('/[^\/]+$/', $jsonData['posts'][$j]['photo-url-1280'], $lastSlashMat); // 行端の / 以降を抽出する(実質ファイル名) | |
if (!preg_match ('/\.[^.]+$/', $lastSlashMat[0])) { // 行端の / 以降(実質ファイル名)に拡張子が含まれているかどうか | |
preg_match ('/\.([^.]+)$/', $jsonData['posts'][$j]['photo-url-75'], $mat); // 含まれていなければ75サイズのURLから拡張子を取得 | |
} else { // そうでなければ | |
$mat[1] = ''; // 保持している拡張子データを破棄 | |
} | |
// ファイルの保存 | |
$fileName = $mat[1] | |
? $path.$lastSlashMat[0].".".$mat[1] // PATH + ファイル名 + . + 拡張子 | |
: $path.$lastSlashMat[0] // PATH + ファイル名 | |
; | |
if(!file_exists ($fileName)) { | |
// ファイルのダウンロード | |
// stream errorが発生したとき、この関数は何も返さない(void状態になる) | |
$imgFileData = file_get_contents ($jsonData['posts'][$j]['photo-url-1280']); | |
if (!$imgFileData) { // stream errorの場合 | |
$saveFailedCnt++; // 失敗カウンタをインクリメントする | |
} else { // ダウンロードに成功 | |
// ファイルを保存 | |
file_put_contents ($fileName, $imgFileData); | |
} | |
} else if(isset($cmdopt['q'])) { return; } | |
} | |
} | |
// JSON paser for Tumblr | |
function jsonFunc ($baseAPI) { | |
$jsonData = file_get_contents ($baseAPI); // JSON取得 | |
$jsonData = str_replace('var tumblr_api_read = ', '', $jsonData); // ヘッダ除去 | |
$jsonData = str_replace(';', '', $jsonData); // 余計な文字の削除 | |
return json_decode ($jsonData, true); // 配列に格納 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment