You are here

function twitter_cron in Twitter 6.2

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

Implementation of hook_cron()

Imports new Twitter statuses for site users, and deletes expired tweets.

File

./twitter.module, line 154

Code

function twitter_cron() {
  if (!variable_get('twitter_import', TRUE)) {
    return;
  }
  module_load_include('inc', 'twitter');

  // Pull up a list of Twitter accounts that are flagged for updating,
  // sorted by how long it's been since we last updated them. This ensures
  // that the most out-of-date accounts get updated first.
  $sql = "SELECT tu.screen_name, tu.password, ta.protected FROM {twitter_user} tu ";
  $sql .= "LEFT JOIN {twitter_account} ta ON tu.screen_name = ta.screen_name ";
  $sql .= "WHERE tu.import = 1 ORDER BY ta.last_refresh ASC";
  $results = db_query_range($sql, 0, 20);
  while ($account = db_fetch_array($results)) {

    // Use the 'cheaper' unauthenticated version if the account isn't protected,
    // or if a password hasn't been specified.
    if (empty($account['protected']) || empty($account['password'])) {
      $statuses = twitter_fetch_timeline($account['screen_name']);
    }
    else {
      $statuses = twitter_fetch_statuses($account['screen_name'], $account['password']);
    }

    // If we got results back, update the account information with the latest
    // follower count, location, user picture, etc. Also, touch the account's
    // last_refreshed timestamp so it won't get thrashed before other, staler
    // accounts.
    if (!empty($statuses)) {
      twitter_cache_account($statuses[0]['account']);
    }
    twitter_touch_account($account['screen_name']);
  }

  // Nuke old statuses.
  if ($age = variable_get('twitter_expire', 0)) {
    db_query('DELETE FROM {twitter} WHERE created_time < %d', time() - $age);
  }
}