You are here

function twitter_pull_retrieve in Twitter Pull 7

Same name and namespace in other branches
  1. 6.2 twitter_pull.module \twitter_pull_retrieve()
  2. 6 twitter_pull.module \twitter_pull_retrieve()
  3. 7.2 twitter_pull.module \twitter_pull_retrieve()

Retrieves tweets by username, hashkey or search term.

Parameters

$twitkey: Twitter key, which can be a username (prepended with @), hashtag (prepended with #), or a search term.

$num_items: Number of tweets to retrieve from Twitter. Can't be more than 200.

1 call to twitter_pull_retrieve()
twitter_pull_render in ./twitter_pull.module
Retrieves appropriate tweets (by username, hashkey or search term) and passes over to the theming function with $themekey key passing tweets array along.

File

./twitter_pull.module, line 139
Twitter Pull module.

Code

function twitter_pull_retrieve($twitkey, $num_items = NULL) {
  global $is_https;

  // If $num_items is not set, use the default value.
  // This value is checked more rigorously in twitter_puller->check_arguments().
  $num_items = intval($num_items) > 0 ? intval($num_items) : twitter_pull_num_items();

  // Cached value is specific to the Twitter key and number of tweets retrieved.
  $cache_key = $twitkey . '::' . $num_items;
  $cache = cache_get($cache_key, TWITTER_PULL_CACHE_TABLE);
  $tweets = array();
  if (!empty($cache) && !empty($cache->data) && time() < $cache->expire) {
    $tweets = $cache->data;
  }
  else {
    try {
      $puller = new twitter_puller($twitkey, $num_items);
      $puller
        ->get_items();
      $tweets = $puller->tweets;
    } catch (Exception $e) {
      watchdog('Twitter Pull', $e
        ->getMessage(), array(), WATCHDOG_WARNING);
      if (!empty($cache) && !empty($cache->data)) {
        return $cache->data;
      }
      else {
        return twitter_pull_empty_message();
      }
    }
    if (!empty($tweets) && is_array($tweets)) {
      $cache_length = twitter_pull_cache_length() * 60;

      //-- in the settings we indicate length in minutes, here we need seconds.
      cache_set($cache_key, $tweets, TWITTER_PULL_CACHE_TABLE, REQUEST_TIME + $cache_length);
    }
  }

  // If we have tweets and are viewing a secure site, we want to set the url
  // to the userphoto to use the secure image to avoid insecure errors.
  if (!empty($tweets) && is_array($tweets) && $is_https) {
    foreach ($tweets as $i => $tweet) {
      $tweets[$i]->userphoto = $tweet->userphoto_https;
    }
  }
  return $tweets;
}