Return to Snippet

Revision: 12896
at March 31, 2009 17:50 by shaunchapman


Initial Code
<?php

  // This is the most inefficient piece of shit you will ever see

  require_once('Date.php');

  $base = getcwd();
  $infile = $base . '/iChat Export.txt';
  $outdir = $base . '/Converted Transcripts';
  $myscreenname = 'theshane100';
  $myname = 'Shaun Chapman';
  $abort = false;

  // Create the output directory if it doesn't exist
  if (!file_exists($outdir)) {
    mkdir($outdir);
  }

  $file = file_get_contents($infile);
  $lines = explode("\r", $file);

  $last_alias = '';

  foreach ($lines as $line) {
    $pieces = explode("\t", $line);
    $count = count($pieces);
    $valid = false;

    if ($count == 5) {
      $valid = true;
    } elseif ($count == 4) {
      // Likely a return
      $pieces[4] = "\n";
      $valid = true;
    }

    if ($valid) {
      $alias = $pieces[0];
      $info['sender'] = $pieces[1];
      $info['time'] = date('Y-m-d', strtotime($pieces[2])) . "T{$pieces[3]}-05:00";
      $info['message'] = $pieces[4];

      if ($info['sender'] == $myscreenname) {
        $info['alias'] = $myname;
      } else {
        $info['alias'] = $alias;
      }

      $alias_arr[$alias][] = $info;
    }
  }


  // Build the dictionary
  foreach ($alias_arr as $messages) {
    foreach ($messages as $message) {
      if (!isset($dictionary[$message['alias']])) {
        $dictionary[$message['alias']] = $message['sender'];
      } elseif ($dictionary[$message['alias']] != $message['sender']) {
        echo "Warning:  {$message['alias']} matches with both {$dictionary[$message['alias']]} and {$message['sender']}.\n";
        //$abort = true;
      }
    }
  }

  if ($abort) {
    echo "Aborting...\n";
    exit;
  }

  // This takes a long time
  foreach ($alias_arr as $alias => $messages) {
    $last_time = new Date();
    $log_num = 0;

    foreach ($messages as $message) {
      $this_time = new Date($message['time']);
      $span = new Date_Span();
      $span->setFromDateDiff($last_time, $this_time);

      if ((int)$span->toHours() >= 1) {
        $last_time = $this_time;
        $log_num++;
      }

      $log_arr[$alias][$log_num][] = $message;
    }
  }


  // Write matched files
  foreach ($log_arr as $alias => $logs) {
    $matched_folder = "$outdir/Matched";
    $not_matched_folder = "$outdir/Not Matched";

    if (!file_exists($matched_folder))
      mkdir($matched_folder);

    if (!file_exists($not_matched_folder))
      mkdir($not_matched_folder);

    foreach ($logs as $log) {
      // Find the other screenname
      $screenname = $myscreenname;

      foreach ($log as $message) {
        if ($message['sender'] != $myscreenname) {
          $screenname = $message['sender'];
        }
      }

      if ($screenname != $myscreenname) {
        $sn_folder = "$matched_folder/$screenname";
      } else {
        // Try to match the screenname
        if (!empty($dictionary[$alias])) {
          $screenname = $dictionary[$alias];
          echo "Matched $alias to $screenname.\n";
          $sn_folder = "$matched_folder/{$dictionary[$alias]}";
        } else {
          $screenname = '[Unknown]';
          echo "Could not find match for $alias.\n";
          $sn_folder = "$not_matched_folder/$alias";
        }
      }

      if (!file_exists($sn_folder)) {
        mkdir($sn_folder);
      }

      $start_time = $log[0]['time'];
      $end_time = $log[count($log) - 1]['time'];
      $log_folder = "$sn_folder/$screenname (" . str_replace(':', '.', str_replace('-05:00', '-0500', $start_time)) . ").chatlog";
      $log_file = "$log_folder/$screenname (" . str_replace(':', '.', str_replace('-05:00', '-0500', $start_time)) . ").xml";
      $output = '';

      // Create log folder if it doesn't exist
      if (!file_exists($log_folder)) {
        mkdir($log_folder);
      }

      $output .= '<?xml version="1.0" encoding="UTF-8" ?>' . "\r";
      $output .= "<!-- Converted from iChat Transcript -->\r";
      $output .= '<chat xmlns="http://purl.org/net/ulf/ns/0.4-02" account="' . $myscreenname . '" service="AIM">';
      $output .= '<event type="windowOpened" sender="' . $myscreenname . '" time="' . $start_time . '"/>' . "\r";

      foreach ($log as $message) {
        $output .= '<message sender="' . $message['sender'] . '" time="' . $message['time'] . '" alias="' . $message['alias'] . '">';
        $output .= '<div><span style="font-family: Helvetica; font-size: 12pt;">';
        $output .= str_replace("\n", '', nl2br(htmlspecialchars($message['message'])));
        $output .= '</span></div></message>' . "\r";
      }

      $output .= '<event type="windowClosed" sender="' . $myscreenname . '" time="' . $end_time . '"/>' . "\r";
      $output .= "</chat>\r";

      // Write to file
      file_put_contents($log_file, $output);
    }
  }

?>

Initial URL


Initial Description
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.

Initial Title
iChat to Adium Chat Transcript Converter

Initial Tags


Initial Language
PHP