Created
July 8, 2015 15:22
-
-
Save cyle/0b16c197b0d1952c5a3a to your computer and use it in GitHub Desktop.
Tumblr GIF Tagger
This file contains hidden or 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 gif tagger | |
goes through your tumblr photo posts, finds .gifs, and tags em #gif | |
so that they can show up in https://tumblr.com/tv/@your-blog-here | |
this was made to be run via command line, i.e. `php giftagger.php` | |
*/ | |
// your blog URL here~~ | |
$blog_name = 'your-blog.tumblr.com'; | |
/* | |
this uses the tumblr.php sdk: https://github.com/tumblr/tumblr.php | |
to install it, install composer and do something like `php composer.phar require tumblr/tumblr:*` | |
*/ | |
require 'vendor/autoload.php'; | |
/* | |
this also requires you to make an "app" on api.tumblr.com | |
because we'll be editing posts, and it needs your authorization to do so | |
you can fill in the following info from the tumblr API console | |
https://api.tumblr.com/console/ | |
*/ | |
$client = new Tumblr\API\Client( | |
'consumer_key_here', | |
'consumer_secret_here', | |
'token_here', | |
'token_secret_here' | |
); | |
// one stupid initial request to get the total photo posts | |
$photo_posts_info = $client->getBlogPosts($blog_name, array('type' => 'photo', 'limit' => 1)); | |
$total = $photo_posts_info->total_posts; | |
$limit = 20; // won't go higher than this | |
$pages = ceil($total/$limit); | |
echo $pages . ' pages of photo posts'."\n"; | |
for ($page = 0; $page < $pages; $page++) { | |
echo 'doing page #'.$page.' of '.$pages."\n"; | |
$posts = $client->getBlogPosts($blog_name, array('type' => 'photo', 'limit' => $limit, 'offset' => ($page * $limit))); | |
foreach ($posts->posts as $post) { | |
$current_tags = $post->tags; // array | |
if (in_array('gif', $current_tags)) { | |
continue; // already got it covered, keep going | |
} | |
$current_post_id = $post->id; | |
$current_url = $post->photos[0]->alt_sizes[0]->url; // string | |
$extension = strtolower(strrchr($current_url, '.')); | |
if ($extension == '.gif') { | |
echo '#'.$current_post_id.' is a gif, needs tag, adding!'."\n"; | |
$current_tags[] = 'gif'; | |
try { | |
$edit_post = $client->editPost($blog_name, $current_post_id, array('tags' => implode(', ', $current_tags))); | |
echo 'ADDED!'."\n"; | |
} catch (Tumblr\API\RequestException $e) { | |
echo 'ERROR! '.print_r($e, true)."\n"; | |
} | |
} | |
} | |
usleep(200000); // 200 millisecond wait so that we don't hit a rate limit | |
} | |
echo 'ALL DONE.'."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment