Tweetlight: the best twitter parser to show their tweets | Flavio's blog
 

Tweetlight: the best twitter parser to show their tweets

Let's be honest: il widget twitter widget to show, on your site or blog, our latest tweets (or the timeline), is bad enough.

It does not allow the customization of every aspect, ed è in javascript, which does not suit everyone.

In a bit of free time I have written a parser twitter (that is, a code that shows the last tweets of a user), and I called Tweetlight – A lightweight json twitter parser.

It is written in PHP and uses the stream containing the latest tweets in json format.

Thanks to a cache, you can set the number of minutes after which you must go and check if there are new tweets (10 minutes are okay).
In this way the loading is very fast, because the tweets are read from a cache file (script execution time: less than 2 milliseconds).

Tweetlight is composed of three rows:

  • tweetlight.php: The actual code, in php.
  • tweetfunc.inc: Functions required by tweetlight.php
  • output.php: The file shows the final result, l’output, that can be pasted where you want.

In the first file, need to enter your twitter username in $ username, the time duration of the cache in the field $ expiretime, and the number of tweets to display in the field $ number_tweets.

We see a preview of this file:

<?
/* Tweetlight - A lightweight json twitter parser (mean execution ltime less than 2ms, using cache).
Set your user name in $username, the number of minutes you want to refresh the cache after in $expiretime (default 10 minutes), and the number of tweets to show in $number_tweets.
You can customize the output as you want, this script is meant to be fast, so it doesn't carry any fancy customization.
last rev: 20-apr-11 by flapane.com */

include('tweetfunc.inc'); // functions
$username='flapane'; 	 // insert your twitter user name
$format='json'; 		 // format 
$expiretime=10; 		 // cache expire time in minutes
$number_tweets=5; 		 // number of tweets to display
$cachename="tweetcache_".$username.".json"; //name of the cachefile

//create the cachefile if it doesn't exist
if (!file_exists($cacheName)) {
     $create = fopen($cacheName, 'W');
 	 chmod ("$cacheName", 0777); //set chmod 777
	 fclose($create);
}

// Is the file older than $expiretime, or the file is new/empty?
$FileAge = time() - filectime($cacheName);	// Calculate file age in seconds
if ($FileAge > ($expiretime * 60) || 0 == filesize($cacheName))
{
	// Now refresh the cachefile with newer content (in number equal to $number_tweets)
	$handle = fopen("http://api.twitter.com/1/statuses/user_timeline/{$Username}.{$format}?count=$number_tweets", "r+");  
	$jsoncontent = stream_get_contents($handle);
	fclose($handle);
	$handle = fopen($cacheName, 'wb');
	fwrite($handle, $jsoncontent);			
}

$tweet=json_decode(file_get_contents($cacheName)); // get tweets and decode them into a php array

$tweet_avatar = $tweet[0]->user->profile_image_url; //display your twitter avatar

echo "<img src='$tweet_avatar' width='30' height='30' alt='twitter avatar' /><b> Last ".$number_tweets." tweets for ".$username."</b><br /><br />";

//output $number_tweets tweets from cache file	
for ($i=0; $I<=($number_tweets-1);$i  ) 
{
$tweet_text = $tweet[$I]->text; //the tweet text
$tweet_link = $tweet[$I]->id_str; //twitter status link
$posted_at = explode(" ", $Tweet[$I]->created_at, 6); //twitter date
$some_date = "$posted_at[0] $posted_at[1] $posted_at[2] $posted_at[3] $posted_at[4] $posted_at[5]";
$postedago = adjust_twitter_date($some_date) ."<br />"; //adjust the twitter date output

echo "<div id='tweet_text'>".$tweet_text = preg_replace("/(?!(?:[^<]+>|[^>]+<\/A>))\b((http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*))\b/is", "<a href=\"\\1\" target=\"\\_blank\">\\1</A>",$tweet_text)."<br /></div>"; //linkify the links in the tweet, and output the tweet

echo "<div id='tweet_date'><b><a href='http://twitter.com/$username/statuses/$tweet_link'># Posted ".human_time_diff(time(),$postedago) ." ago</A></b></div><br />"; //output the tweet age
}
?>

For anyone who wants to understand how it works, There are comments around each function and variable main, ed in breve:

  • It takes the stream format json
  • Check if the cache file already exists
  • If the cache file is recent, without using the update
  • If the cache file is old, update it
  • Show the number of tweets you want

The file should not be touched tweetfunc.inc, serves for the operation of the script.

The first time you run the script, will create a cache file of the type tweetcache_USERNAME.json

This is a screenshot of the output as it will appear:

Screenshoot di tweetlight, twitter parser

Screenshoot di tweetlight, here is how the output stark.

As you can see, the script is “knot and crude”, without customization via CSS, so that it is the simplest, fast, and can be modified.

It's up to you, Se volete, changing their appearance, perhaps adding a background.

You can download the zip file containing the script (And upload it to your ftp server) to this address: Tweetlight – A lightweight json twitter parser.

Se volete, write a comment with your opinion!

 

3 Comments »

Please accept third-party cookies to be able to comment on the post! The CHANGE COOKIE CHOICES button is located in the footer of the site. / In order to comment this post, please accept the third party cookies! The button CAMBIA LE SCELTE DEI COOKIE is in the footer of the website.