You are here

function tweet_feed_process_tweets in Tweet Feed 7

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

Process tweet data from what we have retrieved from the Twitter API

Parameters

mixed $tdata: If an object, it is likely errors. Otherwise an array of tweets returned by the api.

Return value

array $tweets The processed tweets

1 call to tweet_feed_process_tweets()
tweet_feed_cron in ./tweet_feed.module
Implements hook_cron().

File

./tweet_feed.module, line 305

Code

function tweet_feed_process_tweets($tdata) {
  $tweets = array();
  if (!empty($tdata)) {
    if (!empty($tdata->errors)) {
      foreach ($tdata->errors as $error) {
        drupal_set_message(t('Tweet Feed Fail: ') . $error->message . ': ' . $error->code, 'error');
        return array();
      }
    }
    else {
      foreach ($tdata as $key => $tweet) {

        // find out if we already have this tweet, if we do, add it to the update pk
        $count = db_query("SELECT COUNT(*) \n                           FROM {tweet_feed} \n                           WHERE tweet_id = :tweet_id", array(
          ':tweet_id' => $tweet->id_str,
        ))
          ->fetchField();
        $exists = $count > 0 ? TRUE : FALSE;
        $creation_timestamp = strtotime($tweet->created_at);
        $lowest_id = $key == 0 ? $tweet->id_str : $lowest_id;
        $lowest_id = $tweet->id_str <= $lowest_id ? $tweet->id_str : $lowest_id;
        $tweet_html = tweet_feed_format_output($tweet->text);
        $hashtags = tweet_feed_get_hashtags($tweet->entities->hashtags);
        if (!empty($tweet->entities->media)) {
          $media_url = $tweet->entities->media[0]->media_url;
          $media_url_https = $tweet->entities->media[0]->media_url_https;
        }
        else {
          $media_url = NULL;
          $media_url_https = NULL;
        }

        // If we are configured to exclude replies, then do that here. We need to increase
        // the size of the tweets array so that we still have a proper count of all
        // tweets processed.
        $exclude_replies = variable_get('tweet_feed_exclude_replies', 0);
        $is_reply = !empty($tweet->in_reply_to_status_id) ? 1 : 0;
        $data = array(
          'tweet' => utf8_encode($tweet_html),
          'created_at' => $creation_timestamp,
          'user_id' => $tweet->user->id_str,
          'profile_image_url' => $tweet->user->profile_image_url,
          'profile_image_https_url' => $tweet->user->profile_image_url_https,
          'screen_name' => $tweet->user->screen_name,
          'name' => $tweet->user->name,
          'hashtags' => $hashtags,
          'tweet_id' => $tweet->id_str,
          'is_reply' => $is_reply,
          'media_url' => $media_url,
          'media_url_https' => $media_url_https,
        );
        if (!empty($exists)) {
          db_update('tweet_feed')
            ->fields($data)
            ->condition('tweet_id', $tweet->id_str)
            ->execute();
        }
        else {
          db_insert('tweet_feed')
            ->fields($data)
            ->execute();
        }
        $tweets[] = $data;
      }
    }
  }
  else {
    return array();
  }
  return $tweets;
}