Zend Feed for a Swedish web development blog aggregator
Lately I have begun to get more and more annoyed with my feed reader. I use a simple feed reader which lack most features except for subscribing to and reading feeds. The one thing that annoys me the most is that it can’t aggregate several feeds into one feed. Yesterday I finally decided to scratch that itch.
What I want to aggregate are Swedish web development and web entrepeneur blogs. I like the planet-planet concept that for example planet-php use. (Though I don’t think planet-php use python…) Conveniently I had this idn domain, översikt.se, lying around which I haven’t used up until now. As is normal nowadays (for me at least) I used Zend Framework to build the site. It was the first time I used Zend_Feed class. I works like a charm.
The whole website logic (both model and controller) is about 200 lines of code. And the main logic of fetching the feeds are really nice and short.
foreach($urls as $url) {
try {
$feed = Zend_Feed::import($url['feedurl']);
}
catch(PDOException $ex) {
error_log($ex->getMessage());
continue;
}
$feedTitle = $feed->title;
$feedLink = $feed->link;
$title = '';
$link = '';
$desc = '';
$pub = '';
foreach ($feed as $item) {
if (is_a($feed, "Zend_Feed_Rss")) {
$title = $item->title();
$link = $item->link();
$desc = $item->description();
$publ = ($item->pubDate()) ? $item->pubDate() : $item->date();
}
else if(is_a($feed, "Zend_Feed_Atom")) {
$title = $item->title();
$link = $item->link('alternate');
$desc = $item->content();
$publ = ($item->issued()) ? $item->issued() : $item->published();
}
else {
error_log("Feed <$feedid> is an unsupported format");
continue;
}
// Parse and normalize date and check if new
$time = strtotime($publ);
$publ = date("c", $time);
// Execute the previously prepared sql insert statement
$result = $sti->execute(array(
'feedid' => $url['feedid'],
'title' => $title,
'link' => $link,
'publ' => $publ,
'desc' => $desc));
}
}
I skipped database exception and error handing in the above code to make it even more readable. Zend_Feed have failed me only once when trying to read and parse an old weird rss format. Wonderful.
(For those that don’t have browsers that support idn-domains I linked to the punycode variant of the domain name översikt.se, www.xn--versikt-80a.se, above. But mostly because my damn editor refuses to use the Swedish letter “ö” in the href attribute.)
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
