Erik's blog

Code, notes, recipes, general musings

Yahoo! Pipes + PHP = blogroll

leave a comment »

motivation:
I have a bunch of services that output rss feeds. I want to consolidate all the feeds into a single feed, but let an external service handle the overhead of the consolidation, i.e. fetching, caching, etc. Then, I want to display the data in a readable way.

intended audience:

  • some experience w/ PHP
  • very little experience w/ Yahoo! Pipes

solution:

  • see my blogroll pipe in Y! Pipes
  • PHP source code below

notes:

  • While you’re at Pipes, search for “blogroll” to see how others solved the problem

//fn to generate readable estimation of pub date
// @credit: http://www.php.net/manual/en/function.time.php#85481
function elapsed_time($date, $show_less_than_a_minute = false) {
    $seconds = round(abs(time() - strtotime($date)));
    $minutes = round($seconds / 60);

        if ( $minutes <= 1 ) {
            if ( !$showLessThanAMinute ) {
                return ($minutes == 0) ? '< 1 min. ago' : '1 min. ago';
            } else {
                if ( $seconds < 5 ) {
                    return 'less than 5 sec. ago';
                }
                if ( $seconds < 10 ) {
                    return '< 10 sec. ago';
                }
                if ( $seconds < 20 ) {
                    return '< 20 sec. ago';
                }
                if ( $seconds < 40 ) {
                    return '~ 30 sec. ago';
                }
                if ( $seconds < 60 ) {
                    return 'less than a min. ago';
                }

                return '1 min. ago';
            }
        }
        if ( $minutes < 45 ) {
            return $minutes . ' min. ago';
        }
        if ( $minutes < 90 ) {
            return '1 hr. ago';
        }
        if ( $minutes < 1440 ) {
            return round(floatval($minutes) / 60.0) . ' hr. ago';
        }
        if ( $minutes < 2880 ) {
            return '1 day ago';
        }
        if ( $minutes < 43200 ) {
            return round(floatval($minutes) / 1440) . ' days ago';
        }
        if ( $minutes < 86400 ) {
            return '1 mo. ago';
        }
        if ( $minutes < 525600 ) {
            return round(floatval($minutes) / 43200) . ' mo. ago';
        }
        if ( $minutes < 1051199 ) {
            return '1 yr. ago';
        }

        return '> ' . round(floatval($minutes) / 525600) . ' yr. ago';
}

//def fn for sorting items in rss
function reverse_cmp_pub_dates($obj1, $obj2)
{
    $time1 = strtotime($obj1->pubDate);
    $time2 = strtotime($obj2->pubDate);

    if($time1 == $time2)
    {
        return 0;
    }
    elseif($time1 > $time2)
    {
        return -1;
    }
    else
    {
        return 1;
    }
}

//load y! pipes pre-consolidated data
$feed = 'http://pipes.yahoo.com/pipes/pipe.run?_id=gIBkHK_V3RGpmv5oBRNMsA&_render=rss';
$sxml = simplexml_load_file($feed);

//extract items array
$items = $sxml->channel->item;

//sort by pub date
usort($items, 'reverse_cmp_pub_dates');

//pattern for determining origin (see below)
$pattern = '@.*(?<host>flickr|upcoming|delicious|wordpress).*@i';

foreach($items as $item)
{
    //determine origin
    preg_match($regex, $item->guid, $matches);
    $host_name = strtolower($matches['host']);

    switch($host_name)
    {
        case 'flickr':

            $img_html = substr($item->description, strpos($item->description, '<img'));//extract html beginning w/ img tag

            $desc = substr($img_html, 0, strpos($img_html, '/>'));//trim trailing html

            $host_url = 'http://www.flickr.com/photos/erikeldridge/';
            break;

        case 'upcoming':
            $desc = substr($item->description, 0, 100);
            $host_url = 'http://upcoming.yahoo.com/user/283279/';
            break;

        case 'delicious':
            $desc = $item->description;
            $host_url = 'http://delicious.com/erikeldridge';
            break;

        case 'wordpress':
            $desc = $item->description;
            $host_url = 'https://erikeldridge.wordpress.com/';
            break;

        default:
    }

    $data_arr[] = array(
        'host' => array(
            'url' => $host_url,
            'name' => $host_name
        ),
        'item' => array(
            'desc' => $desc,
            'title' => $item->title,
            'date' => elapsed_time($item->pubDate),
            'link' => $item->link,
        )
    );
}

//to display, loop through data_arr and print contents
Advertisements

Written by Erik

November 23, 2008 at 8:07 pm

Posted in code

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: