Multiple feed aggregation with Magpie

17 May 2005 @ early morning | Comments (2)

After a small project that required some simple MagpieRSS usage, I found it a little unclear as to how to merge mulitple feeds together into one master date-sorted feed. An article posted on the Magpie Blog provided most of what I needed, but still didn’t combine the tips (as far as I could tell) to allow for merging/sorting of both RSS and Atom.

The missing link was to combine the two provided date_cmp() functions into a function that would work for both Atom and RSS date formats. Here is the modified function:

// sorts feed array based on published date (used with usort)
// -------------------------------------------------------------
    function date_cmp($a, $b)
    {
        $atime = (empty($a['date_timestamp']))
            ? strtotime($a['dc']['date'])
            : $a['date_timestamp'];

        $btime = (empty($b['date_timestamp']))
            ? strtotime($b['dc']['date'])
            : $b['date_timestamp'];

        if ($atime == $btime)
            return 0;
        return ($atime > $btime) ? -1 : 1;
    }

It seems to work pretty well, although one thing to keep in mind is that Magpie will use the last modified date for sorting, not the publish date. There may be a slightly more elegant way to accomplish the above, particularly because each MagpieRSS object has a "feed_type" property (Atom|RSS), but aside from any glaring omissions this works for me.

Here is an example file that brings together the methods outlined on the Magpie blog, along with the modified date_cmp() function. You’ll need to edit it to suit your needs. It’d probably work best as a function that returns the html you need for inclusion, rather than just echoing it out as this example does.


2 comments

1

I hate magpie rss a lot of bugs. I prefere to use Java script for Headlines news on miy site

Melissa → allabouttheworld.com/
2

hey, thanks a million for this code, it’s exactly what i needed. however, i’d love to tweak it a little bit to my specifications. any chance you can email me and help me out? thanks!

jay

Comments are closed for this article.

Previously