From 47906fc63ff603663838db35921d9556a4a5262f Mon Sep 17 00:00:00 2001 From: Joel Rainwater Date: Fri, 19 Feb 2016 17:34:46 -0500 Subject: [PATCH 1/2] added search methods. Optimized method calls --- StormTwitter.class.php | 72 +++++++++++++++++++++++++++++++-- twitter-feed-for-developers.php | 22 +++++++--- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/StormTwitter.class.php b/StormTwitter.class.php index 3504c9e..baed9cc 100644 --- a/StormTwitter.class.php +++ b/StormTwitter.class.php @@ -81,6 +81,39 @@ 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); + } + } + private function cropTweets($result,$count) { if(!empty($result)){ return array_slice($result, 0, $count); @@ -176,8 +209,41 @@ 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; - - } -} + }} \ No newline at end of file diff --git a/twitter-feed-for-developers.php b/twitter-feed-for-developers.php index a1a76a1..bd157c0 100644 --- a/twitter-feed-for-developers.php +++ b/twitter-feed-for-developers.php @@ -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'); @@ -26,8 +26,18 @@ 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; - -} \ No newline at end of file +} + +/* 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)); +} From ef439346bb37c8ee15dd26f08dfd8c57327e0803 Mon Sep 17 00:00:00 2001 From: Joel Rainwater Date: Fri, 19 Feb 2016 17:41:51 -0500 Subject: [PATCH 2/2] added getTweetById methods --- StormTwitter.class.php | 72 ++++++++++++++++++++++++++++++++- twitter-feed-for-developers.php | 5 +++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/StormTwitter.class.php b/StormTwitter.class.php index baed9cc..c860b47 100644 --- a/StormTwitter.class.php +++ b/StormTwitter.class.php @@ -114,6 +114,35 @@ function searchTweets($count = 20, $options = false) { } } + 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); @@ -246,4 +275,45 @@ private function oauthSearchTweets($options){ } return $result; - }} \ No newline at end of file + } + + 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; + + } + +} diff --git a/twitter-feed-for-developers.php b/twitter-feed-for-developers.php index bd157c0..c0a2347 100644 --- a/twitter-feed-for-developers.php +++ b/twitter-feed-for-developers.php @@ -41,3 +41,8 @@ function getTweets($username = false, $count = 20, $options = false) { function searchTweets($count = 20, $options = false) { return requestTweets('searchTweets', array($count, $options)); } + +/* implement getTweetById */ +function getTweetById($id) { + return requestTweets('getTweetById', array($id)); +}