Friday freebies – Adding RSS data to a site’s front page.


This would be index.pl .htaccess would be set to read this file if index.html did not exist.

#!/usr/bin/perl
#
# (C) 2010, Wayne Smith
#
### If the feed is not static we can do a get request to pick it up.
#  use LWP::UserAgent;
#  $ua = new LWP::UserAgent;
#  $ua->agent(“HomePage”);
#
### There is simple instead of full blown LWP::UserAgent, HomePage UA helps to know its my own request in the log files
#
#  $url = “https://wsmithdesign.wordpress.com/feed/”;
#    $req = new HTTP::Request ‘GET’ => $url;
#    $req->header(‘Accept’ => ‘text/html’);
#   $page = $ua->request($req)->as_string;
#   $page =~ /\n\n/is;
#   $xml = “$'”;

print “Content-type: text/html\n\n”;

# get html

open(HTML, “html.template”);
$good = read HTML, $html, 250000;
close(HTML);

open(HTML, “feed.xml”);
$good = read HTML, $xml, 250000;
close(HTML);

# insert RSS feed in the document where it says “FeedGoesHere”

$html =~ /FeedGoesHere/is;
print “$`\n”; $html = “\n$'”;

$count=10;

# there are xml parser libs for perl but this is simple … We get the item, inside the item we get the link and title. Sometimes more code makes the code easier to read.

while ($count) {
$count = $count – 1;
$xml =~ /<item>(.+?)<\/item>/is;
$xml = “$'”;
$item = “$1”;
$item =~ /<link>(.+?)<\/link>/is;
print “\n<li> <a href=\”$1\”>”;
$item =~ /<title>(.+?)<\/title>/is;
print “$1</a>”;
}

# Now finish document

print “$html”;

Note: feed must be a trusted and valid source! One assumes here your own feed.

Advertisements

One response

  1. […] preference is to use Perl but if you are adding information to a sidebar or other portion of a PHP page or PHP application […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: