/** * Setup TwitterOAuth * * $twitterAuthConf must contain * - consumer_key * - consumer_secret * - oauth_access_token * - oauth_access_token_secret * * @param array $twitterAuthConf * @return $this */ public function setupTwitter(array $twitterAuthConf) { $twitterAuthConf += ['consumer_key' => null, 'consumer_secret' => null, 'oauth_access_token' => null, 'oauth_access_token_secret' => null]; $this->twitter = new TwitterOAuth($twitterAuthConf['consumer_key'], $twitterAuthConf['consumer_secret'], $twitterAuthConf['oauth_access_token'], $twitterAuthConf['oauth_access_token_secret']); $this->twitter->setDecodeJsonAsArray(true); return $this; }
/** * Method sets connection with Twitter API using access token and application credentials. * * @return TwitterOAuth */ private function setConnection() { $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['access_token'], $_SESSION['access_token_secret']); $connection->setDecodeJsonAsArray(true); return $connection; }
public function fetch($limit = FALSE) { // XXX: Store state in database config for now $config = Kohana::$config; $this->_initialize($config); //Check if data provider is available $providers_available = $config->load('features.data-providers'); if (!$providers_available['twitter']) { Kohana::$log->add(Log::WARNING, 'The twitter data source is not currently available. It can be accessed by upgrading to a higher Ushahidi tier.'); return 0; } // check if we have reached our rate limit if (!$this->_can_make_request()) { Kohana::$log->add(Log::WARNING, 'You have reached your rate limit for this window'); return 0; } $options = $this->options(); // Check we have the required config if (!isset($options['consumer_key']) || !isset($options['consumer_secret']) || !isset($options['oauth_access_token']) || !isset($options['oauth_access_token_secret']) || !isset($options['twitter_search_terms'])) { Kohana::$log->add(Log::WARNING, 'Could not fetch messages from twitter, incomplete config'); return 0; } if ($limit === FALSE) { $limit = 50; } $connection = new TwitterOAuth($options['consumer_key'], $options['consumer_secret'], $options['oauth_access_token'], $options['oauth_access_token_secret']); $connection->setDecodeJsonAsArray(true); // Increase curl timeout values $connection->setTimeouts(100, 150); $count = 0; try { $results = $connection->get("search/tweets", ["q" => $this->_construct_get_query($options['twitter_search_terms']), "since_id" => $this->since_id, "count" => $limit, "result_type" => 'recent']); if (empty($results['statuses'])) { return 0; } $statuses = $results['statuses']; // Store the highest id $this->since_id = $statuses[0]['id']; foreach ($statuses as $status) { $id = $status['id']; $user = $status['user']; $screen_name = $user['screen_name']; $text = $status['text']; $additional_data = []; // Skip retweets if (array_key_exists('retweeted_status', $status) && array_key_exists('text', $status['retweeted_status'])) { continue; } if ($status['coordinates'] || $status['place']) { $additional_data['location'] = []; if ($status['coordinates']) { $additional_data['location'][] = $status['coordinates']; } if ($status['place'] && $status['place']['bounding_box']) { // Make a valid linear ring $status['place']['bounding_box']['coordinates'][0][] = $status['place']['bounding_box']['coordinates'][0][0]; // If we don't already have a location if (empty($additional_data['location'])) { // Find center of bounding box $geom = GeoJSON::geomFromText(json_encode($status['place']['bounding_box'])); // Use mysql to run Centroid $result = DB::select([DB::expr('AsText(Centroid(GeomFromText(:poly)))')->param(':poly', $geom->toWKT()), 'center'])->execute(service('kohana.db')); $centerGeom = WKT::geomFromText($result->get('center', 0)); // Save center as location $additional_data['location'][] = $centerGeom->toGeoArray(); } // Add that to location // Also save the original bounding box $additional_data['location'][] = $status['place']['bounding_box']; } } else { if ($status['user'] && $status['user']['location']) { # Search the provided location for matches in twitter's geocoder $results = $connection->get("geo/search", ["query" => $status['user']['location']]); # If there are results, get the centroid of the first one if (!empty($results['result']['places'])) { $geoloc = $results['result']['places'][0]; if ($geoloc['centroid']) { $additional_data['location'][] = array('coordinates' => $geoloc['centroid'], 'type' => 'Point'); } # Add the bounding box too (if available) if ($geoloc['bounding_box']) { $additional_data['location'][] = $geoloc['bounding_box']; } } } } // @todo Check for similar messages in the database before saving $this->receive(Message_Type::TWITTER, $screen_name, $text, $to = NULL, $title = NULL, $id, $additional_data); $count++; } $this->request_count++; //Increment for successful request $this->_update($config); } catch (TwitterOAuthException $toe) { Kohana::$log->add(Log::ERROR, $toe->getMessage()); } catch (Exception $e) { Kohana::$log->add(Log::ERROR, $e->getMessage()); } return $count; }
/** * Fetch Max Posts * * Fetch the maximum number of posts allowed and parse it to get rid of unneeded content. * * @return bool * @throws Exception */ public function getPosts() { $twitter = new TwitterOAuth($this->config->get('twitter', 'consumer'), $this->config->get('twitter', 'secret'), $this->session->getTMP('twitter_access_token')['oauth_token'], $this->session->getTMP('twitter_access_token')['oauth_token_secret']); $twitter->setDecodeJsonAsArray(true); // Need twitter ID of most recent tweet. $beginingID = $twitter->get("statuses/user_timeline", array("count" => 1, 'screen_name' => $this->session->getTMP('twitter_name'), 'include_rts' => true, 'exclude_replies' => false)); // Add one because it will be subtracted later. $beginingID = $beginingID[0]['id'] + 1; // Create array for rendered posts. $posts = array(); // Twitter API max is 3200. $max = 3200; // Starting params. $total = 0; $count = 200; while ($count === 200 && $total < $max) { if (!isset($lastID)) { $lastID = $beginingID; } $postsRaw = $twitter->get("statuses/user_timeline", array("count" => 200, 'max_id' => $lastID - 1, 'screen_name' => $this->session->getTMP('twitter_name'), 'include_rts' => true, 'exclude_replies' => false)); $count = count($postsRaw); $total = $total + $count; $lastID = $postsRaw[$count - 1]['id']; foreach ($postsRaw as $post => $content) { $posts[] = array('url' => 'https://twitter.com/' . $this->session->getTMP('twitter_name') . '/status/' . $content['id'], 'content' => $content['text']); } } unset($postsRaw); $this->session->addPosts($posts); return true; }