You are here

function drush_twitter_search in Twitter 7.5

Same name and namespace in other branches
  1. 6.5 twitter.drush.inc \drush_twitter_search()
  2. 7.6 twitter.drush.inc \drush_twitter_search()
  3. 7.3 twitter.drush.inc \drush_twitter_search()
  4. 7.4 twitter.drush.inc \drush_twitter_search()

Implements drush_COMMANDFILE_COMMANDNAME()

Searches for a keyword at Twitter and return the results.

File

./twitter.drush.inc, line 57
Drush commands for the Twitter module.

Code

function drush_twitter_search($keyword) {

  // Set up a Twitter object to query with.
  module_load_include('inc', 'twitter');
  $twitter = twitter_connect(NULL, TRUE, TRUE);
  if (empty($twitter)) {
    drush_set_error(dt('At least one Twitter account must be authenticated at admin/config/services/twitter in order to perform requests to the Twitter API.'));
    return FALSE;
  }

  // Build the query and get the response.
  $url = variable_get('twitter_api', TWITTER_API) . '/1.1/search/tweets.json';
  $data = $twitter
    ->auth_request($url, array(
    'q' => $keyword,
  ));
  $data = json_decode($data);
  if (!count($data->statuses)) {
    drush_set_error(dt('No tweets found for this keyword.'));
  }
  else {
    drush_print(dt('There are !total tweets containing \'@keyword\'.', array(
      '!total' => $data->search_metadata->count,
      '@keyword' => $keyword,
    )));
    $tweets = $data->statuses;

    // Should we randomize?
    if (drush_get_option('randomize')) {
      $results = shuffle($tweets);
    }

    // Should we limit the list of results?
    if (drush_get_option('limit')) {
      $tweets = array_slice($tweets, 0, drush_get_option('limit'));
    }

    // Print results
    foreach ($tweets as $tweet) {
      drush_print('');
      drush_print(dt('User "@!user", tweeted "!tweet".', array(
        '!user' => $tweet->user->screen_name,
        '!tweet' => $tweet->text,
      )));
      drush_print('');
    }
  }
}