You are here

function tweet_feed_pull_data_from_feed in Tweet Feed 7.3

Same name and namespace in other branches
  1. 7.2 tweet_feed.module \tweet_feed_pull_data_from_feed()

Get Twitter Data

Pull data from the feed given our internal feed id. Our feed object also contains the information about the account associated with this feed (reference) so we have everything we need to connect via the Twitter API and retrieve the data.

Parameters

int fid: The feed is of the feed with which we wish to procure the data

bool web_interface: Are we pulling the data from the web interface?

Return value

array tweets An array of all the tweets for this feed. FALSE if there are problems.

3 calls to tweet_feed_pull_data_from_feed()
tweet_feed_cron in ./tweet_feed.module
Implements hook_cron().
tweet_feed_process_feed in ./tweet_feed.module
Iterate through the feeds and import
tweet_feed_run_import in ./tweet_feed_admin.inc
Run the Import
2 string references to 'tweet_feed_pull_data_from_feed'
tweet_feed_cron in ./tweet_feed.module
Implements hook_cron().
tweet_feed_uninstall in ./tweet_feed.install
Implements hook_uninstall().

File

./tweet_feed.module, line 370

Code

function tweet_feed_pull_data_from_feed($fid, $web_interface = FALSE) {

  // If the fid is empty then we do not have enough information by which to pull data.
  // When this is the case, we need to bail.
  if (empty($fid)) {
    return FALSE;
  }

  // Get our feed object that contains everything we want to know about this feed.
  // If there is an invalid feed, then return false
  $feed = tweet_feed_get_feed_object($fid);
  if (empty($feed)) {
    return FALSE;
  }

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

  // If we have selected to clear our prior tweets for this particular feed, then we need
  // to do that here.
  if (!empty($feed->clear_prior)) {

    // All tweets are nodes, so we do an entity query to get the node id's for the tweets
    // belonging to this feed and delete them. It's conceivable that this could take some
    // time.
    tweet_feed_set_message('Clearing Previous Tweets', 'ok', $web_interface);
    $query = new EntityFieldQuery();
    $result = $query
      ->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'twitter_tweet_feed')
      ->fieldCondition('field_tweet_feed_id', 'value', $fid, '=')
      ->execute();
    if (isset($result['node'])) {
      foreach ($result['node'] as $nid => $node) {

        // Removing the message that tells us the node is deleted. Loading the node just to
        // get the title is like using a nuclear weapon to light your cigarette. Will work on
        // a better way in a future version.
        // $n = node_load($nid);
        node_delete($nid);

        // tweet_feed_set_message($n->title . ': DELETED', 'ok', $web_interface);
      }
    }
    tweet_feed_set_message('All previous tweets for this feed are deleted.', 'ok', $web_interface);
  }

  // Build TwitterOAuth object with client credentials
  $con = new TwitterOAuth($feed->consumer_key, $feed->consumer_secret, $feed->oauth_token, $feed->oauth_token_secret);

  // Get the number of tweets to pull from our list
  $number_to_get = $feed->pull_count * 100;
  $run_count = 0;
  $lowest_id = -1;
  $tweets = array();
  $params = $feed->query_type == QUERY_TIMELINE ? array(
    'user_id' => $feed->timeline_id,
    'count' => 100,
    'tweet_mode' => 'extended',
  ) : array(
    'q' => $feed->search_term,
    'count' => 100,
    'tweet_mode' => 'extended',
  );
  while (count($tweets) < $number_to_get && $lowest_id != 0) {
    tweet_feed_set_message('Tweets Imported: ' . count($tweets) . ', Total To Import: ' . $number_to_get, 'ok', $web_interface);
    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') {
          $lowest_id = $value;
        }
        $params[$key] = $value;
      }
    }
    $data = new stdClass();
    switch ($feed->query_type) {
      case QUERY_TIMELINE:

        // Only display this the first time. No need for each trip down the line.
        if (count($tweets) < 1) {
          tweet_feed_set_message('Retrieving Timeline Status Messages For ID: ' . $feed->timeline_id, 'ok', $web_interface);
        }

        // Set the max id to one lower than the lowest tweet we've imported so we do not
        // get duplicates
        if ($lowest_id > 0) {
          $params['max_id'] = $lowest_id;
        }
        $tdata = json_decode($con
          ->oAuthRequest('https://api.twitter.com/1.1/statuses/user_timeline.json', 'GET', $params));
        break;
      case QUERY_LIST:

        // Only display this the first time. No need for each trip down the line.
        if (count($tweets) < 1) {
          tweet_feed_set_message('Retrieving List Status Messages For List Name: ' . $feed->list_name, 'ok', $web_interface);
        }
        $params = array(
          'slug' => $feed->list_name,
          'owner_id' => $feed->timeline_id,
          'count' => 100,
        );
        if ($lowest_id > 0) {
          $params['max_id'] = $lowest_id;
        }
        $tdata = json_decode($con
          ->oAuthRequest('https://api.twitter.com/1.1/lists/statuses.json', 'GET', $params));
        break;
      case QUERY_SEARCH:
      default:

        // Only display this the first time. No need for each trip down the line.
        if (count($tweets) < 1) {
          tweet_feed_set_message('Retrieving Status Messages For Search Term: ' . $feed->search_term, 'ok', $web_interface);
        }
        $tdata = json_decode($con
          ->oAuthRequest('https://api.twitter.com/1.1/search/tweets.json', 'GET', $params));
        if (empty($tdata->search_metadata->next_results)) {
          $lowest_id = 0;
        }
        break;
    }
    if (!empty($tdata)) {
      tweet_feed_set_message('Processing Tweets', 'ok', $web_interface);

      // If we have errors, then we need to handle them accordingly
      if (!empty($tdata->errors)) {
        foreach ($tdata->errors as $error) {
          tweet_feed_set_message(t('Tweet Feed Fail: ') . $error->message . ': ' . $error->code, 'error', $web_interface);
          $lowest_id = 0;
          $tweets = array();

          // If we're handling through the web interface and we're having an issue, then
          // we need to spit the error to the screen
          if ($web_interface == TRUE) {
            drupal_set_message(t('Tweet Feed Fail: ') . $error->message . ': ' . $error->code, 'error');
          }
        }
      }
      else {
        if ($feed->query_type == QUERY_TIMELINE || $feed->query_type == QUERY_LIST) {

          // Get the lowest ID from the last element in the timeline
          $end_of_the_line = array_pop($tdata);
          array_push($tdata, $end_of_the_line);
          $lowest_id = $end_of_the_line->id_str;

          // Proceed with our processing
          $tweet_data = $tdata;
        }
        else {
          $tweet_data = $tdata->statuses;
        }

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

        // merge the total tweets so we know how many we have. If we have none, trigger
        // the script to proceed on to the next feed.
        $tweet_data = tweet_feed_process_tweets($tweet_data, $feed, $number_to_get, $run_count, $web_interface);
        if (count($tweet_data) == 0) {
          $lowest_id = 0;
        }
        else {
          $tweets = array_merge($tweets, $tweet_data);
        }
      }
      $run_count++;
    }
    else {
      tweet_feed_set_message('No tweets available for this criteria.', 'ok', $web_interface);
      break;
    }
  }

  // If we are processing through cron or the web interface, then hand back all of our
  // tweets so that they may be processed.
  if ($web_interface == TRUE) {
    return $tweets;
  }
}