You are here

function twitter_puller::twitter_get_items in Twitter Pull 6.2

Same name and namespace in other branches
  1. 7.2 twitter_pull.class.inc \twitter_puller::twitter_get_items()

Use the twitter module to get the results.

  • The twitter module will use an authenticated session to get the tweets
  • via the twiter 1.1 API.
1 call to twitter_puller::twitter_get_items()
twitter_puller::get_items in ./twitter_pull.class.inc

File

./twitter_pull.class.inc, line 103
twitter pull class implementation

Class

twitter_puller
@file twitter pull class implementation

Code

function twitter_get_items() {

  // Check for the twitter module.
  if (!module_exists('twitter')) {
    return FALSE;
  }

  // Load the twitter module.
  module_load_include('inc', 'twitter');

  // Get twitkey statistics.
  $prefix = drupal_substr($this->twitkey, 0, 1);
  $slash = strpos($this->twitkey, '/', 1);
  $num = intval($this->num_items);
  $key = drupal_substr($this->twitkey, 1);
  $rts = !empty($this->rts) ? '1' : 'false';
  $exclude_replies = !empty($this->exclude_replies) ? 'true' : 'false';

  // Start building the parameters for the twitter api.
  $params = array(
    'count' => $num,
  );

  // Determin the type of request.
  // Set up the path and params according to the type of request.
  switch ($prefix) {
    case "@":
      if ($slash === FALSE) {

        // Just a normal user timeline.
        $path = "statuses/user_timeline";
        $params['screen_name'] = $key;
        $params['include_rts'] = $rts;
        $params['exclude_replies'] = $exclude_replies;
      }
      else {

        // Since we have at least one slash, we are going to get a list.
        $path = "lists/statuses";
        $params['owner_screen_name'] = drupal_substr($this->twitkey, 1, $slash - 1);
        $params['slug'] = drupal_substr($this->twitkey, $slash + 1);
        $params['include_rts'] = $rts;
        $params['exclude_replies'] = $exclude_replies;
      }
      break;
    case "~":

      // Looking for favorites.
      $path = "favorites/list";
      $params['screen_name'] = $key;
      break;
    default:

      // Default to a not=rmal search.
      $path = "search/tweets";
      $params['count'] = $num > 100 ? 100 : $num;
      $params['q'] = $this->twitkey;
      break;
  }
  $twitter = twitter_connect();
  if (!$twitter) {
    watchdog('Twitter Pull', 'Twitter module is not properly configured and could not be used.');
    return FALSE;
  }

  // Try to load the status via the twitter api (from the twitter module).
  $result = $twitter
    ->call($path, $params, "GET");

  // Create/Empty the tweets array.
  $this->tweets = array();

  // Search results return metadata and the rusults in a sub-array
  // We need to parse the actual result array.
  $result = isset($result['statuses']) ? $result['statuses'] : $result;

  // Proccess the tweets for twitter_pull compatibility.
  $this
    ->parse_items($result);

  // Return if we got any results.
  return count($this->tweets) > 0;
}