Hi,
I have this wrapper module for XML::RSS which should serve as an example, was based on the docs from XML::RSS. You could replace the sql used with another data source.
package News::Feed;
use CGI;
use XML::RSS;
use Date::Manip;
#Wrapper for XML::RSS
sub new {
my $class=shift;
my $att=shift;
my $self={};
bless $self,$class;
$self->_init($att);
return $self;
}
sub _init {
my $self=shift;
my $att=shift;
#hardcode 2.0 rss for now
$self->{rss} = new XML::RSS (version => '2.0');
$self->{_dbh} = $att->{_dbh};
}
sub getHeading {
#todo
}
sub getItems {
# For example print out/return the titles and links of each RSS it
+em
foreach my $item (@{$rss->{'items'}}) {
print "title: $item->{'title'}\n";
print "link: $item->{'link'}\n\n";
}
}
sub createFeedXml {
my $self=shift;
my $details=shift;
#Channel information is required in RSS. The title cannot be more
+ the 40 characters, the link 500, and the description 500 when output
+ting RSS 0.9.
#title, link, and description, are required for RSS 1.0. language
+ is required for RSS 0.91.
#validate $self->feedParams () #todo validate input
$self->{rss}->channel(
title => $details->{title},
link => $details->{link},
description => $details->{description},
dc => {
date => $details->{date},
lastBuildDate =>$details->{date},
subject => $details->{subject},
creator => $details->{creator},
publisher => $details->{publisher},
rights => $details->{rights},
language => 'en-us',
},
syn => {
updatePeriod => "$details->{updatePeriod}",
updateFrequency => "1",
updateBase => "1901-01-01T00:00+00:00",
},
taxo => [
'http://dmoz.org/Computers/Internet',
'http://dmoz.org/Computers/PC'
]
);
}
sub addItem {
my $self=shift;
my $details=shift;
$self->{rss}->add_item(title => "$details->{title}",
link => "$details->{link}",
description=>"$details->{description}"
);
}
#set the image for the feed
sub getImage {
my $self=shift;
my $details=shift;
$self->{rss}->image(title => $details->{title},
url => $details->{imgurl},
link => $details->{link},
width => $details->{width},
height => $details->{height},
description => $details->{description}
);
}
# Parse raw xml into the XML::RSS object
sub parse_sourcexml {
my $self=shift;
my $sourcexml=shift;
$self->{rss}->parse($sourcexml);
return $self->{rss};
}
# Get the source xml with LWP
sub getFeed {
my $self=shift;
my $sourcelink=shift;
$self->{sourcedata} = get($sourcelink);
$self->{parsed}=$self->parse($self->{sourcedata});
return $self->{parsed};
}
sub writeFeedToDisk {
my $self=shift;
my $filename=shift;
$self->{rss}->save("$filename");
}
#feeds that will be generated from our db
sub getOutputFeeds {
my $self=shift;
my $sql="SELECT * FROM news_output_feeds WHERE status='active'";
my $sth=$self->{_dbh}->prepare($sql);
$sth->execute();
return $sth->fetchall_arrayref({});
}
sub getInputFeeds {
my $self=shift;
my $sql="SELECT * FROM news_feeds WHERE status='active'";
}
sub generateAllOutFeeds {
my $self=shift;
my $outfeeds=$self->getOutputFeeds();
foreach my $feeddata (@$outfeeds) {
$self->generateOutFeed($feeddata);
#clear oot rss obj
$self->{rss} = new XML::RSS (version => '2.0');
}
}
sub sanitise {
my $string = shift;
$string =~ tr/\x91\x92\x93\x94\x96\x97\x19/''""\-\-/;
$string =~ s/\x85/.../sg;
$string =~ s/\x13//sg;
$string =~ tr/[\x80-\x9F]//d;
return($string);
}
sub generateOutFeed {
my $self=shift;
my $feeddata=shift;
#run the sql get the data from db
my $sth=$self->{_dbh}->prepare($feeddata->{source_sql_query});
$sth->execute();
use Encode;
my $data=$sth->fetchall_arrayref({});
map {$feeddata->{$_}=encode('utf8', decode("ascii", $feeddata->{$_
+})) } keys %$feeddata;
map {$feeddata->{$_}=sanitise($feeddata->{$_}) } keys %$feeddata;
my $date =UnixDate('today','%a, %d %b %Y %H:%M:%S %Z');
#create channel details
my $channeldetails={
title=>$feeddata->{title},
link=>$feeddata->{link},
description=>$feeddata->{description},
date => $date,
subject => $feeddata->{subject},
creator => $feeddata->{creator},
publisher => $feeddata->{publisher},
rights => $feeddata->{rights},
};
$self->createFeedXml($channeldetails);
# add data to feed
foreach my $row (@$data) {
map {$row->{$_}=sanitise($row->{$_}) } keys %$row;
map {$row->{$_}=encode('utf8', decode("ascii", $row->{$_})) }
+keys %$row;
$self->addItem($row);
}
$self->writeFeedToDisk("$feeddata->{filename}");
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.