Last active
March 15, 2017 17:13
-
-
Save necrogami/09d2f1ee36924c237f02c501edb1ceac to your computer and use it in GitHub Desktop.
Tumblr Post Downloader
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 | |
| ini_set('memory_limit', '-1'); | |
| require 'vendor/autoload.php'; | |
| use Tumblr\API\Client; | |
| $dotenv = new Dotenv\Dotenv(__DIR__); | |
| $dotenv->load(); | |
| $consumerKey = getenv('CONSUMER_KEY'); | |
| $consumerSecret = getenv('CONSUMER_SECRET'); | |
| $token = getenv('TOKEN'); | |
| $tokenSecret = getenv('TOKEN_SECRET'); | |
| $client = new Client($consumerKey, $consumerSecret); | |
| $client->setToken($token, $tokenSecret); | |
| $blogs = $client->getFollowedBlogs(); | |
| $blogset = $blogs->blogs; | |
| for ($i=20; $i < $blogs->total_blogs;) { | |
| $current = $client->getFollowedBlogs(['offset' => $i]); | |
| $blogset = array_merge($blogset, $current->blogs); | |
| $i = $i+20; | |
| } | |
| $blogs = []; | |
| foreach ($blogset as $blog) { | |
| if (!in_array($blog->name, $blogs)) { | |
| $blogs[] = $blog->name; | |
| } | |
| } | |
| foreach ($blogs as $blog) { | |
| echo $blog; | |
| $path = './json/'.$blog.'/'; | |
| if (!file_exists($path)) { | |
| mkdir($path, 0777, true); | |
| } | |
| $blogPostArray = $path.$blog.'.json'; | |
| if (!file_exists($blogPostArray)) { | |
| $empty = []; | |
| file_put_contents($blogPostArray, json_encode($empty)); | |
| } | |
| $blogPostData = json_decode(file_get_contents($blogPostArray), true); | |
| $info = $client->getBlogInfo($blog); | |
| echo ": " . $info->blog->posts . PHP_EOL; | |
| $posts = $client->getBlogPosts($blog)->posts; | |
| $exists = 0; | |
| foreach ($posts as $post) { | |
| $file = $path.$post->id.'.json'; | |
| if (!file_exists($file) && !in_array($post->id, $blogPostData)) { | |
| file_put_contents($file, json_encode($post)); | |
| $blogPostData[$post->id] = 0; | |
| } else { | |
| $exists++; | |
| } | |
| } | |
| echo "0,"; | |
| for ($i=20; $i < $info->blog->posts;) { | |
| if ($exists < 20) { | |
| $posts = $client->getBlogPosts($blog, ['offset' => $i])->posts; | |
| foreach ($posts as $post) { | |
| $file = $path.$post->id.'.json'; | |
| if (!file_exists($file) && !in_array($post->id, $blogPostData)) { | |
| file_put_contents($file, json_encode($post)); | |
| $blogPostData[$post->id] = 0; | |
| } else { | |
| $exists++; | |
| } | |
| } | |
| } | |
| echo " ".$i.","; | |
| $i = $i+20; | |
| } | |
| file_put_contents($blogPostArray, json_encode($blogPostData)); | |
| echo ' Done.' . PHP_EOL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment