package Ernad::Rss; use strict; use warnings; use Data::Dumper; use Ernad::Files; use Ernad::Common; use XML::RSS; use XML::Twig; our $collID; our $rss; sub text2rss { my ($twig, $text) = @_; my $creator = ""; my $link = ""; eval{ $creator = join(", ", map {$_->first_child("person")->first_child_text("name")} $text->children("hasauthor")); }; eval{ $link = $text->first_child("file")->first_child_text("url");}; $rss->add_item( title => $text->first_child_text("title"), link => $link, description => $text->first_child_text("abstract"), dc => { # we assume that the date is well formatted! date => $text->first_child_text("date"), subject => $text->first_child_text("keywords"), creator => $creator } ); return 1; # important for XML::Twig } sub add_channel { my ($twig, $coll) = @_; my $creator = ""; my $title = $coll->first_child_text("title"); eval{$creator = $coll->first_child("haseditor")->first_child("person")-> first_child_text("name");}; my $date = $coll->{'att'}->{'id'}; $date =~ s/(.*):(\d\d\d\d-\d\d-\d\d)$/$2/; $collID = $1; $rss->channel( title => $title, link => $coll->first_child_text("homepage"), description => $title, dc => { creator => $creator, date => $date } ); $coll->purge; return 1; # important for XML::Twig } sub parse { my $file = shift; if(not -f $file) { die "no such file $file"; } my $twig = XML::Twig->new(twig_handlers => { '/amf/collection' => \&add_channel, '/amf/collection/haspart/text' => \&text2rss }); $twig->parsefile($file) or return "Couldn't parse XML in file '$file': $@"; } sub make_rss { my $rs=shift or return; #open(L,"> /tmp/rss.log"); #print L "start\n"; $rss = XML::RSS->new(version => '1.0'); my $e=$rs->{'e'} or die "no erimp"; my $rss_dir=$e->{'conf'}->{'RSS_Dir'} or die; my $in_file=$rs->file(); parse($in_file); our $um = umask(0022); my $repcode=$rs->repcode(); my $out_file="$rss_dir/$repcode.rss.xml"; #print L "out_file is $out_file"; &Ernad::Files::prepare_for_file($out_file); open REPORT, "> $out_file" or die "I can not open $out_file: $!"; print REPORT $rss->as_string; #print L $rss->as_string; close REPORT; #close L; umask($um); } 1;