Skip to content

Instantly share code, notes, and snippets.

@chonthu
Created August 26, 2011 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chonthu/1173960 to your computer and use it in GitHub Desktop.
Save chonthu/1173960 to your computer and use it in GitHub Desktop.
Backtype
/**
* @name backtype
* @author Nithin Meppurathu
* @author_url http://www.nitmedia.com
* @version 1.1
* @license Do Not distribute under any terms
*
* Backtype library for Codeigniter.
* Class Helps us connect with the backtype api and get back results.
*
* Api URL's
* ---------------------------------
* Homepage : http://www.backtype.com/developers
* Comments Search : http://www.backtype.com/developers/comments-search
* Comments Connect : http://www.backtype.com/developers/connect
* Comments Stats : http://www.backtype.com/developers/comments-connect-stats
* Url Comments : http://www.backtype.com/developers/url-comments
* Post Comments : http://www.backtype.com/developers/page-comments
* Post Stats : http://www.backtype.com/developers/page-comments-stats
* Tweetcount : http://www.backtype.com/developers/tweet-count
*/
class Backtype {
var $key = '2637236273672'; //replaces with your backtype api
// Also xml is a valid format
var $format = 'json';
var $api_url = 'http://api.backtype.com/';
var $website = 'http://developers.nitmedia.com/';
/**
* Callback defined by the backtype api
*
* @string comments_search : searches backtypes comments by specifi page, query, or domain
* @string comments_connect : seraches against different sources native, blog, digg, reddit, yc, friendfeed, twitter
* @string comments_connect_stats : searched for statistics a specific domain
* @mixed url_comments : searches backtypes comments by a specific author
* @string post_comments : searches excerpts of comments published on a particular page
* @string post_stats : searches for statistics for a specific page
* @string tweetcount : searches for twitter hits count on specific page
*/
var $callbacks = array(
'comments_search' => 'comments/search',
'comments_connect' => 'comments/connect',
'comments_connect_stats' => 'comments/connect/stats',
'url_comments' => 'url/{$q}/comments',
'post_comments' => 'post/comments',
'post_stats' => 'post/stats',
'tweetcount' => 'tweetcount'
);
/**
* Constructs the class
* Intiializes code igniter
* Calls a curl helper from our codeigniter instalation
*
*/
function __construct()
{
$this->CI = get_instance();
// loads curl helper php curl wrapper with timeout
$this->CI->load->helper('curl_helper');
}
/**
* Curl Constructor
* Takes a query and builds it with our above settings
*
* @param var callback : the object url
* @param array params : (optional) the paramaters for constructing a query string
* @param string : (optional) the format wither json or xml
* @return curled url
*/
function connect($callback, $params=FALSE, $format=FALSE)
{
$query_string = ($params) ? http_build_query($params).'&' : '';
$format_type = ($format) ? $format : $this->format;
$parsed_url = $this->api_url.$callback.'.'.$format_type.'?'.$query_string.'key='.$this->key;
return curl_connect(array('url'=> $parsed_url, 'timeout'=> 5));
}
/**
* Searches backtypes comments by a specific author
*
* @param string q : the query string
* @return string
*/
function author($q)
{
return $this->connect($this->callbacks['url_comments']);
}
/**
* Searches for twitter hits count on specific page
*
* @param string q : the query string
* @param bool batch : (optional)
* Searches for twitter hits count for muiltiple pages
* on responce provides the specific url of page the tweetcount belongs to
* format, commma deliminated
* @return string
*/
function tweets($q, $batch=FALSE)
{
$params['q'] = $q;
if($batch) $params['mode'] = 'batch';
return $this->connect($this->callbacks['tweetcount'],$params);
}
/**
* Gets comments published on a particular page
*
* @param string url : the query string
* @param bool stats : (optional) get statistics the specific page
* @return string
*/
function page($url, $stats=FALSE)
{
$params['url'] = $url;
if($stats)
return $this->connect($this->callbacks['post_stats'],$params);
else
return $this->connect($this->callbacks['post_comments'],$params);
}
/**
* Global search for all comments
*
* @param string q : the query string or url
* @param array sources : (optional) enables to search by sources, comma deliminated
* --- string sources['sources']: (optional) searches against different sources native, blog, digg, reddit, yc, friendfeed, twitter
* --- int sources['sort'] : (optional) sort sources by type
* @param array date : (optional) does not work with sources | dates format in 'YYYY/MM/DD'
* @return string
*/
function comments($q, $sources=FALSE, $stats=FALSE, $date=FALSE)
{
if($stats)
{
$params['url'] = $q;
return $this->connect($this->callbacks['comments_connect_stats'],$params);
}
else
{
if($sources)
{
$params['url'] = $q;
$params['sources'] = $sources['sources'];
if($sources['sort']) $params['sort'] = $sources['sort'];
return $this->connect($this->callbacks['comments_connect'],$params);
}
else
{
$params['q'] = $q;
if($start) $params['start'] = $start;
if($end) $params['end'] = $end;
return $this->connect($this->callbacks['comments_search'],$params);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment