You are here

function twitter_puller::get_items in Twitter Pull 7.2

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 twitter_pull.class.inc \twitter_puller::get_items()

File

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

Class

twitter_puller
@file twitter pull class implementation

Code

function get_items() {

  /* First try to use the twitter module to get the tweets
   * At some point, authentication will be required and if you have the
   * twitter module set up properly, it will handle the authenticated session.
   */
  if ($this
    ->twitter_get_items()) {
    return;
  }
  watchdog('Twitter Pull', 'Twitter Pull is using an Unauthenticated request to twitter apis.  Download, enable and configure the twitter module to allow for authenticated requests.');
  $prefix = drupal_substr($this->twitkey, 0, 1);
  $slash = strpos($this->twitkey, '/', 1);
  $num = intval($this->num_items);
  $rts = !empty($this->rts) ? '1' : 'false';
  $exclude_replies = !empty($this->exclude_replies) ? 'true' : 'false';

  // 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=' . $rts . '&count=' . $num . '&exclude_replies=' . $exclude_replies;
  }
  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);
}