New text for Google
[infodrom.org/www.zeitungsliste.de] / bin / zeitungen-rdf
1 #! /usr/bin/perl
2
3 # zeitungen-rdf - Build RDF file for www.zeitungsliste.de
4 # Copyright (c) 2008  Joey Schulze <joey@infodrom.org>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19
20 use strict;
21 use warnings;
22
23 use DBI;
24 use XML::RSS;
25 use Algorithm::Diff;
26
27 my %cfg = ('home' => 'http://www.zeitungsliste.de/',
28            'webdir' => '/var/www/www.zeitungsliste.de');
29
30 my $dsn = 'dbi:Pg:dbname=zlist';
31
32 my $dbh = DBI->connect('dbi:Pg:dbname=zlist') or die "Can't connect to database\n";
33
34 sub fetchnew
35 {
36     my $query = q{SELECT id,name,description FROM zeitungen
37                   WHERE deleted IS false AND changed > now() - interval'3 months'
38                   ORDER BY changed DESC};
39
40     my $sth = $dbh->prepare ($query) or die "Can't prepare Query: $DBI::errstr\n";
41     my $rv = $sth->execute or die "Can't execute query: $DBI::errstr\n";
42
43     my @ret = ();
44     push @ret, [$_->{id},$_->{name},$_->{description}] while ($_ = $sth->fetchrow_hashref);
45
46     return \@ret;
47 }
48
49 sub rdf_new
50 {
51     my $fname = shift;
52     my $papers = fetchnew();
53
54     my $rss = new XML::RSS (version => '1.0', encoding => 'utf-8');
55     $rss->channel (
56         title          => 'Zeitungsliste',
57         description    => 'Zeitungen im Netz',
58         link           => $cfg{'home'},
59         );
60
61     foreach my $paper (@$papers) {
62         # printf "%d: %s\n%s\n", $paper->[0], $paper->[1], $paper->[2];
63
64         $rss->add_item (
65             title       => $paper->[1],
66             description => $paper->[2],
67             link        => sprintf('%szeitung/%d.html', $cfg{'home'}, $paper->[0]),
68             );
69     }
70
71     if (-e $fname) {
72         my @new = split (/\n/, $rss->as_string);
73         open (F, $fname);
74         chomp(my @old = <F>);
75         close (F);
76
77         my @diff = Algorithm::Diff::diff (\@old, \@new);
78         $rss->save ($fname) if (@diff);
79     } else {
80         $rss->save ($fname);
81     }
82 }
83
84 rdf_new($cfg{webdir} . '/zeitungen.rdf');