ASP 101 - Active Server Pages 101 - Web03
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
An Overview of ASP.NET
Connections, Commands, And Procedures
What is ASP?

QUICK TIP:
Visual Studio Item Templates
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


A MegaTokyo RSS Feed Reader (ASP)

by John Peterson

The other day, one of my favorite online comics, MegaTokyo, finally added an RSS feed. Being the MT junkie that I am, I instantly decided that I needed to put this on my start page.

If you're not familiar with RSS, you might find XML Files's RSS section helpful.

As I've mentioned in the past, because I spend so much of my time in a browser, I've taken the time to build my own customized start page that runs on my local web server. It contains a number of things that I use on a fairly regular basis. These include search forms for my favorite search engines, a page containing dns, ip addresses, and connection details for my servers, links to sites I visit daily, links to my router config screens, etc. So what's the point? The point is that now it's going to contain the latest headlines from MegaTokyo as well.

Why MegaTokyo?

There are RSS feeds for everything these days. So why did it take me this long to write an ASP script to read one? Simple... because I'm lazy and until now there wasn't a feed I wanted on my homepage. Why did I use classic ASP instead of ASP.NET? Two reasons: there are already scripts everywhere to do this in ASP.NET, and my homepage is still running on classic ASP. I know I could easily convert it, but I think the answer to the first question already adequately covers why I haven't.

To be honest, there's nothing special about the code... it'll work with most any RSS feed. I've done a few things specific to MegaTokyo's feed (like replacing <br>s with <br />s for XHTML compliance), but there's really no reason you couldn't use this script with any RSS feed. In fact, we use a very similar script on our homepage to pull in the headlines from DevX's .NET feed. (Before you write, asking me to add headlines for your site, please note that DevX is also owned by Jupitermedia).

The Code

Before I give you the code, I'd like to mention that it includes no caching. This is not a big deal for my intended usage (because my homepage is hit only by me and really only a few times a day), but if you were to put this on a heavily trafficked page, you should absolutely add some form of caching. It will make your pages faster and will lessen the load on the server providing the RSS feed. For more information on an easy way to do this, see my article A Simple Method for Caching HTTP Requests. If you change the code below to build your HTML in a string instead of Response.Writing it directly out to the browser, the caching code can be used almost exactly as it is.

So without any further rambling on my part, here's the code:

Update: I tried using the non-cached version and was just about to pull all my hair out. Apparently I hit my start page more then I was giving it credit and I'm used to it just popping right in instantly. At times the MT servers were slow and it was killing me. Anyway, since I modified my version to cache, I figured I might as well post the caching version here as well. It's nothing sophisticated, it just copies the output text to an application var and uses that instead of making any new HTTP requests for a few hours. A copy of the old version is still available in the zip file if you need it for some reason, but the code listing below does include caching.
megatokyo_rss.asp
<%@ Language="VBScript" %>
<% Option Explicit %>
<% Response.Charset = "UTF-8" %>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>ASP 101's MegaTokyo RSS Feed Reader</title>
</head>
<body>
<%
If DateDiff("h", Application("MegaTokyoUpdated"), Now()) >= 2 _
    Or Request.QueryString("force") <> "" Then
    Dim objXML
    Dim objItemList
    Dim objItem
    Dim strHTML
    Set objXML = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
    objXML.async = False
    objXML.setProperty "ServerHTTPRequest", True
    objXML.Load("http://www.megatokyo.com/rss/megatokyo.xml")
    'objXML.Load(Server.MapPath("megatokyo.xml"))
    If objXML.parseError.errorCode <> 0 Then
       Response.Write "<pre>" & vbCrLf
       Response.Write "<strong>Error:</strong> " & objXML.parseError.reason
       Response.Write "<strong>Line:</strong>  " & objXML.parseError.line & vbCrLf
       Response.Write "<strong>Text:</strong>  " _
          & Server.HTMLEncode(objXML.parseError.srcText) & vbCrLf
       Response.Write "</pre>" & vbCrLf
    End If
    Set objItemList = objXML.getElementsByTagName("item")
    Set objXML = Nothing
    For Each objItem In objItemList
       ' MegaTokyo Feed childNodes: 0=title, 1=link, 2=description
       strHTML = strHTML & "<p>" & vbCrLf
       strHTML = strHTML & "<a href=""" & objItem.childNodes(1).text & """>"
       strHTML = strHTML & "<strong><em>" & objItem.childNodes(0).text
       strHTML = strHTML & "</em></strong></a><br />" & vbCrLf
       strHTML = strHTML & Replace(objItem.childNodes(2).text, "<br>", "<br />") & vbCrLf
       strHTML = strHTML & "</p>" & vbCrLf
    Next
    Set objItemList = Nothing
    Application.Lock
    Application("MegaTokyoContent") = strHTML
    Application("MegaTokyoUpdated") = Now()
    Application.UnLock
End If
%>
<%= Application("MegaTokyoContent") %>
<!--<%= Application("MegaTokyoUpdated") %>-->
</body>
</html>

The Results

I did very little work on the formatting, since anyone who uses the script will probably want to modify the output to match their site. The output is pretty much plain vanilla HTML, but here's what it looks like anyway:

[piro] - "parts and pieces"
[piro]
posted: 06.22.2004 [1:18 am EST]

After finishing the comic last night, i sat down and foolishly watched the first two episodes of Samurai 7, a new anime series by Gonzo roughly based on Akira Kurosawa's classic film Seven Samurai. Sorta like Ghost in the Shell meets an old fasion samurai flick...[read rant]

[dom] - "In the Shadow of Giants"
[dom]
posted: 00.21.2004 [1:00 am PST]

So I'm typing this rant with one hand (no jokes until you finish the sentence, please) because with my left hand, I'm playing City of Heroes. I have 89 more minutes to rescue a group of mystics from the nefarious clutches of the Circle of Thorns, and I haven't a moment to lose--if I dillydally even a moment, they may not survive the night... [read rant]

Comic [574] posted - "player statistics"
[piro]
posted: 06.21.2004 [1:12 pm EST]

Comic [574] - "player statistics"

posted

[piro] - "Pulling a smile"
[piro]
posted: 06.19.2004 [6:42 pm EST]

One of the things that I've noticed about myself is that I am very much a creature of habit. For instance, when I find a dish at a restaurant that I like, I will order the same thing every single time I go there. Habits are hard to change, it's a lot like trying to divert a stream - it takes a lot of digging, engineered fill and concrete pipes to encourage it to go somewhere else...[read rant]

[seraphim] - "Cart Chunking"
[seraphim]
posted: 06.19.2004 [6:40 pm EST]

The last rant I wrote late at night was a disaster. I was so embarrassed when I went back later and saw all the typos, misspellings, and dropped words. Piro proofread it for me too. Last time I ask him....[read rant]

RSS feed goes live on MT site
[piro]
posted: 06.19.2004 [04:16 pm EST]

I suppose the best way to test this thing is to actually let it loose in the wild and see how much damage it does. ^^;;

I'm anticipating a lot of 'how do i use this RSS thing?' questions that the link i provided will not really answer adequately for the less technically savvy, so any suggestions of websites and readers to refer people to will be welcome.

Welcome to the new Megatokyo RSS feed!
[piro]
posted: 06.19.2004 [03:33 pm EST]

Hi, welcome to the first implimentation (read as "manually updated and maintained") RSS feed for Megatokyo.

It's a pretty simple feed for now, but the intent is to experiment to see how well this can work for the site. Basically, by keeping an eye on this feed, you will be alerted to changes on the Megatokyo website (new comic, new rants) the Fredart site (new sketches, etc) and the Megagear store (when it finally opens :P)

This is a manually updated RSS feed, so i will have to update things by hand for a while, but when the new site is done, most things should be automated.

All feedback and comments appreciated. I hope you find this new feature to the Megatokyo website useful!

Note that the above is not being generated dynamically. It's just a sample run of the code from which I cut and pasted the HTML.

Alternate Format

Now that MegaTokyo is publishing thumbnails, I've hacked (meaning bad code... I'm not responsible when it breaks!) together a new format for my homepage. Here's a sample:

Comic [608] - "Endgames - depopulation and wayfinding"
One Shot Episode, number 46 - Gameworlds: "Endgames" comic special
[click to read]

[piro]
posted: 09.13.2004 [12:45 am EST]
Comic [607] - "Can't go there..."
chapter 5, episode 74
[click to read]

[piro]
posted: 09.10.2004 [12:45 am EST]
Comic [606] - "watching people play"
chapter 5, episode 73
[click to read]

[piro]
posted: 09.08.2004 [4:28 pm EST]
Comic [605] - "battles should be fought to be won"
chapter 5, episode 72
[click to read]

[piro]
posted: 09.06.2004 [2:20 am EST]
Comic [604] - "I put myself here, I'll stay here"
chapter 5, episode 71
[click to read]

[piro]
posted: 09.03.2004 [1:00 am EST]

As before, this is not live. It's also just a static copy of a sample run. I've also cached the images locally for the sake of MT's servers... they do enough work!

You can get a copy of the hacked version from here: mt_rss_hack.zip (1.1 KB), but if you want to read or use the code for anything else, please check out the original version below first.

Download

For those who don't like cutting and pasting, you can download a zip file containing the code from here: megatokyo_rss.zip (1.9 KB)

Related Information


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers