You are here

social_content_twitter.module in Social Content 7

Same filename and directory in other branches
  1. 7.2 modules/twitter/social_content_twitter.module

Social Content: Twitter module.

File

modules/twitter/social_content_twitter.module
View source
<?php

/**
 * @file
 * Social Content: Twitter module.
 */
define('SOCIAL_CONTENT_TWITTER_HOST', 'https://twitter.com');
module_load_include('php', 'oauth_common', 'lib/OAuth');
module_load_include('inc', 'social_content_twitter', 'social_content_twitter.filters');
function social_content_twitter_social_content_info() {
  $info = array();
  $shared = array(
    'content_type' => 'tweet',
    'settings_form' => 'social_content_tweet_settings_form',
    'external_id_field_mapping' => array(
      'id_str' => 'field_tweet_external_id',
    ),
    'post_callback' => 'social_content_twitter_post_callback',
  );
  $additional_settings_shared = array(
    'api_url' => 'https://api.twitter.com/1.1',
    'oauth_consumer_key' => '',
    'oauth_consumer_secret' => '',
    'oauth_token' => '',
    'oauth_secret' => '',
    'import_image' => 0,
  );
  $info['twitter_account'] = $shared + array(
    'title' => t('Twitter (account)'),
    'data_callback' => 'social_content_twitter_account_data_callback',
    'additional_settings' => $additional_settings_shared + array(
      'account' => '',
    ),
  );
  $info['twitter_hashtag'] = $shared + array(
    'title' => t('Twitter (hashtag)'),
    'data_callback' => 'social_content_twitter_hashtag_data_callback',
    'additional_settings' => $additional_settings_shared + array(
      'hashtags' => '',
    ),
  );
  return $info;
}
function social_content_tweet_settings_form(&$form, $social_content_type, $settings) {
  if ($social_content_type['name'] == 'twitter_account') {
    $form['account'] = array(
      '#type' => 'textfield',
      '#title' => t('Twitter account'),
      '#description' => t('The twitter account to pull the statuses from'),
      '#default_value' => $settings['account'],
    );
  }
  elseif ($social_content_type['name'] == 'twitter_hashtag') {
    $form['hashtags'] = array(
      '#type' => 'textfield',
      '#title' => t('Hashtags'),
      '#description' => t('Without the leading #. You can define multiple hashtags separated by space.'),
      '#default_value' => $settings['hashtags'],
    );
  }
  $form['api_url'] = array(
    '#type' => 'textfield',
    '#title' => t('API URL'),
    '#description' => t('Twitter api url do not include trailing /.'),
    '#default_value' => $settings['api_url'],
  );
  $form['oauth_consumer_key'] = array(
    '#type' => 'textfield',
    '#title' => t('Consumer key'),
    '#default_value' => $settings['oauth_consumer_key'],
  );
  $form['oauth_consumer_secret'] = array(
    '#type' => 'textfield',
    '#title' => t('Consumer secret'),
    '#default_value' => $settings['oauth_consumer_secret'],
  );
  $form['oauth_token'] = array(
    '#type' => 'textfield',
    '#title' => t('Oauth token'),
    '#default_value' => $settings['oauth_token'],
  );
  $form['oauth_secret'] = array(
    '#type' => 'textfield',
    '#title' => t('Oauth secret'),
    '#default_value' => $settings['oauth_secret'],
  );
  $form['import_image'] = array(
    '#type' => 'checkbox',
    '#title' => t('Import user profile images'),
    '#description' => t('The user profile image of each tweet will be saved to the node.'),
    '#default_value' => $settings['import_image'],
  );
}
function social_content_twitter_account_data_callback($settings, $last_id = NULL) {
  $params = array(
    'screen_name' => $settings['account'],
    'exclude_replies' => TRUE,
    'count' => $settings['limit'],
  );
  if ($last_id) {
    $params['since_id'] = $last_id;
  }
  $data = social_content_twitter_request('statuses/user_timeline.json', $params, $settings);
  if ($data) {
    return $data;
  }
  return FALSE;
}
function social_content_twitter_hashtag_data_callback($settings, $last_id = NULL) {
  if (!empty($settings['hashtags'])) {
    $posts = array();
    $hashtags = explode(' ', $settings['hashtags']);
    foreach ($hashtags as $hashtag) {
      $params = array(
        'q' => $hashtag,
        'count' => $settings['limit'],
      );
      if ($last_id) {
        $params['since_id'] = $last_id;
      }
      $data = social_content_twitter_request('search/tweets.json', $params, $settings);
      if ($data && isset($data->statuses) && is_array($data->statuses)) {
        $posts += $data->statuses;
      }
      else {
        return FALSE;
      }
    }
    return $posts;
  }
  return FALSE;
}
function social_content_twitter_request($endpoint, $params, $settings) {
  $endpoint = $settings['api_url'] . '/' . $endpoint;
  $oauth = array(
    'consumerKey' => $settings['oauth_consumer_key'],
    'consumerSecret' => $settings['oauth_consumer_secret'],
    'oauthToken' => $settings['oauth_token'],
    'oauthSecret' => $settings['oauth_secret'],
  );
  $consumer = new OAuthConsumer($oauth['consumerKey'], $oauth['consumerSecret'], NULL);
  $token = new OAuthConsumer($oauth['oauthToken'], $oauth['oauthSecret']);
  $oauth_request = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $endpoint, $params);
  $oauth_request
    ->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, $token);
  $result = drupal_http_request($oauth_request
    ->to_url());
  if ($result->code == 200) {
    return json_decode($result->data);
  }
  else {
    watchdog('social_content_twitter', 'Error fetching feed, data: !data', array(
      '!data' => $result->data,
    ), WATCHDOG_WARNING);
    return FALSE;
  }
}
function social_content_twitter_post_callback(&$wrapper, $post, $external_id, $settings) {

  // No retweets.
  // TODO: Support retweets via setting options.
  if (isset($post->retweeted_status)) {
    return FALSE;
  }
  $wrapper->created
    ->set(strtotime($post->created_at));
  $body_text = social_content_validate_text($post->text);
  $filter = filter_format_exists('tweet') ? 'tweet' : 'filtered_html';
  $wrapper->body
    ->set(array(
    'value' => $body_text,
    'format' => $filter,
  ));
  $tweet_url = 'http://twitter.com/' . $post->user->screen_name . '/status/' . $post->id_str;
  $wrapper->field_tweet_link
    ->set(array(
    'url' => $tweet_url,
  ));
  $wrapper->field_tweet_user
    ->set($post->user->screen_name);
  if ($settings['import_image']) {
    $image_field_name = 'field_tweet_user_picture';
    $image_file = social_content_get_image_file($post->user->profile_image_url, $image_field_name);
    $wrapper->{$image_field_name}
      ->set($image_file);
  }
  return TRUE;
}