# # Simple RSS News Feed Generator in Perl
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© August 2003 Tony Lawrence

Simple RSS News Feed Generator

If you don't know what an RSS News feed is, see RSS Syndication Feeds and Why Do I Care?

So, you have a web site, and you want to generate an RSS feed. In my case, I have the entire process of adding anything to my site scripted (see The AutoMagic Web Site), and of course generating new RSS documents is just part of that. But you may not have reached that state yet, so here's a little Perl script that will help you along.

This asks you for new page information, and stores that in a file (or you can create the file manually or any other way you like). It then reads the file backwards, and generates an RSS file.

You can use this to create your own RSS News feeds, or just to see what the feed should look like. It's pretty simple code, so shouldn't be hard to understand.

I set the maximum number of RSS headlines to 15. You can change that, but keep two things in mind: some RSS News readers won't display more than 15 items anyway, and all RSS News readers are going to regularly request the RSS file from your site to see if anything has changed. If it's a big file, that can add up to a lot of bandwidth.

By the way, don't bother with this if you hardly ever add new pages to your site. It just isn't worth the trouble, and no one is going to care. But if you do add new pages regularly, you really want to do this: having an RSS feed improves your readers ability to find new content.

You'll enter three pieces of information for each page, separated by pipe symbols ("|"):

The URL:  http://yourweb.co/yourpage.html
A Title:  How to Medicate a Cat
A Description: I lose every time we try this!
 

The URL is where the page is on the internet, the title is what will be displayed in the RSS News reader, and often the description will show up when the user hovers over the title with their mouse, or it may appear in a separate box. So, you'd enter (in the file or at the keyboard):

http://yourweb.co/yourpage.html|How to Medicate a Cat|I lose every time we try this!
 

and so on. Here's the code:

#!/usr/bin/perl
# url of the generated file
$MY_RSSFILE="http://aplawrence.com/rss.rdf";
# physical location
$MY_FILE="./rss.rdf";
# title
$MY_TITLE="Site News for A.P.Lawrence Unix, Linux and Mac OS X Resources";
# description
$MY_DESC="Resources and information for Unix and Linux, and other computer related topics. Thousands of articles, reviews, consultants listings, skills tests, opinion, how-to's, suggestions and more for Unix, Linux and Mac OS X, netorking, web site maintenance and more.. ";
# where is the site
$MY_LINK="http://aplawrence.com";
# your logo or other image
$MY_IMAGE="http://aplawrence.com/image21.gif";
$MY_IMAGE_TITLE="A.P. Lawrence Logo";
# local storage for page info
$MY_STORED_DATA="./storeit";
# how many items to generate.
# you can make it larger, but some readers will only show 15, and you
# do need to stop somewhere.
$MAX=15;
getnew();
open(O,">$MY_FILE") or die "How come $! $MY_FILE ?";
$now=time;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($now);
@month=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
$year += 1900;
@days=("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
$AM="AM";
$AM="PM" if $hour > 12;
$hms=sprintf("%.2d:%.2d:%.2d",$hour,$min,$sec);
$update="$days[$wday], $mday $month[$mon] $year $hms GMT";
print O <<EOF;
<?xml version="1.0" encoding="iso-8859-1"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="$MY_RSSFILE">
<title>$MY_TITLE</title>
<description>
$MY_DESC
</description>
<dc:language>en</dc:language>
<link>$MY_LINK</link>
<lastBuildDate>$update</lastBuildDate>
<image rdf:resource="$MY_IMAGE">
</image>
<items>
<rdf:Seq>
EOF
for ($x=0;$x < 15;$x++) {
last if not $url[$x];
print O <<EOF;
<rdf:li rdf:resource="$url[$x]" />
EOF
}

print O  <<EOF;
</rdf:Seq>
</items>
</channel>
<image rdf:about="$MY_IMAGE">
<title>$MY_IMAGE_TITLE</title>
<url>$MY_IMAGE</url>
<link>$MY_LINK</link>
</image>


EOF
for ($x=0; $x < 15 ; $x++) {
last if not $url[$x];
print O <<EOF;
<item rdf:about="$url[$x]">
<title>$stuff[$x]</title>
<description>$desc[$x]</description>
<link>$url[$x]</link>
</item>
EOF
}
print O "</rdf:RDF>\n";
close O;

sub getnew {

print <<EOF;
Add new information; CTRL-D when done.
Enter: url|title|description
Example
http://yourweb.com/cows.html|Cows of New Jersey|Examining the cow breeds of New Jersey farms
EOF
$x=0;
open(O,">>$MY_STORED_DATA") or die "Hey $! $MY_STORED_DATA";
while (<>) {
  last if $x == $MAX;
  print O $_;
  $x++;
}
close O;
open(O,"$MY_STORED_DATA") or die "Huh? $! $MY_STORED_DATA";
@webpage=<O>;close O;
$xcnt=0;
foreach (reverse @webpage) {
  last if $xcnt == $MAX ;
  chomp;
  ($link,$title,$desc)=split /\|/;
  $stuff[$xcnt]=$title;
  $desc[$xcnt]=$desc;
  $url[$xcnt++]=$link;
}
}
 

If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Simple RSS News Feed Generator in Perl


Inexpensive and informative Apple related e-books:

Take Control of Pages

Take control of Apple TV, Second Edition

Take Control of Preview

Take Control of Numbers

Take Control of iCloud, Fifth Edition





More Articles by © Tony Lawrence





Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





Never let a computer know you're in a hurry. (Anonymous)




Linux posts

Troubleshooting posts


This post tagged:

Code

SEO

Web/HTML



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode