You are not logged in.

Announcement

Support Has Moved!

SimplePie support has moved to Yahoo! Groups, and bug tracking has moved to bugs.simplepie.org. This support site will remain available as a read-only archive for the foreseeable future.

#1 20 November 2006 05:24:44

Laurentvw
New member
Registered: 20 November 2006
Posts: 4

Only display feeds from today

Hey,
I'm using Simplepie Beta 3 (I think) and also the multifeeds package.
I'd like to only display the feeds from today (from the last 24h is better). Currently, I sometimes see some feeds from 1 or 2 days ago, which I would like to strip out. So, what I'm looking for is, a piece of code that only displays all feeds from the last 24 hours. As I don't know php very well, I'm hoping for someone to help me. Thanks smile

-Laurent

Last edited by Laurentvw (20 November 2006 07:39:53)

Offline

 

#2 24 November 2006 12:03:50

Skyzyx
Creator and Co-Developer
From: Silicon Valley, CA, USA
Registered: 30 January 2006
Posts: 1567
Website

Re: Only display feeds from today

Something like this should do the trick (only displays feeds from the last 24 hours, give or take an hour based on cache life):

Code:

<?php
include('simplepie.inc');
//$feed = new SimplePie('http://digg.com/rss/index.xml');
$feed = new SimplePie('feed://www.tuaw.com/rss.xml');

// Display the name of the feed
echo '<h1>' . $feed->get_feed_title() . '</h1>';

// Create a new array to hold data in
$new = array();

// Loop through all of the items in the feed
for ($x=0; $x < $feed->get_item_quantity(); $x++) {

    // Make sure that we have a handle for the current item.
    $item = $feed->get_item($x);

    // Calculate 24 hours ago
    $yesterday = time() - (24*60*60);

    // Compare the timestamp of the feed item with 24 hours ago.
    if ($item->get_date('U') > $yesterday) {

        // If the item was posted within the last 24 hours, store the item in our array we set up.
        $new[] = $item;
    }
}

// Loop through all of the items in the new array and display whatever we want.
foreach($new as $item) {
    echo '<h3>' . $item->get_title() . '</h3>';
    echo '<p>' . $item->get_date('j M Y, H:i:s O') . '</p>';
    echo $item->get_description();
    echo '<hr />';
}
?>

SimplePie 1.0 | Trunk Demo | SimplePie Twitter | My Twitter | My LinkedIn

I'm currently working 2 full-time jobs, plus SimplePie. Responses will be lighter until February-ish 2008, but I expect to be available 1-2 times a week to answer questions. To get responses faster, try our IRC chat. irc://irc.freenode.net/simplepie. You must have an IRC client installed.

Offline

 

#3 25 November 2006 02:40:22

focusfriends
SimplePie Superstar
Registered: 31 August 2006
Posts: 66

Re: Only display feeds from today

You can't rely your "only today's items" judgement on an item's pubdate. Lots of feeds give a new pubdate to each item during each generation of the feed. Others use the same static pubdate for all feed items during every feed generation.

The only way to do this properly is to make your own (database) archive of your periodic feed crawls and apply a "signature" to each item so you know if it's new or not. And then you can use your own "insert stamp" in your "only today's items" judgement. Be aware though that you can't rely on the pubdate or item link (guid) in your "new item" judgement.

For more details, send me a personal message.

Offline

 

#4 25 November 2006 12:14:24

Skyzyx
Creator and Co-Developer
From: Silicon Valley, CA, USA
Registered: 30 January 2006
Posts: 1567
Website

Re: Only display feeds from today

focusfriends -- Agreed.  You can't *rely* on the item's date, but this will work in a pinch.  For best results, you should aggregate the data in a database like you said.


SimplePie 1.0 | Trunk Demo | SimplePie Twitter | My Twitter | My LinkedIn

I'm currently working 2 full-time jobs, plus SimplePie. Responses will be lighter until February-ish 2008, but I expect to be available 1-2 times a week to answer questions. To get responses faster, try our IRC chat. irc://irc.freenode.net/simplepie. You must have an IRC client installed.

Offline

 

#5 25 November 2006 12:50:27

Laurentvw
New member
Registered: 20 November 2006
Posts: 4

Re: Only display feeds from today

@ focusfriends: Alright, I will consider that. Thanks for letting me know.

@ Skyzyx: Thanks for the code, it works well as expected.

Offline

 

#6 20 April 2007 04:35:27

Daniel
New member
Registered: 24 March 2007
Posts: 1

Re: Only display feeds from today

Can you please post the full multifeed code unsing the last 24 hours hack; I canīt figure out at which location implementing it... the full php-script would be the easiest way understanding the implementation...

Thanks in advance,
Daniel

Offline

 

#7 20 April 2007 08:58:21

Jolie
Member
Registered: 6 April 2007
Posts: 16

Re: Only display feeds from today

Hi how do i finding the difference between the current date and the date of the item, and applying the css style.

How do i make a function that return the age of the story in minutes, then I just had a big if statement to apply the appropriate style depending on that age.

if ($age < 5) {
    $style = "age1";
} else if (age < 10) {
    $style = "age2"
...

Offline

 

#8 23 April 2007 02:54:13

esteban
New member
Registered: 26 February 2007
Posts: 6
Website

Re: Only display feeds from today

focusfriends wrote:

The only way to do this properly is to make your own (database) archive of your periodic feed crawls and apply a "signature" to each item so you know if it's new or not. And then you can use your own "insert stamp" in your "only today's items" judgement. Be aware though that you can't rely on the pubdate or item link (guid) in your "new item" judgement.

I am looking into applying some functionality to new items and I also considered a database.  However, the problem of what data is new exists at a fundamental level.  Each time the Feed is updated it might only include one new item, but at this stage I don't know what is new and what is old, this is pre-database data, raw from SimplePie.  So how do I know what data to put into the database?  Do I put it all in and risk duplicates?  Or do I look at the pubDate and not put anything in that already has that pubDate?  It seems we already dismissed the pubDate as being inaccurate. 

So the question stands, how can we know what item is fresh and what item is old?  I suppose we could test titles to see if they are unique before putting the item into the database, but that feels a little like a hack. 

Some WordPress plugins that take feeds and submit them into WordPress as stories seem to know what is a new story and what is old, I wonder how they do that?

Last edited by esteban (23 April 2007 02:54:53)

Offline

 

#9 23 April 2007 15:22:24

andy7629
Member
Registered: 22 March 2007
Posts: 10

Re: Only display feeds from today

Another question...

How would I go about implementing this into multifeed?

Offline

 

#10 23 April 2007 23:02:28

Skyzyx
Creator and Co-Developer
From: Silicon Valley, CA, USA
Registered: 30 January 2006
Posts: 1567
Website

Re: Only display feeds from today

We're addressing a number of these issues in 1.0:

1) Multifeeds support is integrated directly into the core so there's no more hacking the data together.

2) SimplePie parses out the data and provides an API for accessing that data.  If you want to build an aggregator, it is up to you to determine what is new and what is old based on your own database solution.

Many people use the <guid> value ($item->get_id()) to keep track, but currently if there is no <guid>, get_id() returns false.  One thing that still needs to be implemented, but is still on-track for 1.0 is that when a guid is not available, SimplePie will generate a unique guid hash from a combination of the title, permalink URL, and pubDate of the item.  That should allow a fairly reliable, unchanging unique identifier for the news item in question.

In the meantime, I would suggest generating a hash of your own using the aforementioned pieces of data and either the md5() or sha1() PHP functions.


SimplePie 1.0 | Trunk Demo | SimplePie Twitter | My Twitter | My LinkedIn

I'm currently working 2 full-time jobs, plus SimplePie. Responses will be lighter until February-ish 2008, but I expect to be available 1-2 times a week to answer questions. To get responses faster, try our IRC chat. irc://irc.freenode.net/simplepie. You must have an IRC client installed.

Offline

 

#11 23 April 2007 23:49:52

Skyzyx
Creator and Co-Developer
From: Silicon Valley, CA, USA
Registered: 30 January 2006
Posts: 1567
Website

Re: Only display feeds from today

Code to separate older posts vs. posts from the last 24 hours:

Code:

<?php
/*************************************************************
DEMO: SEPARATING TODAYS POSTS FROM PREVIOUS POSTS
We'll give current items a classname of "current", and older 
items will get a classname of "older".

This demonstration will use PHP short-tags (because I'm feeling 
lazy tonight), although you can manually convert them to the 
standard tags if you want:

<?=$feed->get_title()?> == <?php echo $feed->get_title(); ?>
*************************************************************/

// Require SimplePie
require_once('../simplepie.inc');

// Calculate 24 hours ago
$yesterday = time() - (24 * 60 * 60);

// Create new instance of SimplePie object
// http://simplepie.org/docs/reference/simplepie-object/
$feed = new SimplePie('http://uneasysilence.com/feed/');

// Automatically set the proper HTTP headers.
$feed->handle_content_type();

// Begin (X)HTML-formatted code.
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">    
<head>
    <title>Demo Page</title>

    <style type="text/css">
    body {
        font:14px/1.5em Verdana, sans-serif;
        background-color:#fff;
        color:#000;
        padding:0 50px;
    }

    div {
        border-top:1px solid #000;
        padding:5px 30px;
    }

    div.current {
        background-color:#efe;
        color:#000;
    }

    div.older {
        background-color:#eee;
        color:#999;
    }

    div.older a {
        text-decoration:underline;
        color:#666;
    }
    </style>
</head>

<body>
    <h1><a href="<?=$feed->get_permalink()?>"><?=$feed->get_title()?></a></h1>
    <p><?=$feed->get_description()?></p>

    <? foreach($feed->get_items() as $item): ?>

    <?
    // Let's get the date of the post in seconds since Unix epoch (since that's how $yesterday (above) is calculated)
    $post_date = $item->get_date('U');

    // This will be the classname by default.
    $classname = 'older';

    // If the date of the post is newer than 24 hours ago, change the classname to 'current'.
    if ($post_date > $yesterday)
    {
        $classname = 'current';
    }
    ?>

    <div class="<?=$classname?>">
        <h4><a href="<?=$item->get_permalink()?>"><?=$item->get_title()?></a></h4>
        <?=$item->get_description()?>
        <p>Posted on <?=$item->get_date('l, j F Y, g:i a')?></p>
    </div>

    <? endforeach; ?>

</body>
</html>

SimplePie 1.0 | Trunk Demo | SimplePie Twitter | My Twitter | My LinkedIn

I'm currently working 2 full-time jobs, plus SimplePie. Responses will be lighter until February-ish 2008, but I expect to be available 1-2 times a week to answer questions. To get responses faster, try our IRC chat. irc://irc.freenode.net/simplepie. You must have an IRC client installed.

Offline

 

#12 24 April 2007 01:02:14

esteban
New member
Registered: 26 February 2007
Posts: 6
Website

Re: Only display feeds from today

Thanks guys, sounds like v1.0 with a proper API will be awesome.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson