You are not logged in.
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.
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 ![]()
-Laurent
Last edited by Laurentvw (20 November 2006 07:39:53)
Offline
Something like this should do the trick (only displays feeds from the last 24 hours, give or take an hour based on cache life):
<?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 />';
}
?>Offline
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
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.
Offline
@ focusfriends: Alright, I will consider that. Thanks for letting me know.
@ Skyzyx: Thanks for the code, it works well as expected.
Offline
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
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
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
Another question...
How would I go about implementing this into multifeed?
Offline
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.
Offline
Code to separate older posts vs. posts from the last 24 hours:
<?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>Offline