You are here

function tweet_feed_cron in Tweet Feed 6

Same name and namespace in other branches
  1. 8.3 tweet_feed.module \tweet_feed_cron()
  2. 7.3 tweet_feed.module \tweet_feed_cron()
  3. 7 tweet_feed.module \tweet_feed_cron()
  4. 7.2 tweet_feed.module \tweet_feed_cron()

implementation of hook_cron()

File

./tweet_feed.module, line 135

Code

function tweet_feed_cron() {
  $tweet_feed_consumer_key = variable_get('tweet_feed_consumer_key', NULL);
  $tweet_feed_consumer_secret = variable_get('tweet_feed_consumer_secret', NULL);
  $tweet_feed_oauth_token = variable_get('tweet_feed_oauth_token', NULL);
  $tweet_feed_oauth_token_secret = variable_get('tweet_feed_oauth_token_secret', NULL);

  // check to see if our api tokens are set up, if not then skip cron
  if (empty($tweet_feed_consumer_key) || empty($tweet_feed_consumer_secret) || empty($tweet_feed_oauth_token) || empty($tweet_feed_oauth_token_secret)) {
    return FALSE;
  }

  // Load in our twitter oauth class
  module_load_include('inc', 'tweet_feed', 'inc/twitter-oauth');

  // If we have selected to truncate our tweet table, then do so here
  $truncate = variable_get('tweet_feed_truncate', 0);
  if ($truncate == 1) {
    db_query('TRUNCATE TABLE {tweet_feed}');
  }

  // Build TwitterOAuth object with client credentials
  $con = new TwitterOAuth($tweet_feed_consumer_key, $tweet_feed_consumer_secret, $tweet_feed_oauth_token, $tweet_feed_oauth_token_secret);

  // get the number of tweets to pull from our list
  $number_to_get = variable_get('tweet_feed_pull_count', 100);
  $current_count = 0;
  $tweets = array();
  $params = variable_get('tweet_feed_query_type', 'search') == 'timeline' ? array(
    'user_id' => variable_get('tweet_feed_user_id', NULL),
    'count' => 100,
  ) : array(
    'q' => variable_get('tweet_feed_search_query', NULL),
    'count' => 100,
  );
  while (count($tweets) <= $number_to_get) {
    if (!empty($tdata->search_metadata->next_results)) {
      $next = substr($tdata->search_metadata->next_results, 1);
      $parts = explode('&', $next);
      foreach ($parts as $part) {
        list($key, $value) = explode('=', $part);
        if ($key == 'max_id') {
          $value = $lowest_id;
        }
        $params[$key] = $value;
      }
    }
    if (variable_get('tweet_feed_query_type', 'search') == 'timeline') {
      $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';

      /* get the tweets */
      $tdata = new stdClass();
      $tdata->statuses = json_decode($con
        ->oAuthRequest($url, 'GET', $params));
    }
    else {
      $url = 'https://api.twitter.com/1.1/search/tweets.json';

      /* get the tweets */
      $tdata = json_decode($con
        ->oAuthRequest($url, 'GET', $params));
    }
    $data = tweet_feed_process_tweets($tdata->statuses);

    // If this is FALSE, then we have hit an error and need to stop processing
    if ($data['tweets'] === FALSE) {
      break;
    }

    // merge the total tweets so we know how many we have
    $tweets = array_merge($data['tweets'], $tweets);
    $lowest_id = $data['lowest_id'];
  }
}