migrating jaiku to identi.ca or twitter

i recently decided to move my tech tips microblog to identi.ca (the original copy was on jaiku), as i felt it was a little more befitting, actively developed, etc (although jaiku is now open source).

anyway… so i wanted to migrate my posts over - so i wrote a php script to do it (it assumes your jaiku is public and reads it without hassling with oauth).

<?php
$sleepTime = 5;
$jaikuSource = "http://username.jaiku.com/json";
 
$mode = 'identi.ca';
$baseStatusUrl = 'http://identi.ca/api/statuses/update.json';
 
// thanks, php-twitter
if ($mode == 'twitter'){
   $baseStatusUrl = 'http://twitter.com/statuses/update.json';
   $headers = array('Expect:', 'X-Twitter-Client: ',
      'X-Twitter-Client-Version: ','X-Twitter-Client-URL: ');
}
 
$ctr = 0;
$entries = array();
 
print "destination account username: ";
$username = trim(fgets(STDIN));
print "password: ";
system('stty -echo');
$password = trim(fgets(STDIN));
system('stty echo');
print "\n";
 
$done = false;
$params = '';
while (true){
   $count = 0;
   $posts = fetchUrl($jaikuSource . $params);
   $json = json_decode($posts, true);
   $stream = $json['stream'];
   $lastEntry = null;
   foreach ($stream as $entry){
      if (isset($entry['comment_id'])) continue;
      $lastEntry = $entry;
      $count++;
      $entries[$ctr++] = $entry['title'];
   }
   if ($count == 0) break;
   $lastPostTime = $lastEntry['created_at'];
   $ts = split('-', $lastPostTime);
   $hd = split('T', $ts[2]);
   $min = split('Z', $ts[4]);
   $gmtime = gmmktime($hd[1], $ts[3], $min[0], $ts[1], $hd[0], $ts[0]) - 1;
   $params = "?offset=$gmtime";
}
 
for ($i = $ctr-1; $i>=0; $i--){
   $params = array('status' => $entries[$i]);
   if ($i != ($ctr-1)){
      print "sleeping $sleepTime seconds\n";
      sleep($sleepTime);
   }
   twitterApiCall($baseStatusUrl, $params);
   print "updated status to: " . $entries[$i] . "\n";
}
 
function fetchUrl($url){
   $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $resp = curl_exec($ch);
   curl_close($ch);
   return $resp;
}
 
function twitterApiCall($url, $args = null){
   global $username, $password, $headers;
 
   // thanks, php-twitter
   $ch = curl_init($url);
   if (!is_null($args)){
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
   }
   if ((!empty($username)) && (!empty($password)))
      curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   if (!empty($headers))
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   $resp = curl_exec($ch);
   $info = curl_getinfo($ch);
   curl_close($ch);
   if ($info['http_code']!=200)
      print "error - got an http code of: " . $info['http_code'] . "\n";
}

make sure you edit $baseStatusUrl and $mode as necessary. enjoy!

comments powered by Disqus