iChat to Adium Chat Transcript Converter


/ Published in: PHP
Save to your folder(s)

This is kind of a sloppy way of converting iChat transcripts to Adium transcripts.

To get this to work, export your chats using [Logorrhea](http://spiny.com/logorrhea/) (`File > Export Chats...`) then use the exported file as the `$infile` for this script. Also, change `$myscreenname` and `$myname` to your screenname and name, respectively. Finally, run the script in Terminal (`php this_script.php`).

**Note:** For this script to work, you must have PHP, PEAR, and PEAR's Date module installed (type `sudo pear install Date` in Terminal).

Sorry that this is a pain in the ass to get working. I wrote it very quickly for personal use and thought others might get some use out of it.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // This is the most inefficient piece of shit you will ever see
  4.  
  5. require_once('Date.php');
  6.  
  7. $base = getcwd();
  8. $infile = $base . '/iChat Export.txt';
  9. $outdir = $base . '/Converted Transcripts';
  10. $myscreenname = 'theshane100';
  11. $myname = 'Shaun Chapman';
  12. $abort = false;
  13.  
  14. // Create the output directory if it doesn't exist
  15. if (!file_exists($outdir)) {
  16. mkdir($outdir);
  17. }
  18.  
  19. $file = file_get_contents($infile);
  20. $lines = explode("\r", $file);
  21.  
  22. $last_alias = '';
  23.  
  24. foreach ($lines as $line) {
  25. $pieces = explode("\t", $line);
  26. $count = count($pieces);
  27. $valid = false;
  28.  
  29. if ($count == 5) {
  30. $valid = true;
  31. } elseif ($count == 4) {
  32. // Likely a return
  33. $pieces[4] = "\n";
  34. $valid = true;
  35. }
  36.  
  37. if ($valid) {
  38. $alias = $pieces[0];
  39. $info['sender'] = $pieces[1];
  40. $info['time'] = date('Y-m-d', strtotime($pieces[2])) . "T{$pieces[3]}-05:00";
  41. $info['message'] = $pieces[4];
  42.  
  43. if ($info['sender'] == $myscreenname) {
  44. $info['alias'] = $myname;
  45. } else {
  46. $info['alias'] = $alias;
  47. }
  48.  
  49. $alias_arr[$alias][] = $info;
  50. }
  51. }
  52.  
  53.  
  54. // Build the dictionary
  55. foreach ($alias_arr as $messages) {
  56. foreach ($messages as $message) {
  57. if (!isset($dictionary[$message['alias']])) {
  58. $dictionary[$message['alias']] = $message['sender'];
  59. } elseif ($dictionary[$message['alias']] != $message['sender']) {
  60. echo "Warning: {$message['alias']} matches with both {$dictionary[$message['alias']]} and {$message['sender']}.\n";
  61. //$abort = true;
  62. }
  63. }
  64. }
  65.  
  66. if ($abort) {
  67. echo "Aborting...\n";
  68. }
  69.  
  70. // This takes a long time
  71. foreach ($alias_arr as $alias => $messages) {
  72. $last_time = new Date();
  73. $log_num = 0;
  74.  
  75. foreach ($messages as $message) {
  76. $this_time = new Date($message['time']);
  77. $span = new Date_Span();
  78. $span->setFromDateDiff($last_time, $this_time);
  79.  
  80. if ((int)$span->toHours() >= 1) {
  81. $last_time = $this_time;
  82. $log_num++;
  83. }
  84.  
  85. $log_arr[$alias][$log_num][] = $message;
  86. }
  87. }
  88.  
  89.  
  90. // Write matched files
  91. foreach ($log_arr as $alias => $logs) {
  92. $matched_folder = "$outdir/Matched";
  93. $not_matched_folder = "$outdir/Not Matched";
  94.  
  95. if (!file_exists($matched_folder))
  96. mkdir($matched_folder);
  97.  
  98. if (!file_exists($not_matched_folder))
  99. mkdir($not_matched_folder);
  100.  
  101. foreach ($logs as $log) {
  102. // Find the other screenname
  103. $screenname = $myscreenname;
  104.  
  105. foreach ($log as $message) {
  106. if ($message['sender'] != $myscreenname) {
  107. $screenname = $message['sender'];
  108. }
  109. }
  110.  
  111. if ($screenname != $myscreenname) {
  112. $sn_folder = "$matched_folder/$screenname";
  113. } else {
  114. // Try to match the screenname
  115. if (!empty($dictionary[$alias])) {
  116. $screenname = $dictionary[$alias];
  117. echo "Matched $alias to $screenname.\n";
  118. $sn_folder = "$matched_folder/{$dictionary[$alias]}";
  119. } else {
  120. $screenname = '[Unknown]';
  121. echo "Could not find match for $alias.\n";
  122. $sn_folder = "$not_matched_folder/$alias";
  123. }
  124. }
  125.  
  126. if (!file_exists($sn_folder)) {
  127. mkdir($sn_folder);
  128. }
  129.  
  130. $start_time = $log[0]['time'];
  131. $end_time = $log[count($log) - 1]['time'];
  132. $log_folder = "$sn_folder/$screenname (" . str_replace(':', '.', str_replace('-05:00', '-0500', $start_time)) . ").chatlog";
  133. $log_file = "$log_folder/$screenname (" . str_replace(':', '.', str_replace('-05:00', '-0500', $start_time)) . ").xml";
  134. $output = '';
  135.  
  136. // Create log folder if it doesn't exist
  137. if (!file_exists($log_folder)) {
  138. mkdir($log_folder);
  139. }
  140.  
  141. $output .= '<?xml version="1.0" encoding="UTF-8" ?>' . "\r";
  142. $output .= "<!-- Converted from iChat Transcript -->\r";
  143. $output .= '<chat xmlns="http://purl.org/net/ulf/ns/0.4-02" account="' . $myscreenname . '" service="AIM">';
  144. $output .= '<event type="windowOpened" sender="' . $myscreenname . '" time="' . $start_time . '"/>' . "\r";
  145.  
  146. foreach ($log as $message) {
  147. $output .= '<message sender="' . $message['sender'] . '" time="' . $message['time'] . '" alias="' . $message['alias'] . '">';
  148. $output .= '<div><span style="font-family: Helvetica; font-size: 12pt;">';
  149. $output .= str_replace("\n", '', nl2br(htmlspecialchars($message['message'])));
  150. $output .= '</span></div></message>' . "\r";
  151. }
  152.  
  153. $output .= '<event type="windowClosed" sender="' . $myscreenname . '" time="' . $end_time . '"/>' . "\r";
  154. $output .= "</chat>\r";
  155.  
  156. // Write to file
  157. file_put_contents($log_file, $output);
  158. }
  159. }
  160.  
  161. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.