Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added searchTweets and getTweetById #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions StormTwitter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,68 @@ function getTweets($screenname = false,$count = 20,$options = false) {

}

function searchTweets($count = 20, $options = false) {

if ($count > 20) $count = 20;
if ($count < 1) $count = 1;

$default_options = array('trim_user'=>true, 'exclude_replies'=>true, 'include_rts'=>false);

if ($options === false || !is_array($options)) {
$options = $default_options;
} else {
$options = array_merge($default_options, $options);
}

$result = $this->checkValidCache("SEARCH",$options);

error_log($result);
if ($result !== false) {
return $this->cropTweets($result,$count);
}
//If we're here, we need to load.
$result = $this->oauthSearchTweets($options);
if (isset($result['errors'])) {
if (is_array($results) && isset($result['errors'][0]) && isset($result['errors'][0]['message'])) {
$last_error = $result['errors'][0]['message'];
} else {
$last_error = $result['errors'];
}
return array('error'=>'Twitter said: '.$last_error);
} else {
return $this->cropTweets($result,$count);
}
}

function getTweetById($id) {

$options = array("id"=>$id);
$result = $this->checkValidCache("ID",$options);

if ($result !== false) {
return $result;
}

//if we're here, we need to load.
$result = $this->oauthGetTweetById($options);

if (is_array($result) && isset($result['errors'])) {
if (is_array($result) && isset($result['errors'][0]) && isset($result['errors'][0]['message'])) {
$last_error = $result['errors'][0]['message'];
} else {
$last_error = $result['errors'];
}
return array('error'=>'Twitter said: '.json_encode($last_error));
} else {
if (is_array($result)) {
return $result;
} else {
$last_error = 'Something went wrong with the twitter request: '.json_encode($result);
return array('error'=>$last_error);
}
}
}

private function cropTweets($result,$count) {
if(!empty($result)){
return array_slice($result, 0, $count);
Expand Down Expand Up @@ -176,8 +238,82 @@ private function oauthGetTweets($screenname,$options) {
$this->st_last_error = $last_error;
}
}
}

private function oauthSearchTweets($options){
$key = $this->defaults['key'];
$secret = $this->defaults['secret'];
$token = $this->defaults['token'];
$token_secret = $this->defaults['token_secret'];
$cachename = "SEARCH-".$this->getOptionsHash($options);
$options = array_merge($options, array('count' => 20));
if (empty($key)) return array('error'=>'Missing Consumer Key - Check Settings');
if (empty($secret)) return array('error'=>'Missing Consumer Secret - Check Settings');
if (empty($token)) return array('error'=>'Missing Access Token - Check Settings');
if (empty($token_secret)) return array('error'=>'Missing Access Token Secret - Check Settings');

$connection = new TwitterOAuth($key, $secret, $token, $token_secret);
$result = $connection->get('search/tweets', $options);

if (is_file($this->getCacheLocation())) {
$cache = json_decode(file_get_contents($this->getCacheLocation()),true);
}

if (!isset($result['errors'])) {
$cache[$cachename]['time'] = time();
$cache[$cachename]['tweets'] = $result;
$file = $this->getCacheLocation();
file_put_contents($file,json_encode($cache));
} else {
if (is_array($results) && isset($result['errors'][0]) && isset($result['errors'][0]['message'])) {
$last_error = '['.date('r').'] Twitter error: '.$result['errors'][0]['message'];
$this->st_last_error = $last_error;
} else {
$last_error = '['.date('r').'] Twitter returned an invalid response. It is probably down.';
$this->st_last_error = $last_error;
}
}

return $result;
}

private function oauthGetTweetById($options) {
$key = $this->defaults['key'];
$secret = $this->defaults['secret'];
$token = $this->defaults['token'];
$token_secret = $this->defaults['token_secret'];

$cachename = "ID-".$this->getOptionsHash($options);

if (empty($key)) return array('error'=>'Missing Consumer Key - Check Settings');
if (empty($secret)) return array('error'=>'Missing Consumer Secret - Check Settings');
if (empty($token)) return array('error'=>'Missing Access Token - Check Settings');
if (empty($token_secret)) return array('error'=>'Missing Access Token Secret - Check Settings');

$connection = new TwitterOAuth($key, $secret, $token, $token_secret);
$result = $connection->get('statuses/show', $options);

if (is_file($this->getCacheLocation())) {
$cache = json_decode(file_get_contents($this->getCacheLocation()),true);
}

if (!isset($result['errors'])) {
$cache[$cachename]['time'] = time();
$cache[$cachename]['tweets'] = $result;
$file = $this->getCacheLocation();
file_put_contents($file,json_encode($cache));
} else {
if (is_array($results) && isset($result['errors'][0]) && isset($result['errors'][0]['message'])) {
$last_error = '['.date('r').'] Twitter error: '.$result['errors'][0]['message'];
$this->st_last_error = $last_error;
} else {
$last_error = '['.date('r').'] Twitter returned an invalid response. It is probably down.';
$this->st_last_error = $last_error;
}
}

return $result;

}

}
27 changes: 21 additions & 6 deletions twitter-feed-for-developers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
require('StormTwitter.class.php');
require('twitter-feed-for-developers-settings.php');

/* implement getTweets */
function getTweets($username = false, $count = 20, $options = false) {

/* setup tweet request */
function requestTweets($method, $args) {
$config['key'] = get_option('tdf_consumer_key');
$config['secret'] = get_option('tdf_consumer_secret');
$config['token'] = get_option('tdf_access_token');
Expand All @@ -26,8 +26,23 @@ function getTweets($username = false, $count = 20, $options = false) {
$config['directory'] = plugin_dir_path(__FILE__);

$obj = new StormTwitter($config);
$res = $obj->getTweets($username, $count, $options);
$res = call_user_func_array(array($obj, $method), $args);

update_option('tdf_last_error',$obj->st_last_error);
return $res;

}
}

/* implement getTweets */
function getTweets($username = false, $count = 20, $options = false) {
return requestTweets('getTweets', array($username, $count, $options));
}

/* implement searchTweets */
function searchTweets($count = 20, $options = false) {
return requestTweets('searchTweets', array($count, $options));
}

/* implement getTweetById */
function getTweetById($id) {
return requestTweets('getTweetById', array($id));
}