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

## this script parses RSS 1.0 only!

use LWP::Simple qw/get/;
use XML::Simple;
use CGI qw/:standard/;

use vars qw/$parse $rss/;

my $url  = 'http://www.esight.org/rss/dew_rotation.xml'; 
my $path_to_html = '/local/path/to/public_html/';

my $tries=0;

   my $feed = get($url);

   while (!$feed) {

      $tries++;
      $feed = get ($url);
      sleep 900;
      die "Can't connect to eSight $url\n" if $tries > 12;      ## 3 hrs
   }

   $feed=~ s/^[\s\n]+//;

   ## parse the feed
    $parser = XML::Simple->new();
    $rss = $parser->XMLin($feed, noattr=>1, suppressempty=>1);

   my $html_file = $rss->{'channel'}->{'title'} . '.html';
      $html_file =~ s/[\s\/]/_/g;

   my $div = collect_data();

      open (OUT, ">" . $path_to_html . $html_file) or die "Can't write $html_file: $!\n";
        print OUT '<DIV CLASS="channelLink">' . "\n" . 
                  $div .
                  "</TABLE>\n</DIV>";
     close OUT;

exit();


###
sub collect_data  {

  my @table_rows=();
  my $link_text;

    ## A. channel section...

    if (defined($rss->{'image'}->{'url'})) {
         $link_text = qq|<IMG SRC="$rss->{'image'}->{'url'}" WIDTH="200" HEIGHT="46" BORDER="0" ALT="$rss->{'image'}->{'title'}">|;
    } else {
         $link_text = font({-face=>'Times New Roman',
                            -size=>'+2'},$rss->{'channel'}->{'title'});
    }

  my $div = a({-href=>$rss->{'channel'}->{'link'}},$link_text) . p;

    if (defined($rss->{'textinput'}->{'link'})) {

        $div .= start_form(-action=>$rss->{'textinput'}->{'link'}) .
                table({ -widht=>'100%',
                        -cellspacing=>0,
                        -cellpadding=>3,
                        -border=>0 },

                      Tr([ td( font({-face=>'Verdana',
                                     -size=>3},$rss->{'textinput'}->{'title'} . ':') .
                               textfield(-name => $rss->{'textinput'}->{'name'},
                                         -override  => 1,
                                         -size      => 15,
                                         -maxlength => 80)   ),

                           td( submit(-value=>'Search') )  ])) .
                end_form .
                p;
    }

   ## B. item section

  $div .=  qq|<P><DIV CLASS="linkentries">\n|; 

  if (ref($rss->{'item'}) =~ /HASH/) {       ## single item

     if(defined($rss->{'item'}->{'title'}) && defined($rss->{'item'}->{'link'})) {

        push(@table_rows,td( a({-href=>$rss->{'item'}->{'link'}},$rss->{'item'}->{'title'}) ));

        if(defined($rss->{'item'}->{'description'})) {
           my $description = $rss->{'item'}->{'description'};
              $description =~ s/[“”]/"/g;
           push(@table_rows,td( font({-face=>'Ariel',-size=>3}, $description . p)));
        }
     }

  } else {                                   ## multiple items

      foreach my $item (@{$rss->{'item'}})  {
        next unless defined($item->{'title'}) && defined($item->{'link'});
         push(@table_rows,td( a({-href=>$item->{'link'}},$item->{'title'}) ));
         push(@table_rows,td( font({-face=>'Ariel',-size=>3},$item->{'description'} . p)));
      }
  }
  $div .= table({-width=>'55%'},Tr([ @table_rows ]));

  return $div;
}