#!/usr/bin/perl
# Slashdot Sidebar & Active Desktop
# Copyright (C) 2003 Neil Fraser
# http://neil.fraser.name/software/slashdot
use strict;

# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General
# Public License as published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# http://www.gnu.org/

# This script is run by the Cron Daemon once every five minutes.
# It imports Slashdot's article list, parses it, then exports it
# to HTML.  Mozilla and Active Desktop load the HTML file.

my $homedir = '/home/neil/html/software/slashdot';

# Download the list of articles.
# This step is hidden, since it contains a list of proxies placed around the net.
# But all it does is downloads http://rss.slashdot.org/Slashdot/slashdot/to to this directory.
do "$homedir/loadproxy.pl";

# Parse the list and extract the data.
my $count = 0;
my $text = '';
open (FILE, "$homedir/slashdot.rdf") || print "Cannot read slashdot.rdf!\n";
  my $item = 0;
  my $title;
  my $link;
  while (<FILE>) {
    if (/<item>/i) {
      $item = 1;
      $title = '';
      $link = '';
    } elsif (/<\/item>/i) {
      if ($title && $link) {
        $text .= "<BR>&middot; <A HREF=\"$link\" TARGET='_content'>$title</A>\n";
      }
      $item = 0;
      $count++;
    } elsif (/^\s*<title>(.+)<\/title>\s*$/i) {
      $title = $1;
    } elsif (/^\s*<link>(.+)<\/link>\s*$/i) {
      $link = $1;
    }
  }
close (FILE);

# Write out the new article list.
if ($text && $count > 4) {
  open (FILE, "> $homedir/slashdot.html");
    print FILE <<END;
<html><head><title>Slashdot Articles</title><meta http-equiv="Pragma" content="no-cache"><meta http-equiv=refresh content=1200></head><body border="0px"><FONT FACE='sans-serif' SIZE=1>
<B><A HREF='http://www.slashdot.org/' TARGET='_content'>Slashdot</A> articles:</B>
$text
</font></body></html>
END
  close (FILE);
}
