Source

webapps / toplinks.txt

<?php
/*
toplinks.php
Author: Thejesh GN thejeshgn.com 
Page:
Version: 0.1
Code/Help page: http://thejeshgn.com/tools/twitter-toplinks/

*/

$id="thej";  //your twitter id
$password=""; //your twitter password
$format="json"; 
$count=400; //how many tweets I need to scan
$bitly_key=""; //bit.ly api key
$bitly_id=""; //bit.ly id
$scheduled=true; //do you want to schedule
$output_file_name="index.html";  //what should be the name of the result file. Req only if scheduled=true  


$messages;

class Link {
	var $url;
	var $visits;
	var $tweet;
	var $tweetId;
	var $desc;
	var $longurl;
	var $created_at;
	
	function Link($url, $visits,$tweet,$tweetId,$desc,$longurl,$created_at) {		
		$this->url = $url;
		$this->visits = $visits;
		$this->tweet=$tweet;
		$this->tweetId=$tweetId;
		$this->desc=$desc;
		$this->longurl=$longurl;
		$this->created_at=$created_at;
	}
}

function sortObject($data) {
	for ($i = count($data) - 1; $i >= 0; $i--) {
		$swapped = false;
		for ($j = 0; $j < $i; $j++) {
			if ( $data[$j]->visits < $data[$j + 1]->visits ) {
				$tmp = $data[$j];
                $data[$j] = $data[$j + 1];
                $data[$j + 1] = $tmp;
                $swapped = true;
			}
		}
		if (!$swapped) {
			return $data;
		}
	}
}


$content;
$content ='<html>';
$content .='<head>';
$content .='<title>Recently popular links tweeted by @'.$id.'</title>';
$content .='</head>';
$content .='<body>';
	  $curtime = time();
	  $utcdiff = date('Z', $curtime);  // get difference to UTC in seconds
	  $bmttime = $curtime - $utcdiff + 19800;   // IST = UTC+5.5
$content .='<p><i>Generated at ' .  date("F j, Y, g:i a",$bmttime).'</i><br></p>';

$content .='<h2>Most recent Top links from @'.$id.'</h2>';
$content .='These are the  most recent links Ive pushed through Twitter, ranked by the number of times they have been clicked by readers or retweeters.';
$content .='<br><br>';
$content .='<table cellspacing="0" cellpadding="0"><tr bgcolor="lightgray"><td>';
$content .='<table cellspacing="1" cellpadding="4" >';
				
	$content .='<tr bgcolor="#f5f5f5">';
	$content .='<td valign="top" align="right">Rank</td>';
	$content .='<td valign="top">Description</td>';
	$content .='<td valign="top" align="right">Link</td>';
	$content .='<td valign="top" align="right">Created At</td>';
	$content .='<td valign="top" align="right">Clicks</td>';
	$content .='<td valign="top">&nbsp;</td>';
	$content .='<td valign="top">&nbsp;</td>';
	$content .='</tr>';				

$credentials=sprintf("%s:%s", $id, $password);
$api_url = sprintf("http://twitter.com/statuses/user_timeline.%s", $format);
$api_url .= sprintf("?count=%d", $count);
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $api_url);
curl_setopt($curl_handle, CURLOPT_USERPWD, $credentials);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
$twitter_data = curl_exec($curl_handle);
$http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);
$tweets = json_decode($twitter_data);
$i =0;
$regex='/http:\/\/bit\.ly\/[a-zA-Z0-9]*/';
$regexCom='/http:\/\/bitly\.com\/[a-zA-Z0-9]*/';
foreach ($tweets as $tweet){
	$urls;
	$url="nothing";
	$longurl="nothing";
	$code;
	$click;
	$longurl;
	$desc;
	if(preg_match($regex, $tweet->text, $urls) || preg_match($regexCom, $tweet->text, $urls)){
		$url=$urls[0];
		$code=str_replace("http://bit.ly/", "", $url);
		$code=str_replace("http://bitly.com/", "", $code);
		$bitly_api_url='http://api.bit.ly/stats?version=2.0.1&hash='.$code.'&login='.$bitly_id.'&apiKey='.$bitly_key;
		$curl_handle = curl_init();
		curl_setopt($curl_handle, CURLOPT_URL, $bitly_api_url);
		curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
		$info = curl_exec($curl_handle);
		$http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
		curl_close($curl_handle);
		$stats = json_decode($info);
		$click = $stats->results->clicks;
		
		$bitly_api_url='http://api.bit.ly/info?version=2.0.1&hash='.$code.'&login='.$bitly_id.'&apiKey='.$bitly_key;
		$curl_handle = curl_init();
		curl_setopt($curl_handle, CURLOPT_URL, $bitly_api_url);
		curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
		$info = curl_exec($curl_handle);
		$http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
		curl_close($curl_handle);
		$stats = json_decode($info);
		//var_dump($stats->results->$code->htmlMetaDescription);
		$longurl= $stats->results->$code->longUrl;
		$desc = $stats->results->$code->htmlMetaDescription;
		if($desc){
		}else{
			$desc=$tweet->text;
		}
		$longurl=str_replace("http://", "", $longurl);
		$longurl=str_replace("https://", "", $longurl);
		$longurl=preg_replace("/\/.*/","", $longurl);
		$messages[$i]= new Link($url,$click,$tweet->text,$tweet->id,$desc,$longurl,$tweet->created_at);
		$i=$i+1;
	}
}

$i =0;
$messages = sortObject($messages);	
foreach ($messages as $message){

	if($i%2){
		$content .='<tr bgcolor="#f5f5f5">';
	}else{
		$content .='<tr bgcolor="#ffffff">';
	}
	$content .='<td valign="top" align="right">'.($i+1).'.</td>';
	$content .='<td valign="top">'.$message->desc.'</td>';
	$content .='<td valign="top" align="right"><a href="'.$message->url.'"><font size="-1">'.$message->longurl.'</font></a></td>';
	$content .='<td valign="top" align="right"><font size="-1">&nbsp;'.$message->created_at.'</font></td>';
	$content .='<td valign="top" align="right"><font size="-1">&nbsp;'.$message->visits.'</font></td>';
	$content .='<td valign="top"><a href="http://twitter.com/home?status=RT%20@'.$id.'%20'.urlencode($message->tweet).'"><font size="-1">RT</font></a></td>';
	$content .='<td valign="top"><a href="http://twitter.com/'.$message->tweetId.'/status/'.$message->tweetId.'"><img src="http://media.thejeshgn.com/img/icons/twitter.png" alt="Open this tweet in Twitter" width="13" border="0" height="16"></a></td>';
	$content .='</tr>';
	$i=$i+1;
}

$content .='</table>';
$content .='</td></tr></table>';
$content .='<br>Inspired by <a href="http://twitter.com/davewiner">@davewiners</a> TopLinks project.&nbsp;&nbsp; <a href="http://thejeshgn.com">Thejesh GN Project</a>';
$content .='<br>Note: Source code will be released once I clean up.';
$content .='</body>';
$content .='</html>';


if($scheduled){	
	$fh = fopen($output_file_name, 'w');
	fwrite($fh, $content);
	fclose($fh);
}else{
	echo $content;
}

?>