You are here

function twitter_fetch_user_timeline in Twitter 6.5

Same name and namespace in other branches
  1. 6.3 twitter.inc \twitter_fetch_user_timeline()
  2. 6.4 twitter.inc \twitter_fetch_user_timeline()
  3. 7.6 twitter.inc \twitter_fetch_user_timeline()
  4. 7.3 twitter.inc \twitter_fetch_user_timeline()
  5. 7.4 twitter.inc \twitter_fetch_user_timeline()
  6. 7.5 twitter.inc \twitter_fetch_user_timeline()

Fetches a user's timeline.

1 call to twitter_fetch_user_timeline()
twitter_cron in ./twitter.module
Implements hook_cron().

File

./twitter.inc, line 264

Code

function twitter_fetch_user_timeline($id) {
  $twitter_account = twitter_account_load($id);
  $since = db_result(db_query("SELECT MAX(twitter_id) FROM {twitter} WHERE screen_name = '%s'", $twitter_account->screen_name));

  // Connect to the Twitter's API. If the account is authenticated, use that
  // account, otherwise grab the first authenticated account.
  if ($twitter_account
    ->is_auth()) {
    $twitter = twitter_connect($twitter_account);
  }
  else {
    $twitter = twitter_connect(NULL, TRUE, TRUE);
  }

  // That should have worked, but there might have been a problem.
  if (empty($twitter)) {
    watchdog('twitter', 'Unable to authenticate to download tweets for the %name account.', array(
      '%name' => $twitter_account->screen_name,
    ));
    return FALSE;
  }

  // Fetch tweets.
  $params = array();
  if (!empty($since)) {
    $params['since_id'] = $since;
  }

  // Trigger hook_twitter_prefetch_timeline().
  // Doing it this way so that the hook may adjust the parameters.
  $hook = 'twitter_prefetch_timeline';
  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;
    if (function_exists($function)) {
      $function($twitter_account, $params);
    }
  }

  // Load the timeline.
  $statuses = $twitter
    ->user_timeline($id, $params);

  // Trigger hook_twitter_statuses_alter().
  drupal_alter('twitter_statuses', $statuses, $twitter_account);

  // Save each tweet.
  if (count($statuses) > 0) {
    watchdog('twitter', 'Downloaded %count tweets for the %name account.', array(
      '%count' => count($statuses),
      '%name' => $twitter_account->screen_name,
    ));
    foreach ($statuses as $status) {
      twitter_status_save($status);
    }
  }

  // Trigger hook_twitter_insert_statuses().
  module_invoke_all('twitter_insert_statuses', $statuses, $twitter_account);

  // Update account details.
  if (count($statuses) > 0) {
    twitter_account_save($statuses[0]->user);
  }
}