Skip to content

Instantly share code, notes, and snippets.

@elzup
Created August 11, 2014 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elzup/e9bac68ac14120395096 to your computer and use it in GitHub Desktop.
Save elzup/e9bac68ac14120395096 to your computer and use it in GitHub Desktop.
<?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