Monday 2 May 2011

RSS Feed: How to create custom feeds (part 1)

   


This is the first of a two parts article about creating custom RSS feeds. It might be a complex task in terms of coding, however the logic behind it is not that difficult to understand. I decided to divide it in two, so that I can explain it in depth.

Some assumptions must be made before starting with the creation of the page.
1) We have a database from which we fetch data for the feed. We assume the resulting recordset is called RS (as we always do in our examples).
2) In the root directory of our web site, there's a folder called "rss" where we are going to write the feed (xml file). The folder must have read and write permissions.
3) The page used to create the feed is an asp page with VBScript as programming language.
4) The feed is generated by opening the above asp page. Thus the creation of the feed is not automatic. Anyway, it could be integrated at the end of a data inserting process, so that every time a new item is inserted or updated in the database, the asp page is run (and the related xml file created).

The Recordset
I'm going to describe the feed file creation in depth, as much as I can, step by step. The only thing I will not show is the recordset fetching procedure. Let me explain it a little bit.
We assume that the data we get from the database is quite simple (for obvious explanatory purpose). The items fetched will be:
1) a title;
2) a link to the article;
3) a main body part (the actual text of the news);
4) a date (which is the date when the news is published).

Those 4 items will be:
1) rs.fields.item("news_title").value;
2) rs.fields.item("news_link").value;
3) rs.fields.item("news_body").value;
4) rs.fields.item("news_date").value.

I hope that it is clear enough. If not, I believe you should consider reviewing some recordset fetching tutorials.

Building the page
Now we can start creating our feed. The asp page will be called "generate.asp".
First of all, create your recordset in whatever way you like. Put it at the beginning of your page, so that the data is fetched and it is ready to be used. Then we need a little function I found here. After collecting the data from the database, add the following code:
<%
Function return_RFC822_Date(myDate, offset)
  Dim myDay, myDays, myMonth, myYear
  Dim myHours, myMinutes, mySeconds
   
  myDate = CDate(myDate)
  myDay = WeekdayName(Weekday(myDate),true)
  myDays = Day(myDate)
  myMonth = MonthName(Month(myDate), true)
  myYear = Year(myDate)
  myHours = zeroPad(Hour(myDate), 2)
  myMinutes = zeroPad(Minute(myDate), 2)
  mySeconds = zeroPad(Second(myDate), 2)
   
  return_RFC822_Date = myDay&", "& _
                       myDays&" "& _
                       myMonth&" "& _
                       myYear&" "& _
                       myHours&":"& _
                       myMinutes&":"& _
                       mySeconds&" "& _
                       offset
End Function

Function zeroPad(m, t)
  zeroPad = String(t-Len(m),"0")&m
End Function
%>
These are actually two function we will use to convert normal dates into RFC-822 format date. This is very useful for converting dates into required formats for dates in RSS feeds. We are going to pass the rs.fields.item("news_date").value to the function, so that we will have a properly formatted date value.

The feed 
At this point, we are going to create the feed itself. The basic structure of a feed is not that complicated. Anyway, we must comply to some specification. First of all, we are going to write down the procedure for creating the xml file:
<%
dim var_path, var_file, var_count
dim vardate
dim vardatein

var_path = server.mappath("rss")
var_file = var_path & "\" & "news.xml"
var_count = 0

set objfso = server.createobject("scripting.filesystemobject")
set objtstream = objfso.createtextfile(var_file, true)
As you can see, some variables are set:
1) var_path is the path of the rss folder;
2) var_file is the complete path and name of the xml file (news.xml);
3) var_count will represent the number of items we will insert into the feed. We set it to 0 (zero);
4) var_date represent the date of the feed;
5) var_datein is the date of a single item.
We will use var_count, var_date and var_datein later.
After setting those variables we create the xml object and we are ready to start creating it.

The next part will be dealing with the creation of the feed.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.