Simple way to parse Weblogs.com feeds.

As an additional (read: useless) module to add to LiveJournal was quite easy, however WebLogs is not quite the same to parse with it’s semi-proprietary format.

The following trivial code snippit uses PHP’s (now) native XML routines to parse this format and make it human-readable. Feel free to steal, use, or adapt as you need – you’ll notice that it’s quite, ah… inelegant.

WebLogs Parsing Test
<?php
  parse_webblogs();
  // print_r($parsedlogs);
  while (list($number, $stuff) = each ($parsedlogs)) {
    echo "Entry $number: " . nicetime("$stuff[WHEN]") . " ago $stuff[NAME] [<a href='$stuff[URL]'>RSS</a>]<br />";
  }
  function nicetime($arg = "") {
    // Boy is this ugly.  I'll have to fix it later.
    if ($arg < 60)
      return $arg . "s";
    if ($arg > 60 && $arg < 3600)
      return round(($arg / 60)) . "m";
    if ($arg > 3600)
      return round(($arg / 3600)) . "h";
  }
  function parse_webblogs() {
    global $parsedlogs;
    $fp = @fopen('http://www.weblogs.com/rssUpdates/changes.xml', 'rB');
    if (! $fp)
      return false;
    $data = fread($fp, 1000000);
    $data = str_replace("\\r", '', $data);
    $data = str_replace("\\n", '', $data);
    $xp = xml_parser_create();
    xml_set_element_handler($xp, '_simple_xml_startElement', '_simple_xml_endElement');
    xml_parse($xp, $data, true);
    xml_parser_free($xp);
    return $parsedlogs;
  }
  function _simple_xml_startElement($xp, $element, $attr) {
    global $parsedlogs;
    if (strcasecmp('weblog', $element)) {
      return;
    }
    $parsedlogs[] = $attr;
  }
  function _simple_xml_endElement($xp, $element) {
    global $parsedlogs;
    return;
  }
?>