#!/home/intute/perl/bin/perl # RDN RSS viewer v2 # Simple RSS viewer with templating # # Pete Cliff & Andy Powell # $Id: index.cgi,v 1.1 2004/05/15 09:03:40 rdn Exp $ # template, rss, layout, (output) format use XML::RSS; use XML::Atom::Feed; use LWP::UserAgent; use LWP; use LWP::Protocol; use CGI; use URI::Escape; use Unicode::String qw(utf8 latin1); use HTML::Entities; # added by Paul Smith 2008-07-25 to address entity encoding issues my $cgi = new CGI; my $tptag = ""; my $tpuri = $cgi->param('template') || ""; my $rssuri = $cgi->param('rss'); my $layout = $cgi->param('layout') || "table"; #my $format = $cgi->param('format') || "html"; # this line changed Sat May 15 09:54:51 BST 2004 - AP my $format = $cgi->param('format'); my $template; unless ($tpuri eq "") { $template = readURL($tpuri) || $tptag; } else { $template = $tptag; } # my $options = $cgi->param('options'); - one day this might get implemented... ;-) if (defined $rssuri) { $rssuri =~ s/www\.intute\.ac\.uk\//www\.intute\.ac\.uk\//; # removed :8080 since moving out of web proxy to tintagel print "$rssuri\n"; if ($rss = readURL($rssuri)) { CASE: { if ($layout eq "table") { $rsshtml = asHTMLtable($rss,$rssuri); last CASE; }; if ($layout eq "list") { $rsshtml = asHTMLlist($rss); last CASE; }; # default to table $rsshtml = asHTMLtable($rss,$rssuri); last CASE; } my %s = ($tptag, $rsshtml); $output = sub_engine($template, \%s); } else { # RSS retrieve failed; $output = "

RSS viewer ERROR

\n

Unable to retrieve RSS at $rssuri
\nSorry

\n"; } } else { # no source RSS file $output = "

RSS viewer ERROR

\n

No RSS URL specified - please use add rss=url option
\nSorry

\n"; } CASE: { if ($format eq "javascript") { $output = asJavaScript($output); last CASE; } if ($format eq "html") { $output = $output; last CASE; }; # defaults to JavaScript $output = asJavaScript($output); last CASE; } print $cgi->header(); print $output; # == the end == sub readURL { my ($url) = @_; $url = uri_unescape($url); print "$url\n"; my $ua = LWP::UserAgent->new; #$ua->proxy(['http', 'ftp'], "http://scoop.ukoln.ac.uk:3128"); $ua->agent('RDN RSS Viewer/1.1'); # my $h = new HTTP::Headers # Cache-control => 'no-cache', # Pragma => 'no-cache'; my $req = HTTP::Request->new(GET => "$url");#, $h; my $c = $ua->request($req); # my $content = $c->content; # $content =~ s/(\<[^\>]*\>)//; # print $content; # print scalar $c->content."\n"; return $c->content; # return $content; } sub asHTMLlist { my ($raw) = @_; my $rss = new XML::RSS; my $toGo = ""; eval { $rss->parse($raw); }; if ($@) { $toGo .= "

RSS viewer ERROR

\nInvalid0 RSS
\nSorry
\n"; return $toGo; } $toGo .= "\n"; $toGo .= "\n"; if ( $rss->{image}{url} ) { $toGo .= "\n"; } else { $toGo .= "\n"; } $toGo .= "\n"; $toGo .= "\n"; $toGo .= "

".toLatin1($rss->{channel}{title})."

{image}{link})."\">{image}{url})."\" alt=\"".toLatin1($rss->{image}{title})."\" border=\"0\" valign=\"top\" align=\"right\"> 

".toLatin1($rss->{channel}{description})." <{channel}{link})."\">".toLatin1($rss->{channel}{link}).">

\n"; $toGo .= "
{textinput}{link})."\">".toLatin1($rss->{textinput}{description})."
{textinput}{name})."\">
{textinput}{title})."\">
\n" if ($rss->{textinput}{title}); $toGo .= "
    \n"; for my $item (@{$rss->{items}}) { $toGo .= "
  • {'link'})."\">".toLatin1($item->{title})."
    \n"; my $description = toLatin1($item->{description}); $description =~ s/\[Programme details:\s*(.*)\]\s*//i; my $progDetails = $1; undef $1; $toGo .= "$description
    \n"; $toGo .= "[Programme details]\n" if $progDetails; } $toGo .= "

\n"; $toGo .= "Contact: ".toLatin1($rss->{channel}{webMaster})."
" if ($rss->{channel}{webMaster}); $toGo .= "Last modified: ".toLatin1($rss->{channel}{lastBuildDate})."
" if ($rss->{channel}{lastBuildDate}); $toGo .= toLatin1($rss->{channel}{copyright})."
" if ($rss->{channel}{copyright}); return $toGo; } sub asHTMLtable { my ($raw,$rssuri) = @_; my $toGo; ### # $toGo = "

$raw
\n"; # return $toGo; ### if ( $raw =~ m|]+xmlns="http://|s ) { # It's an Atom feed my $atom_feed; eval { $atom_feed = XML::Atom::Feed->new( $rssuri ); }; if ($@) { $toGo .= "

RSS viewer ERROR

\n"; $toGo .= "

Invalid Atom feed ($@)
\n"; $toGo .= "Sorry

\n"; return $toGo; } $toGo .= "
\n"; $toGo .= "\n"; $toGo .= ""; my $image = $atom_feed->logo() || $atom_feed->icon(); if ( $image) { $toGo .= ""; } else { $toGo .= ""; } $toGo .= "\n"; if ( $atom_feed->subtitle() ) { $toGo .= "\n"; } $toGo .= "\n"; for my $entry ( $atom_feed->entries() ) { my $link = $entry->link(); $toGo .= "\n"; my $description; if ( $entry->summary() ) { $description = $entry->summary(); } elsif ( $entry->content() ) { $description = $entry->content(); $description = $description->body() if $description->body(); } $toGo .= "\n"; } $toGo .= "\n"; $toGo .= "
".$atom_feed->title()." 
".$atom_feed->subtitle()."
href()."\">".$entry->title()."
".$description."
\n"; $toGo .= "
\n"; return $toGo; } # Else it's not Atom, try RSS my $rss = new XML::RSS; eval { $rss->parse($raw); }; if ($@) { $toGo .= "

RSS viewer ERROR

\n"; $toGo .= "

Invalid RSS
\n"; $toGo .= "Sorry

\n"; return $toGo; } # This is RSS 0.9 - 1.0 $toGo .= "
\n"; $toGo .= "\n"; $toGo .= ""; if ( $rss->{image}{url} ) { $toGo .= ""; } else { $toGo .= ""; } $toGo .= "\n"; $toGo .= "\n"; $toGo .= "\n"; for my $item (@{$rss->{items}}) { $toGo .= "\n"; $toGo .= "\n"; } $toGo .= "\n"; $toGo .= "
".toLatin1($rss->channel('title'))."image('link'))."\">image('url'))."\"> 
".toLatin1($rss->channel('description'))."
{link})."\">".toLatin1($item->{title})."
".toLatin1($item->{description})."
\n"; $toGo .= "
\n"; return $toGo; } sub asJavaScript { my ($content) = @_; # my $toGo = "\n"; return $toGo; } sub sub_engine { my ($html, $subs) = @_; %subh = %{$subs}; foreach $key (keys(%subh)) { $html =~ s/$key/$subh{$key}/g; } return $html; } sub toLatin1 { my ($in) = @_; decode_entities($in); # added by Paul Smith 2008-07-25 to address entity encoding issues encode_entities($in, "\200-\377"); # added by Paul Smith 2008-07-25 to address entity encoding issues my $u = utf8($in); return $u->latin1; }