Display X most recent Twitter entries

Just thought I'd post a little block code for displaying your X most recent Twitter entries. You need to add your Twitter rss feed to the aggregator, and then modify the code below with your feed ID.

<?php
$fid
= 4; // Add your Twitter rss feed to the aggregator, then put the feed id here
$limit = 5; // Change this to the number of entries you'd like to show
$result = db_query('SELECT description, timestamp FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,'.$limit, $fid);
print
'<ul>';
while(
$row = db_fetch_object($result)) {
    print
'<li><div class="my-twitter-status">'.substr($row->description, 4).'</div>'; // I removed the first 4 characters from the  description, as I didn't want each line to read 'Al: did this'... is there a setting I can change in my Twitter account for this prefix to be removed?
   
print '<div class="my-twitter-status-time">'.twitter_ago($row->timestamp).'</div></li>';
}
print
'</ul>';

// Thanks to drenei for posting this function - drupal.org/node/61565
function twitter_ago($timestamp){
  
$difference = time() - $timestamp;
  
$text = format_interval($difference, 2) . " ago";
   return
$text;
}
?>

Linking to the tweet URL

victortrac - March 26, 2008 - 20:07

Thanks for this.. saved me a few minutes. I decided to add the link to the tweet as well:

<?php
$fid = 5;
$limit = 5;

$result = db_query('SELECT description, timestamp, link FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,' . $limit, $fid);

print '<ul>';
while($row = db_fetch_object($result)) {
  print '<li><div class="my-twitter-status"><a href="'.$row->link.'">'.substr($row->description, 12).'</a></div>';
  print '<div class="my-twitter-status-time">'.twitter_ago($row->timestamp).'</div></li>'."\n";
}
print '</ul>';

function twitter_ago($timestamp){
   $difference = time() - $timestamp;
   $text = format_interval($difference, 2) . " ago";
   return $text;
}

Use Feedapi

Lynn - April 5, 2008 - 22:02

You can also use feedapi module: http://drupal.org/feedapi . You will also need views module for this approach.

Install feedapi module and enable it and its submodules (simplepie parser and feedapi node). Follow the instructions for simplepie parser installation which requires downloading the simplepie class.

Create a new content type called e.g. 'Tweet'.
Add a new 'feed' node (feedapi adds a new content type called 'feed'). Include the link to your twitter rss feed in this node. In the 'processers' fieldset assign the incoming feed items to the content type 'Tweet.'

Now each of your tweets will be saved as a node of type tweet.
Create a new block view which filters on Node type='tweet'. In the view block footer you can add a link to "Follow me".

should be...

hayesr - June 27, 2008 - 17:08

Linking to the tweet URL and turning text links into hrefs

tizzo - April 8, 2008 - 04:04

I find myself posting links often and didn't want to make my readers paste the link into their address bars. This update adds a regexp that parses the tweet and turns any text starting with http into a link. I moved the link to the original tweet into a link below the update but above the display of the time since the update.

<?php
$fid
= 1; // Add your Twitter rss feed to the aggregator, then put the feed id here
$limit = 5; // Change this to the number of entries you'd like to show

$result = db_query('SELECT description, timestamp, link FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,' . $limit, $fid);

print
'<ul>';
while(
$row = db_fetch_object($result)) {
 
$tweet = substr($row->description, 6);  // Change this to the number of characters to remove from the beginning of each post.  This allows you to remove username: from the beginning of each post.
 
$tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $tweet);
  print
'<li><div class="my-twitter-status">'.$tweet.'</div><div class="tweet"><a href="'.$row->link.'">Tweet</a></div>';
  print
'<div class="my-twitter-status-time">'.twitter_ago($row->timestamp).'</div></li>'."\n";
}
print
'</ul>';

function
twitter_ago($timestamp){
  
$difference = time() - $timestamp;
  
$text = format_interval($difference, 2) . " ago";
   return
$text;
}
?>

Linking to other Twitterers

tizzo - June 18, 2008 - 19:03

I also wanted to be able to provide links to the people that I messaged in my tweets (making any instance of @SomeUserName link back to http://www.twitter.com/SomeUserName). After a little more regexp magic this snippet does that.

<?php
$fid
= 1; // Add your Twitter rss feed to the aggregator, then put the feed id here
$limit = 5; // Change this to the number of entries you'd like to show

$result = db_query('SELECT description, timestamp, link FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,' . $limit, $fid);

print
'<ul>';
while(
$row = db_fetch_object($result)) {
 
$tweet = substr($row->description, 6);  // Change this to the number of characters to remove from the beginning of each post.  This allows you to remove username: from the beginning of each post.
 
$tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $tweet);
$tweet = ereg_replace("@[^<>[:space:]]+[[:alnum:]/]","<a href=\"http://www.twitter.com/\\0\">\\0</a>", $tweet);
$tweet = preg_replace('/@/', '${1}${3}', $tweet, 1);
  print
'<li><div class="my-twitter-status">'.$tweet.'</div><div class="tweet"><a href="'.$row->link.'">Original Tweet</a></div>';
  print
'<div class="my-twitter-status-time">'.twitter_ago($row->timestamp).'</div></li>'."\n";
}
print
'</ul>';

function
twitter_ago($timestamp){
  
$difference = time() - $timestamp;
  
$text = format_interval($difference, 2) . " ago";
   return
$text;
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.