APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS














(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



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



Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar


Auto FTP Manager


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Code
       - Web/HTML


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress