#!/usr/bin/perl -w

##################################################
# merge RDF/RSS files
# Author: Shelley Powers
##################################################

use lib '.';
use strict;
use XML::RSS;
use HTML::Entities;

# read in list of RDF/RSS files
my $rdffile = "/home/shelleyp/www/work/cronapp/indexfiles.txt";
open(DAT, $rdffile) || die("could not open");
my @files=<DAT>;
close(DAT);

# how many items to include
my $total = 10;

# array for all RSS items
my %arry;

# read in each RDF/RSS file, load into array
foreach my $file (@files) {
  my $rss = new XML::RSS;

  $rss->parsefile($file);

   foreach my $item(@{$rss->{'items'}}) {
     my $dt = $item->{'dc'}->{'date'};
     $arry{$dt} = $item;
     }
}

# sort descending order by timestamp
my @keys = reverse(sort(keys %arry));

# create new RDF/RSS file
# create header
my $rss = new XML::RSS (version => '1.0');
$rss->channel(
   title        => "Burningbird Network",
   link         => "http://burningbird.net",
   description  => "Burningbird: Burning online since 1995",
   dc => {
     subject    => "writing,technology,art,photography,science,environment,politics",
     creator    => 'shelleyp@burningbird.net',
     publisher  => 'shelleyp@burningbird.net',
     rights     => 'Copyright 1995-2003, Shelley Powers, Burningbird',
     language   => 'en-us',
   },
   syn => {
     updatePeriod     => "hourly",
     updateFrequency  => "1",
     updateBase       => "1901-01-01T00:00+00:00",
   },
 );

 $rss->image(
   title  => "Burningbird",
   url    => "http://burningbird.net/mm/birdflame.gif",
   link   => "http://burningbird.net/",
   dc => {
     creator  => "Shelley Powers",
   },
 );

# add items
my $i = 0;

while ($i < $total) {
  my $key = $keys[$i];

  # build new RSS item
  $rss->add_item(
     title        => encode_entities($arry{$key}->{'title'}),
     description  => encode_entities($arry{$key}->{'description'}),
     link         => $arry{$key}->{'link'},
     dc => {
         subject  => $arry{$key}->{'dc'}->{'subject'},
         creator  => $arry{$key}->{'dc'}->{'creator'},
         date     => $arry{$key}->{'dc'}->{'date'},
     },
  );
  $i++;
}
$rss->save('/home/shelleyp/www/index.rdf');
