#!/usr/local/bin/perl -w

#dan's perl rss aggregator from http://ilrt.org/discovery/2001/05/rss-agg
#hacked to use my photo feeds feeds

#use strict;

use XML::RSS;
use LWP::Simple;

my $dir="http://www.ilrt.bristol.ac.uk/discovery/2003/03/rss/";

my $content;

my @remote = (	
		$dir . "2003.rss",
		$dir . "2002.rss",
		$dir . "2001.rss",
		$dir . "2000.rss"
		);


# TODO: use eval () to recover from XML::RSS errors

print header();

print "last update: ".`date`."<br />"; #sigh, can't be bothered to perlman

print <<HTML;  
<table> 
HTML

my @todo=@remote;

print "<div align=\"center\">\n";

my $verbo = 'full'; # verbosity
while  (@todo) {
  my $lhs = shift @todo;
#  my $mid = shift @todo;
  my $rhs = shift @todo;
  print "<tr>";
  cell($lhs , $verbo );
#  cell($mid , $verbo );
  cell($rhs, $verbo );
  print "</tr>";
}

print "</div>\n\n";

sub cell {
  my $uri=shift;
  my $full =shift;
  if (!$uri) { return; }
  my $rss = new XML::RSS;

#  eval (" 
  my $content = get($uri) or die($!);
 
  $rss->parse($content);

  print "<td valign=\"top\">\n";
  print_html($rss , $full);
  print "</td>\n";
  return '';
}

print <<HTML;
</tr>
</table>
HTML


print footer();


sub header {
my $verbosity = shift;

my $sourcefiles="";

my $count=0;
foreach $a (@remote){
$sourcefiles=$sourcefiles . " \n" . $a ;
	if($count<$#remote){
	$sourcefiles=$sourcefiles . ", ";
	}
$count++;
}

return <<HTML;
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style type="text/css">
/*<![CDATA[*/
body {
  margin-left: 10%;
  margin-right: 10%;
  background: white;
  color: black;
}
p, h1, h2, h3, h4, em, strong, b, td, th, caption, blockquote, ul, li, dl, dt, ol, div, span, table  {font-family: sans-serif}
h1 { margin-left: -8% }
h2 {margin: 1.6em 0 0.8em -4%}
h2, h4  { color: #157 }
h3 { color: #078; font-size:95% }

/*]]>*/
</style>


  <title>ILRT Semantic Web technical reports</title>
</head>
<body bgcolor="#ffffff">

<h2>ILRT Semantic Web technical reports at a glance</h2>

<p>For more information see the <a href="/projects/semantic_web">Semantic Web
group page</a>
</p>

<p>Search using keyword</p>
<form
action="http://swordfish.rdfweb.org:8085/rdfquery/keywordquery.jsp"
method="GET">
<input type="hidden" name="query"
value="
SELECT ?url, ?title
 FROM $sourcefiles 
 WHERE 
(http://purl.org/rss/1.0/title ?url ?title) "/>
<input type="hidden" name="template" value="<a href='?url'>?title</a><br
/>"/>
<input type="hidden" name="variable" value="?title"/>
<input type="text" name="keyword"/>
<input type="HIDDEN" name="dburi" value="null" /><br />
<input type="submit" value="Ask" />
</form>

HTML
}

sub footer {
return <<HTML;

<p>This viewer uses <a
href="/discovery/2001/05/rss-agg/">Dan Brickley's code</a>
</p>
<p>
 This is a little traversal program which goes through a directory
 picking out index.html and readme.html, and getting the titles to
 build an rss file. For usage, see <a
href="2003/03/rss/Makefile">Makefile</a>, for files (perl and the rss
files)
see <a href="2003/03/rss/">directory</a>.
</p>

<hr />
<address>
<a
href="mailto:libby.miller\@bristol.ac.uk">libby.miller\@bristol.ac.uk</a>,
ILRT Bristol, 2003-03-12.
</address>


</body>
</html>
HTML
}

###################################### RSS 2 HTML code (from xml:rss)

# SUBROUTINES
sub print_html {
    my $rss = shift;
    my $full = shift;

    print <<HTML;
<table bgcolor="#000000" border="0" width="100%"><tr><td>
<TABLE CELLSPACING="1" CELLPADDING="4" BGCOLOR="#DDDDDD" BORDER=0
width="100%">
  <tr>
  <td valign="middle" align="center" bgcolor="#CCCCCC"><font
color="#000000"
face="Arial,Helvetica">
<strong>
<a href="$rss->{'channel'}->{'link'}">$rss->{'channel'}->{'title'}</a>
</strong>
<p>
<em><small>$rss->{'channel'}->{'description'}</small></em>
</p>
</font>
</td></tr>

<tr><td>
HTML

    # print channel image
    if ($rss->{'image'}->{'link'}) {
	print <<HTML;
<center>
<p><a href="$rss->{'image'}->{'link'}"><img src="$rss->{'image'}->{'url'}" alt="$rss->{'image'}->{'title'}" border="0"
HTML
        print " width=\"$rss->{'image'}->{'width'}\""
	    if $rss->{'image'}->{'width'};
	print " height=\"$rss->{'image'}->{'height'}\""
	    if $rss->{'image'}->{'height'};
	print "></a></center><p>\n";
    }

    # print the channel items
    foreach my $item (@{$rss->{'items'}}) {

        if ($full && $item->{'description'} ) { 
	  print "<p><small>";
  	  print $item->{'description'};
	  print "</small><br />"; 
	}
	next unless defined($item->{'title'}) && defined($item->{'link'});
	print "<small><a href=\"$item->{'link'}\">$item->{'title'}</a></small><br /></p>\n";

        print "</li>\n";

    }

    # if there's a textinput element
    if ($rss->{'textinput'}->{'title'}) {
	print <<HTML;
<form method="get" action="$rss->{'textinput'}->{'link'}">
$rss->{'textinput'}->{'description'}<br /> 
<input type="text" name="$rss->{'textinput'}->{'name'}"><br />
<input type="submit" value="$rss->{'textinput'}->{'title'}">
</form>
HTML
    }

    # if there's a copyright element
    if ($rss->{'channel'}->{'copyright'}) {
	print <<HTML;
<p><sub>$rss->{'channel'}->{'copyright'}</sub></p>
HTML
    
    return 0;
    }


    print <<HTML;
</td>
</TR>
</TABLE>
</td></tr></table>
HTML
}







