You are here

function tweet_feed_cron in Tweet Feed 7

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

Implements hook_cron().

File

./tweet_feed.module, line 163

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();

  // Initialize our parameters
  $query_type = variable_get('tweet_feed_query_type', 'search');
  switch ($query_type) {
    case 'timeline':
      $params = array(
        'user_id' => variable_get('tweet_feed_user_id', NULL),
        'count' => $number_to_get,
      );
      break;
    case 'list':
      $list_name = variable_get('tweet_feed_timeline_list', NULL);
      $params = array(
        'owner_id' => variable_get('tweet_feed_user_id', NULL),
        'count' => 100,
        'slug' => $list_name,
      );
      break;
    default:
      $params = array(
        'q' => variable_get('tweet_feed_search_query', NULL),
        'count' => 100,
      );
      break;
  }
  $lowest_id = -1;
  while (count($tweets) < $number_to_get && $lowest_id != 0) {
    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') {
          if (PHP_INT_MAX > 2147483647) {
            $value = $lowest_id - 1;
          }
          else {

            // We have to do it this way because PHP on Windows can't handle 64 bit integers.
            $orig = $lowest_id;
            $last = abs(substr($orig, -1) - 1);

            // Not perfect :/
            $value = substr($orig, 0, -1) . $last;
          }
        }
        $params[$key] = $value;
      }
    }
    switch ($query_type) {
      case 'timeline':
        $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
        $tdata = new stdClass();
        $tdata->statuses = json_decode($con
          ->oAuthRequest($url, 'GET', $params));
        break;
      case 'search':
        $url = 'https://api.twitter.com/1.1/search/tweets.json';
        $tdata = json_decode($con
          ->oAuthRequest($url, 'GET', $params));
        break;
      case 'list':
        $list_name = variable_get('tweet_feed_timeline_list', NULL);
        $params = array(
          'owner_id' => variable_get('tweet_feed_user_id', NULL),
          'count' => 100,
          'slug' => $list_name,
        );
        if ($lowest_id > 0) {
          $params['max_id'] = $lowest_id - 1;
        }
        $tdata = new stdClass();
        $tdata->statuses = json_decode($con
          ->oAuthRequest('https://api.twitter.com/1.1/lists/statuses.json', 'GET', $params));
      default:
        break;
    }
    if (!empty($tdata)) {

      // Get the lowest ID from the last element in the timeline
      $end_of_the_line = array_pop($tdata->statuses);
      array_push($tdata->statuses, $end_of_the_line);
      $lowest_id = $end_of_the_line->id_str;
      $tweet_data = tweet_feed_process_tweets($tdata->statuses);
      if (count($tweet_data) == 0) {
        $lowest_id = 0;
      }
      else {
        if (count($tweet_data) < 100 && count($tweet_data) > 0) {
          $lowest_id = 0;
          $tweets = array_merge($tweets, $tweet_data);
        }
        else {
          $tweets = array_merge($tweets, $tweet_data);
        }
      }
    }
    else {
      break;
    }
  }
}