Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Seeking advice on generating a syndication feed

by hakkr (Chaplain)
on Nov 16, 2005 at 16:23 UTC ( #509089=note: print w/replies, xml ) Need Help??


in reply to Seeking advice on generating a syndication feed

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}"); }

Replies are listed 'Best First'.
Re^2: Seeking advice on generating a syndication feed
by fizbin (Chaplain) on Nov 16, 2005 at 16:47 UTC

    Am I correct in assuming then that you don't generate the XML feed on the fly, but rather save it off in a separate file?

    That might be the solution to my performance worries, but I'll have to think about how to trigger an update of the RSS feed when appropriate...

    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

      Yes it's created every day in a cron and written to disk with the writeFeedToDisk() sub. You could change it so it only writes the file when the source data changes maybe with a database trigger.

      Had the same worries about aggregators and news readers hitting my feed too much for it to be created dynamically

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://509089]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2023-02-05 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?