#! /usr/bin/perl # zeitungen-rdf - Build RDF file for www.zeitungsliste.de # Copyright (c) 2008 Joey Schulze # # 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA. use strict; use warnings; use DBI; use XML::RSS; use Algorithm::Diff; my %cfg = ('home' => 'http://www.zeitungsliste.de/', 'webdir' => '/var/www/www.zeitungsliste.de'); my $dsn = 'dbi:Pg:dbname=zlist'; my $dbh = DBI->connect('dbi:Pg:dbname=zlist') or die "Can't connect to database\n"; sub fetchnew { my $query = q{SELECT id,name,description FROM zeitungen WHERE deleted IS false AND changed > now() - interval'3 months' ORDER BY changed DESC}; my $sth = $dbh->prepare ($query) or die "Can't prepare Query: $DBI::errstr\n"; my $rv = $sth->execute or die "Can't execute query: $DBI::errstr\n"; my @ret = (); push @ret, [$_->{id},$_->{name},$_->{description}] while ($_ = $sth->fetchrow_hashref); return \@ret; } sub rdf_new { my $fname = shift; my $papers = fetchnew(); my $rss = new XML::RSS (version => '1.0', encoding => 'utf-8'); $rss->channel ( title => 'Zeitungsliste', description => 'Zeitungen im Netz', link => $cfg{'home'}, ); foreach my $paper (@$papers) { # printf "%d: %s\n%s\n", $paper->[0], $paper->[1], $paper->[2]; $rss->add_item ( title => $paper->[1], description => $paper->[2], link => sprintf('%szeitung/%d.html', $cfg{'home'}, $paper->[0]), ); } if (-e $fname) { my @new = split (/\n/, $rss->as_string); open (F, $fname); chomp(my @old = ); close (F); my @diff = Algorithm::Diff::diff (\@old, \@new); $rss->save ($fname) if (@diff); } else { $rss->save ($fname); } } rdf_new($cfg{webdir} . '/zeitungen.rdf');