You are here

function twitter_puller::get_items in Twitter Pull 7

Same name and namespace in other branches
  1. 6.2 twitter_pull.class.inc \twitter_puller::get_items()
  2. 6 twitter_pull.class.inc \twitter_puller::get_items()
  3. 7.2 twitter_pull.class.inc \twitter_puller::get_items()

File

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

Class

twitter_puller
@file twitter pull class implementation

Code

function get_items() {
  $prefix = drupal_substr($this->twitkey, 0, 1);
  $slash = strpos($this->twitkey, '/', 1);
  $num = intval($this->num_items);

  // lists have the format @username/listname
  if ($prefix == '@' && $slash !== FALSE) {
    $username = drupal_substr($this->twitkey, 1, $slash - 1);
    $listname = drupal_substr($this->twitkey, $slash + 1);
    $url = 'http://api.twitter.com/1/' . urlencode($username) . '/lists/' . urlencode($listname) . '/statuses.json?per_page=' . $num;
  }
  elseif ($prefix == "@") {
    $key = drupal_substr($this->twitkey, 1);
    $url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . urlencode($key) . '&include_rts=true&count=' . $num;
  }
  elseif ($prefix == "~") {
    $key = drupal_substr($this->twitkey, 1);
    $url = 'http://api.twitter.com/1/favorites/' . urlencode($key) . '.json?count=' . $num;
  }
  else {
    if ($num > 100) {
      $num = 100;
    }
    $url = 'http://search.twitter.com/search.json?q=' . urlencode($this->twitkey) . '&rpp=' . $num;
  }
  $ret = drupal_http_request($url, array(
    'timeout' => 2,
  ));
  if ($ret->code < 200 || $ret->code > 399) {
    $errmsg = t('An unknown error occurred.');
    if (isset($ret->error) && !empty($ret->error)) {
      $errmsg = check_plain($ret->error);
    }
    elseif (isset($ret->data) && !empty($ret->data)) {
      $errdata = json_decode($ret->data);
      if (isset($errdata->error) && !empty($errdata->error)) {
        $errmsg = check_plain($errmsg->error);
      }
    }
    if ($ret->code == 400) {
      $errmsg .= ' ' . t('This site may be subject to rate limiting. For more information, see: http://apiwiki.twitter.com/Rate-limiting');
    }
    throw new Exception(t('Could not retrieve data from Twitter.') . ' ' . $errmsg);
  }
  $items = json_decode($ret->data);
  $this
    ->parse_items($items);
}