Created
August 11, 2014 06:26
-
-
Save elzup/e9bac68ac14120395096 to your computer and use it in GitHub Desktop.
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 | |
// Tumblrに投稿したTagの一括置き換えをする | |
/* action config */ | |
// 置換前の文字列 | |
$search = '-'; | |
// 置換後の文字列 | |
$replace = ' '; | |
// 完全一致のみ置換 -> TRUE, 部分置換 -> FALSE | |
$is_compmatch = FALSE; | |
// ブログ名 | |
$blog_name = 'elzup'; | |
// 取得するブログの投稿のオプション | |
$options = array( | |
'tag' => 'icon', | |
); | |
require_once('./vendor/autoload.php'); | |
require_once('./user.php'); | |
$client = new Tumblr\API\Client(CONSUMER_KEY, CONSUMER_SECRET); | |
$client->setToken(TOKEN, TOKEN_SECRET); | |
$offset = 0; | |
while (TRUE) { | |
echo PHP_EOL; | |
echo "[[$offset]]"; | |
$options['offset'] = $offset; | |
$offset += 20; | |
$res = $client->getBlogPosts($blog_name, $options); | |
// 処理終了 | |
if (empty($res->posts)) { | |
break; | |
} | |
foreach ($res->posts as $post) { | |
echo $post->post_url . PHP_EOL; | |
echo '[' . implode(',', $post->tags) . ']'; | |
// tagが空なら次の投稿へ | |
if (empty($post->tags)) { | |
continue; | |
} | |
$tags = array_replace_value($post->tags, $search, $replace, $is_compmatch); | |
// 置換が発生した場合 | |
if ($tags !== $post->tags) { | |
// 編集処理 | |
$tags_str = implode(',', $tags); | |
$data = array ( | |
'tags' => $tags_str, | |
); | |
$res = $client->editPost($blog_name, $post->id, $data); | |
var_dump($res); | |
echo "changed-> [{$tags_str}]\n"; | |
} else { | |
echo 'skip' . PHP_EOL; | |
} | |
echo PHP_EOL; | |
} | |
} | |
function array_replace_value($array, $search, $replace, $is_compmatch) { | |
foreach ($array as &$v) { | |
// 完全一致モードの場合 | |
if ($is_compmatch && $v == $search) { | |
$v = $replace; | |
} // 部分置換 | |
elseif (strpos($v, '-') !== FALSE) { | |
$v = str_replace($search, $replace, $v); | |
} | |
} | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment