if you want to get all tech news update (or php news, did i forget to say python news or web 2/3/x news??) in one place, what could be better than a twitter account or a RSS feed reader. If you have a RSS feed aggregator, its plain and simple. But if you choose the other one (I mean Twitter) you have to be something more than a dumbass, as you have to create it by your own- lol!
so let me be something more than a “dumbass” today and show you how did i do it. i choose my all time best tool “php” for it. added some toppings with an external rss mashup builder and finally served the dish with twitter api. lets see what was the difficult part in it
goal:
you will provide a set of rss feed urls, then your application will create a mashed up feed with them (all in one place, sorted in ascending order by date), and publish the latest feed titles since last time update.
challenges
0. building a mashup with rss feed
1. keeping track of last time update and separate items which are new
3. post them to twitter
4. setting up cron to run it periodically
getting hand dirty
step 1: creating a rss feed mashup
so lets build a mashup of your favorite rss feed urls. you can make use of yahoo pipes for this or take help of any other web based mashup service. for this one, i experimented with the one available at xfruits.com. go there, open an account and add your feed urls. xfruit will give you a single feedurl which will deliver the mashed up content, sortedn in ascending order by date. so this part is done.
[update]
I tried with Xfruit but it doesn’t work well, most of the time it is down – and their update frequency is slow. so i build the mashup with yahoo pipes. here is the source
–
after making a mashup of techcrunch, phpdeveloper, insidefacebook, ajaxian and honeytechblog my final mashed up feed url (via yahoo pipes)is http://pipes.yahoo.com/pipes/pipe.run?_id=bDfi0Pww3hGhpiLVPm7D0g&_render=rss
step 2: coding your little franky, the frankenstein
well, this code makes use of a tinyurl API to shorten the url of each feed story, SimpleXML to parse the RSS feed and extract stories and story-links from it, a text file to keep track of last updated content and twitter API to post them to twitter. here they come one by one.
class.tinyurl.php
[sourcecode lang=’php’]
< ?php
/**
* shorten any url with the help of tinyurl API
*
*/
class TinyUrl
{
static function getTinyUrl($url)
{
$tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url={$url}");
return $tinyurl;
}
}
?>
[/sourcecode]
class.cachemanager.php
[sourcecode lang=’php’]
< ?php
/**
* keeps track of updated items since last execute
*
*/
class CacheManager
{
private $lastHash;
function __construct()
{
$this->lastHash = file_get_contents(“hash.txt”);
}
function getLashHash()
{
return $this->lastHash;
}
function setLastHash($hash)
{
$this->lastHash = $hash;
file_put_contents(“hash.txt”,$hash);
}
}
?>
[/sourcecode]
class.twitter.php
[sourcecode lang=’php’]
< ?php
/**
* update user's status in twitter
*
*/
class Twitter
{
private $username;
private $password;
function __construct($username, $password)
{
$this->username = $username;
$this->password = $password;
}
function publishMessage($message)
{
try{
$tweetUrl = “http://{$this->username}:{$this->password}@www.twitter.com/statuses/update.xml”;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $tweetUrl);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, “status={$message}”);
$result = curl_exec($curl);
//print_r($result);
}
catch(Exception $e)
{
//print_r($e);
error_log($e->getMessage());
}
}
}
?>
[/sourcecode]
now the most interesting part, to make use of all your objects and to cook them properly. here is the code of index.php
index.php
[sourcecode lang=’php’]
< ?php
include_once("class.cachemanager.php");
include_once("class.tinyurl.php");
include_once("class.twitter.php");
$twitter = new Twitter("username","password");
$cm = new CacheManager();
$lastHash = $cm->getLashHash();
$firstHash= “”;
$feedUrl = “http://pipes.yahoo.com/pipes/pipe.run?_id=bDfi0Pww3hGhpiLVPm7D0g&_render=rss”;
$rss = new SimpleXMLElement($feedUrl,LIBXML_NOCDATA, true);
$feedcount = count($rss->channel->item)-1;
$items = array();
/**
* process elements to get a list of items to post in twitter
* also set a check point so that items are not double posted
*/
for($i=0;$i< =$feedcount;$i++)
{
$title = $rss->channel->item[$i]->title;
$link = $rss->channel->item[$i]->link;
//echo “{$title}
{$link}
“;
if($i==0) $firstHash = md5($title);
$hash = md5($title);
if($lastHash!=$hash)
{
$shortlink = TinyUrl::getTinyUrl($link);
$items[] = array(“title”=>$title,”link”=>$shortlink);
}
else
break;
}
/**
* set the checkpoint
*/
$cm->setLastHash($firstHash);
/**
* post to twitter
*/
$items = array_reverse($items);
foreach ($items as $item)
{
$message = “{$item[‘title’]} – {$item[‘link’]}”;
$twitter->publishMessage($message);
echo $message.”
“;
}
echo “Whoa, Done Finally!”;
?>
[/sourcecode]
so you are done coding it. now set it as a cron job (you can use http://www.setcronjob.com if you are looking for a free one) from your cpanel. you are done.
You can check my twitter news updater at http://twitter.com/techupdater