#!/usr/bin/perl use warnings; use strict; use XML::RSS; use LWP::Simple; use DB_File; use Fcntl; use Getopt::Long; use Net::Lyskom::Simple; use Text::Wrap; our @url; our $urlfile; our $dbfile; our $confname; our $komuser; our $kompass; our $komserver = "kom.lysator.liu.se"; our %db; =head1 NAME rss2kom.pl - Import RSS feeds to Lyskom =head1 SYNOPSIS rss2kom.pl --user="My Importer" --pass=secret --conf="RSS Import Conference" --dbfile=/var/db/komimport.db --url=http://example.com/rss =head1 DESCRIPTION Imports entries from an RSS feed to a Lyskom server. All configuration is handled via GNU-type long command line arguments. The available arguments are: =over =item --url An URL to an RSS feed. You must have at least one of these (unless you have an --urlfile instead), and you can have as many as your command line can hold. =item --urlfile The name of a file with one complete URL per line. The URLs in this file will be fetched. This is cumulative with any URLs given with the --url switch. =item --dbfile The name of the database file used to remember imported entries between invocations of this program. =item --conf The name of the Lyskom conference where imported entries should be posted. Remember to quote it appropriately for your command line environment. =item --user The name of the Lyskom user to log in as. =item --pass The password for the Lyskom user given with --user. =item --server The address of the Lyskom server to import to. Defaults to kom.lysator.liu.se if not given. =back =head1 AUTHOR Calle Dybedahl The latest version of this script is always available from . =cut GetOptions( "url=s" => \@url, "urlfile=s" => \$urlfile, "dbfile=s" => \$dbfile, "conf=s" => \$confname, "user=s" => \$komuser, "pass=s" => \$kompass, "server=s" => \$komserver ); if ($urlfile) { if (open U, $urlfile) { while () { chomp; push @url,$_; } } close U; } our $rss = XML::RSS->new; our $kom = Net::Lyskom::Simple->new($komuser,$kompass,$komserver); tie %db, 'DB_File', $dbfile, O_CREAT|O_RDWR, 0666, $DB_HASH or die "Failed to tie to database file $dbfile: $!\n"; sub post { my $i = shift; my $subject = $i->{title}; my $ctype = "text/plain"; my $body; my $raw; if (exists($i->{'http://purl.org/rss/1.0/modules/content/'}->{encoded})) { $raw = $i->{'http://purl.org/rss/1.0/modules/content/'}->{encoded} } else { $raw = $i->{description} } if ($raw && $raw =~ m||) { # Probably HTML $body .= "
".$rss->channel('title')."
\n" if @url>1; $body .= "

".$i->{title}."

\n"; $body .= "

".$raw."

\n"; $body .= '

'.$i->{link}."

"; $body =~ s|\s+/>|>|g; $ctype = "text/html"; } else { # Probably not HTML $body .= "[".$rss->channel('title')."]\n\n" if @url>1; $body .= "\n ".$i->{title}; $body .= "\n"; $body .= " "; $body .= "=" x length($i->{title}); $body .= "\n\n"; if ($raw) { $body .= wrap " ", " ", $raw; $body .= "\n\n"; } $body =~ s/&/&/g; $body .= "( ".$i->{link}." )"; } return $kom->post($confname, $subject, $body, "content_type", $ctype ); } foreach my $url (@url) { our $text = get $url or next; eval { $rss->parse($text); }; next if $@; foreach my $i (reverse @{$rss->{items}}) { $i->{title} =~ s/[\n\r]/ /sg if (defined($i) && defined($i->{title})); $i->{link}="" unless exists($i->{link}); $i->{title}="" unless exists($i->{title}); next if exists $db{$i->{link}." ".$i->{title}}; my $textno = post($i) or next; $db{$i->{link}." ".$i->{title}} = $textno; } }