#!/usr/bin/perl

# $Id: rss.pl,v 1.8 2007/04/25 13:18:54 gaute Exp $
#
# Generates an RSS feed from the news on our front page.
#
# Usage: perl rss.pl < index.html > no.rss
#
# Copyright (C) 2007, Gaute Hvoslef Kvalnes <gaute@verdsveven.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

# Basic idea:
#   Make an RSS "item" for each <div class="nyhet">.
#
#   <div class="nyhet">
#     <h3>Overskrift</h3>
#
#     <p class="dato">DD.MM.ÅÅÅÅ</p>
#
#     <p></p>
#   </div>

# title: "Overskrift"
# link: "http://no.openoffice.org/" # always
# pubDate: DD.MM.YYYY
# guid: crafted from title and date
# description: <the rest>

use strict;
use warnings;
use utf8;

use Encode qw(decode);
use POSIX qw(mktime strftime setlocale);
use XML::RSS;
use HTML::Entities qw(encode_entities_numeric);

my $CHANNEL_TITLE = "OpenOffice.org p&#xE5; norsk";
my $CHANNEL_LINK = "http://no.openoffice.org/";
my $CHANNEL_DESC = "Nytt om OpenOffice.org";
my $CHANNEL_LANG = "no";

setlocale(&POSIX::LC_ALL, "C");
# RFC 822 formatted date:
my $rfc822_fmt = '%a, %d %b %Y %H:%M:%S %z';

# Slurp stdin. Perl Cookbook 8.0.1.
undef $/;
my $html = <>;

my $rss = XML::RSS->new(version => '2.0',
                        encode_output => 0);
$rss->channel(title => $CHANNEL_TITLE,
              link => $CHANNEL_LINK,
              description => $CHANNEL_DESC,
              language => $CHANNEL_LANG);

# Parse <div id="nyheter">. Perl Cookbook 20.18.3.
my $news;
($news) = $html =~ m{<div id="nyheter">(.*)</div> <!-- "nyheter" -->}s
  or die "Couldn't find <div id=\"nyheter\">.";

while ($news =~ m{<div\ class="nyhet">.*?<h3[^>]*>
                  (.+?)    # $1: title
                  </h3>.*?
                  <p\ class="dato">
                  ([0-9]{2})\.    # $2, $3, $4: date DD.MM.YYYY
                  ([0-9]{2})\.
                  ([0-9]{4})
                  </p>
                  (.*?)    # $5: description
                  </div>}gxs) {
  my $title = encode_entities_numeric(decode("utf8", $1));
  my $day = $2;
  my $month = $3 - 1;
  my $year = $4 - 1900;
  my $description = encode_entities_numeric(decode("utf8", $5));

  # Craft a guid from title and date.
  my $guid = "$title $year-$month-$day";

  my $date = strftime($rfc822_fmt,
                      localtime(mktime(0, 0, 0, $day, $month, $year)));

  $rss->add_item(title => $title,
                 link => $CHANNEL_LINK,   # We have no separate news
                                          # pages, so we always link
                                          # to the front page.
                 description => $description,
                 guid => $guid,
                 pubDate => $date);
}

print $rss->as_string;
