Skip to content

Instantly share code, notes, and snippets.

@kenmickles
Created August 24, 2011 18:20
Show Gist options
  • Save kenmickles/1168754 to your computer and use it in GitHub Desktop.
Save kenmickles/1168754 to your computer and use it in GitHub Desktop.
Quick and dirty script to copy data from one Tumblr to another.
<?php
/**
* Quick and dirty script to copy data from one Tumblr to another.
* Useful for setting up a test blog for theme development.
*
* @author Ken Mickles
*/
// the blog to copy from
define('TUMBLR_SRC', $_ENV['TUMBLR_SRC']);
// the blog to copy to
define('TUMBLR_BLOG', $_ENV['TUMBLR_BLOG']);
// login data
define('TUMBLR_EMAIL', $_ENV['TUMBLR_EMAIL']);
define('TUMBLR_PASSWORD', $_ENV['TUMBLR_PASSWORD']);
// turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// fetch tumblr data
$url = 'http://'.TUMBLR_SRC.'.tumblr.com/api/read/json';
$response = file_get_contents($url);
if ( empty($response) ) {
die("Received empty response from Tumblr!\n");
}
// parse JSON
$response = trim(preg_replace('/^var tumblr_api_read = /', '', trim($response)), ';');
$data = json_decode($response, 1);
// process posts
foreach ( $data['posts'] as $post ) {
// see http://www.tumblr.com/docs/en/api#api_write for details
$url = 'http://www.tumblr.com/api/write';
$post_data = array(
'email' => TUMBLR_EMAIL,
'password' => TUMBLR_PASSWORD,
'group' => TUMBLR_BLOG,
'type' => $post['type'],
'generator' => "Tumblrtestr",
'date' => $post['date'],
);
switch ( $post['type'] ) {
case 'regular':
$post_data['title'] = $post['regular-title'];
$post_data['body'] = $post['regular-body'];
break;
case 'photo':
$post_data['source'] = $post['photo-url-500'];
$post_data['caption'] = $post['photo-caption'];
$post_data['click-through-url'] = $post['url'];
break;
case 'quote':
$post_data['quote'] = $post['quote-text'];
$post_data['source'] = $post['quote-source'];
break;
case 'link':
$post_data['name'] = $post['link-text'];
$post_data['url'] = $post['link-url'];
$post_data['description'] = $post['link-description'];
break;
case 'conversation':
case 'video':
case 'audio':
// skipped cause jacki doesn't have these
break;
}
// make the request
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$response = curl_exec($ch);
curl_close($ch);
// if we got a post_id back, it worked. add the url to the list
if ( is_numeric($response) ) {
echo "Created new post: {$response}\n";
}
else {
echo "Tumblr API error: {$response}\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment