One of the things I really like about gamedev.net is their Image of the Day Gallery. The thing I don’t like about it is that there isn’t an RSS feed so I occasionally have to go through the gallery and view what I haven’t seen. But as I become more and more dependent on RSS feeds to bring me the information I found it harder to get to these site (mostly because of my hectic schedule) so I decided I should write my own RSS feed. (For anyone who is interested the feed can be found at http://feeds.feedburner.com/GamedevnetIotdGallery.)

Below is the Perl code that creates the feed. It’s basically just a few steps:

  1. Download the main gallery page using LWP.
  2. Read in the text file with the old matches. I stored the information in a text file because I will move this to other computers and if I can just move the support files I don’t have to worry about the computer having a SQL server.
  3. Use a regular expression to find the IOTD posts. For every link I check the old results to see if we have already added this to the page. I added the new result at the start of the results because that we want the newer items to show up first in the RSS feed.
  4. Dump the results to disk. That way we don’t keep adding the same files.
  5. Build a minimum RSS feed. I just added the fewest fields needed for it to validate. There are a lot of things that could be added to this like the date that it was downloaded and maybe a short description but I wanted to keep it as lean as possible.

I wanted to have an easy way to subscribe to the feed so I set it up in Feedburner. That way I can easily move my feed without having to worry about the subscriptions.

#!/usr/bin/perl -w
# This script will download new GameDev.net IOTD pages.

use LWP::Simple;
use strict;

# the start page
my $strURL = "http://www.gamedev.net/community/forums/gallery.asp?forum_id=62";
my $strBase = "http://www.gamedev.net/community/forums/";

# check to see if a different page was sent
if(defined($ARGV[0])){
    $strURL = $ARGV[0];
}

# download the page
my $strPage = get $strURL or die "Unable to download $strURL";

# get the old results
my $strMatches = "";
my $bOpen = 1;

open FILE, "results.txt" or $bOpen=0;
if($bOpen == 1){
    while(<FILE>){
	$strMatches .= $_;
    }

    close FILE;
}

my $intNew = 0;

# get the urls
while($strPage =~ /<br>\s+<a href="([^"]+?)" title="[^"]+?">\s+<img src="[^"]+?">\s+<br>/gsi){
    my $strMatch = $1;
# check to see if we have already found this
    if((rindex $strMatches, $strMatch) > 1){
	$strMatches = "$strBase$strMatch\n$strMatches";

	# increment the count of new items
	$intNew++;
    }
}

# check to see if there was a change
exit if($intNew == 0);

# write out the results
open FILE, ">results.txt" or die "Unable to open file to dump results\n";
print FILE $strMatches;
close FILE;

# build the RSS feed
my $strFeed = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$strFeed .= "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
$strFeed .= "<channel>\n";
$strFeed .= "<title>GameDev.net IOTD Gallery</title>\n";
$strFeed .= "<link>http://www.gamedev.net/community/forums/gallery.asp?forum_id=62</link>\n";
$strFeed .= "<description>Pulling Information about the Gamedev.net iotd gallery.</description>\n";
$strFeed .= "<atom:link href=\"\" rel=\"self\" type=\"application/rss+xml\" />";

# build the items
my $intCount = 0;
while(($strMatches =~ /(\S+)/gsi) and ($intCount < 11)){
    $strFeed .= "<item>\n";
    $strFeed .= "<link>$1</link>\n";
    $strFeed .= "<guid>$1</guid>\n";
    $strFeed .= "<title>Image Added</title>\n";
    $strFeed .= "</item>\n";
    $intCount++;
}

$strFeed .= "</channel>\n";
$strFeed .= "</rss>\n";

# save the feed
open FEED, ">gamedeviotd.xml" or die "Unable to open eed to dump results.";
print FEED $strFeed;
close FEED;