View source
<?php
include_once 'tweet_feed.field_info.inc';
define('QUERY_SEARCH', 1);
define('QUERY_TIMELINE', 2);
define('QUERY_LIST', 3);
function tweet_feed_menu() {
$items = array();
$items['admin/config/services/tweet_feed'] = array(
'title' => t('Tweet Feed'),
'description' => t('The settings for the Tweet Feed module.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweet_feed_settings_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/services/tweet_feed/settings'] = array(
'title' => t('Settings'),
'description' => t('The settings for the Tweet Feed module.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweet_feed_settings_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/services/tweet_feed/accounts'] = array(
'title' => t('Twitter API Accounts'),
'description' => t('List of available API accounts used to collect feeds.'),
'page callback' => 'tweet_feed_accounts_table',
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/services/tweet_feed/feeds'] = array(
'title' => t('Twitter Feeds'),
'description' => t('List of configured feeds to aggregate.'),
'page callback' => 'tweet_feed_feeds_table',
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items['admin/config/services/tweet_feed/feeds/run/%tweet_feed_id'] = array(
'title' => t('Import Feed'),
'description' => t('Import tweets from a specific feed.'),
'page callback' => 'tweet_feed_run_import',
'page arguments' => array(
6,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/config/services/tweet_feed/accounts/add'] = array(
'title' => t('Add Account'),
'description' => t('Add a new Twitter API account.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweet_feed_account_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_LOCAL_ACTION,
);
$items['admin/config/services/tweet_feed/accounts/edit/%tweet_feed_id'] = array(
'title' => t('Add Account'),
'description' => t('Add a new Twitter API account.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweet_feed_account_form',
6,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/config/services/tweet_feed/accounts/delete/%tweet_feed_id'] = array(
'title' => t('Add Account'),
'description' => t('Delete Twitter API account.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweet_feed_delete_account_form',
6,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/config/services/tweet_feed/feeds/add'] = array(
'title' => t('Add Feed'),
'description' => t('Add a new feed to the list of aggregated feeds.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweet_feed_feeds_form',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_LOCAL_ACTION,
);
$items['admin/config/services/tweet_feed/feeds/edit/%tweet_feed_id'] = array(
'title' => t('Edit Feed'),
'description' => t('Edit one of the listed feeds.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweet_feed_feeds_form',
6,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/config/services/tweet_feed/feeds/delete/%tweet_feed_id'] = array(
'title' => t('Delete Feed'),
'description' => t('Delete a feed from the list of aggregated feeds.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tweet_feed_delete_feed_form',
6,
),
'access callback' => 'user_access',
'access arguments' => array(
'administer tweet feed settings',
),
'file' => 'tweet_feed_admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function tweet_feed_id_load($value) {
return intval($value) ? $value : FALSE;
}
function tweet_feed_permission() {
return array(
'administer tweet feed settings' => array(
'title' => t('Access Tweet Feed Settings'),
'description' => t('Allow the changing of OAuth tokens and search queries.'),
),
);
}
function tweet_feed_cron() {
$feed = array();
$result = db_select('tweet_feeds', 'f')
->fields('f', array(
'fid',
))
->orderBy('fid', 'ASC')
->execute();
while ($fdata = $result
->fetchObject()) {
$feed[] = $fdata->fid;
}
$last_fid = variable_get('tweet_feed_cron_last_fpos', NULL);
if ($last_fid === NULL) {
$current_fid = 0;
}
else {
$current_fid = $last_fid + 1;
if ($current_fid > count($feed) - 1) {
$current_fid = 0;
}
}
variable_set('tweet_feed_cron_last_fpos', $current_fid);
variable_set('tweet_feed_cron_last_feed', $feed[$current_fid]);
$tweets = tweet_feed_pull_data_from_feed($feed[$current_fid], TRUE);
if (!is_array($tweets) || count($tweets) < 1) {
return FALSE;
}
$queue = drupalQueue::get('tweet_feed_queue');
$queue_size = $queue
->numberOfItems();
if ($queue_size < 1) {
foreach ($tweets as $key => $tweet) {
$update_node_id = 0;
$hash = NULL;
$result = db_select('tweet_hashes', 'h')
->fields('h', array(
'nid',
'tid',
'hash',
))
->condition('h.tid', $tweet->id)
->execute();
if ($result
->rowCount() > 0) {
$tdata = $result
->fetchObject();
$hash = md5(serialize($tweet->text));
if ($hash == $tdata->hash) {
continue;
}
else {
$update_node_id = $tdata->nid;
}
}
$queue
->createItem(array(
'tweet' => $tweet,
'feed' => $feed,
'update_node_id' => $update_node_id,
'hash' => $hash,
));
}
$queue_size = $queue
->numberOfItems();
}
for ($i = 0; $i < $queue_size; $i++) {
$item = $queue
->claimItem($i);
$feed_object = tweet_feed_get_feed_object($item->data['feed'][0]);
tweet_feed_save_tweet($item->data['tweet'], $feed_object, $item->data['update_node_id'], $item->data['hash']);
$queue
->releaseItem($item);
$queue
->deleteItem($item);
}
}
function tweet_feed_process_feed($fid = NULL) {
if ($fid == NULL) {
$result = db_select('tweet_feeds', 'f')
->fields('f', array(
'feed_name',
'fid',
))
->orderBy('feed_name', 'ASC')
->execute();
}
else {
$result = db_select('tweet_feeds', 'f')
->fields('f', array(
'feed_name',
'fid',
))
->condition('f.fid', $fid)
->orderBy('f.feed_name', 'ASC')
->execute();
}
while ($fdata = $result
->fetchObject()) {
tweet_feed_set_message('Processing Feed: ' . $fdata->feed_name, 'ok', $web_interface);
tweet_feed_pull_data_from_feed($fdata->fid);
}
}
function tweet_feed_get_feed_object($fid) {
if (empty($fid)) {
return FALSE;
}
$query = db_select('tweet_feeds', 'f');
$query
->join('tweet_accounts', 'a', 'a.aid = f.aid');
$query
->fields('f', array(
'fid',
'query_type',
'timeline_id',
'search_term',
'list_name',
'pull_count',
'clear_prior',
'new_window',
));
$query
->fields('a', array(
'consumer_key',
'consumer_secret',
'oauth_token',
'oauth_token_secret',
));
$query
->condition('f.fid', $fid);
$result = $query
->execute();
$feed = $result
->fetchObject();
return $feed;
}
function tweet_feed_pull_data_from_feed($fid, $web_interface = FALSE) {
if (empty($fid)) {
return FALSE;
}
$feed = tweet_feed_get_feed_object($fid);
module_load_include('inc', 'tweet_feed', 'inc/twitter-oauth');
if (!empty($feed->clear_prior)) {
tweet_feed_set_message('Clearing Previous Tweets', 'ok', $web_interface);
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'twitter_tweet_feed')
->fieldCondition('field_tweet_feed_id', 'value', $fid, '=')
->execute();
if (isset($result['node'])) {
foreach ($result['node'] as $nid => $node) {
$n = node_load($nid);
node_delete($nid);
tweet_feed_set_message($n->title . ': DELETED', 'ok', $web_interface);
}
}
tweet_feed_set_message('All previous tweets for this feed are deleted.', 'ok', $web_interface);
}
$con = new TwitterOAuth($feed->consumer_key, $feed->consumer_secret, $feed->oauth_token, $feed->oauth_token_secret);
$number_to_get = $feed->pull_count;
$current_count = 0;
$lowest_id = -1;
$tweets = array();
$params = $feed->query_type == QUERY_TIMELINE ? array(
'user_id' => $feed->timeline_id,
'count' => 100,
) : array(
'q' => $feed->search_term,
'count' => 100,
);
while (count($tweets) <= $number_to_get && $lowest_id != 0) {
tweet_feed_set_message('Tweets Imported: ' . count($tweets) . ', Total To Import: ' . $number_to_get, 'ok', $web_interface);
if (!empty($tdata->search_metadata->next_results)) {
$next = substr($tdata->search_metadata->next_results, 1);
$parts = explode('&', $next);
foreach ($parts as $part) {
list($key, $value) = explode('=', $part);
if ($key == 'max_id') {
$lowest_id = $value;
}
$params[$key] = $value;
}
}
$data = new stdClass();
switch ($feed->query_type) {
case QUERY_TIMELINE:
if (count($tweets) < 1) {
tweet_feed_set_message('Retrieving Timeline Status Messages For ID: ' . $feed->timeline_id, 'ok', $web_interface);
}
$tdata = json_decode($con
->oAuthRequest('https://api.twitter.com/1.1/statuses/user_timeline.json', 'GET', $params));
break;
case QUERY_LIST:
if (count($tweets) < 1) {
tweet_feed_set_message('Retrieving List Status Messages For List Name: ' . $feed->list_name, 'ok', $web_interface);
}
$tdata = json_decode($con
->oAuthRequest('https://api.twitter.com/1.1/lists/statuses.json', 'GET', array(
'slug' => $feed->list_name,
'owner_id' => $feed->timeline_id,
'count' => $feed->pull_count,
)));
break;
case QUERY_SEARCH:
default:
if (count($tweets) < 1) {
tweet_feed_set_message('Retrieving Status Messages For Search Term: ' . $feed->search_term, 'ok', $web_interface);
}
$tdata = json_decode($con
->oAuthRequest('https://api.twitter.com/1.1/search/tweets.json', 'GET', array(
'q' => $feed->search_term,
'count' => $feed->pull_count,
)));
break;
}
if (!empty($tdata)) {
tweet_feed_set_message('Processing Tweets', 'ok', $web_interface);
if (!empty($tdata->errors)) {
foreach ($tdata->errors as $error) {
tweet_feed_set_message(t('Tweet Feed Fail: ') . $error->message . ': ' . $error->code, 'error', $web_interface);
$lowest_id = 0;
$tweets = array();
if ($web_interface == TRUE) {
drupal_set_message(t('Tweet Feed Fail: ') . $error->message . ': ' . $error->code, 'error');
}
}
}
else {
if ($feed->query_type == QUERY_TIMELINE || $feed->query_type == QUERY_LIST) {
$end_of_the_line = array_pop($tdata);
array_push($tdata, $end_of_the_line);
$lowest_id = $end_of_the_line->id;
$tweet_data = $tdata;
}
else {
$tweet_data = $tdata->statuses;
}
if (isset($tweet_data['tweets']) && $tweet_data['tweets'] === FALSE) {
break;
}
$returned_tweets = tweet_feed_process_tweets($tweet_data, $feed, $web_interface);
if (count($returned_tweets) == 0) {
$lowest_id = 0;
}
else {
if (count($returned_tweets) <= 100 && count($returned_tweets) > 0) {
$lowest_id = 0;
$tweets = array_merge($tweets, $returned_tweets);
}
else {
$tweets = array_merge($tweets, $returned_tweets);
}
}
}
}
else {
tweet_feed_set_message('No tweets available for this criteria.', 'ok', $web_interface);
break;
}
}
if ($web_interface == TRUE) {
return $tweets;
}
}
function tweet_feed_process_tweets($tweet_data, $feed, $web_interface = FALSE) {
$tweets = array();
$total_hashi = 0;
foreach ($tweet_data as $key => $tweet) {
$update_node_id = 0;
$result = db_select('tweet_hashes', 'h')
->fields('h', array(
'nid',
'tid',
'hash',
))
->condition('h.tid', $tweet->id)
->execute();
if ($result
->rowCount() > 0) {
$tdata = $result
->fetchObject();
$hash = md5(serialize($tweet->text));
if ($hash == $tdata->hash) {
continue;
}
else {
$update_node_id = $tdata->nid;
}
}
if ($web_interface == FALSE) {
tweet_feed_save_tweet($tweet, $feed, $update_node_id, $tdata->hash);
if ($key > 1 && !($key % 20) || $key + 1 == count($tweet_data)) {
tweet_feed_set_message('Loaded ' . $key . ' out of ' . count($tweet_data) . ' (' . number_format($key / count($tweet_data) * 100, 2) . '%)', 'ok', $web_interface);
}
}
$tweets[] = $tweet;
}
return $tweets;
}
function tweet_feed_process_hashtags($hashtag_entities) {
$hashtags = array();
foreach ($hashtag_entities as $entity) {
$vocabulary = taxonomy_vocabulary_machine_name_load('hashtag_terms');
if ($vocabulary == FALSE) {
tweet_feed_set_message('The Hashtag Terms taxonomy vocabulary could not be found. Please uninstall and re-install Tweet Feed', 'fatal', $web_interface);
return FALSE;
}
$result = db_select('taxonomy_term_data', 'td')
->fields('td', array(
'tid',
))
->condition('td.vid', $vocabulary->vid)
->condition('td.name', $entity->text)
->execute();
if ($result
->rowCount() > 0) {
$tid = $result
->fetchField();
}
else {
$term = new stdClass();
$term->vid = $vocabulary->vid;
$term->name = $entity->text;
taxonomy_term_save($term);
$tid = $term->tid;
}
$hashtags[] = $tid;
}
return $hashtags;
}
function tweet_feed_format_output($tweet, $new_window = FALSE) {
$target = $new_window == 1 ? '_blank' : '_self';
$tweet = preg_replace('/(((f|ht){1}tp:\\/\\/)[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]+)/i', '<a target="' . $target . '" href="\\1">\\1</a>', $tweet);
$tweet = preg_replace('/(((f|ht){1}tps:\\/\\/)[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]+)/i', '<a target="' . $target . '" href="\\1">\\1</a>', $tweet);
$tweet = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]+)/i', '\\1<a target="' . $target . '" href="http:\\/\\/\\2">\\2</a>', $tweet);
$tweet = preg_replace('/([_\\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\\.)+[a-z]{2,3})/i', '<a href="mailto:\\1">\\1</a>', $tweet);
$pattern = '/@([A-Za-z0-9_]{1,15})(?![.A-Za-z])/';
$replace = '<a target="' . $target . '" href="http://twitter.com/' . strtolower('\\1') . '">@\\1</a>';
$tweet = preg_replace($pattern, $replace, $tweet);
$tweet = preg_replace('/(^|\\s)#(\\w*+\\w*)/', '\\1<a target="' . $target . '" href="http://twitter.com/search?q=%23\\2">#\\2</a>', $tweet);
return $tweet;
}
function tweet_feed_set_message($message, $type = 'status', $web_interface = FALSE) {
if ($web_interface != FALSE) {
return;
}
global $user;
if (function_exists('drush_print')) {
if ($type != 'error' && $type != 'warning' && $type != 'fatal') {
drush_log($message, 'ok');
}
else {
if ($type == 'fatal') {
drush_set_error($message);
}
else {
drush_log($message, $type);
}
}
}
else {
if ($type != 'drush') {
$type = $type == 'fatal' ? 'error' : $type;
$type = $type == 'ok' ? 'status' : $type;
if ($user->uid == 1) {
drupal_set_message(check_plain($message), $type);
}
}
}
}
function tweet_feed_save_tweet($tweet, $feed, $update_node_id = 0, $hash = NULL, $cron = FALSE) {
$creation_timestamp = strtotime($tweet->created_at);
$tweet_html = tweet_feed_format_output($tweet->text, $feed->new_window);
$hashtags = tweet_feed_process_hashtags($tweet->entities->hashtags);
if ($hashtags === FALSE) {
tweet_feed_bail();
}
$node = new stdClass();
$node->created = $creation_timestamp;
if ($update_node_id > 0) {
$node->nid = $update_node_id;
node_load($node->nid);
}
$tweet_hash = md5(serialize($tweet->text));
if ($tweet_hash == $hash) {
return NULL;
}
$node->type = 'twitter_tweet_feed';
$node->uid = 1;
$node->status = 1;
$node->comment = 0;
$node->promote = 0;
$node->moderate = 0;
$node->sticky = 0;
$node->language = LANGUAGE_NONE;
$title_tweet_text = preg_replace('/[\\x00-\\x08\\x10\\x0B\\x0C\\x0E-\\x19\\x7F]' . '|[\\x00-\\x7F][\\x80-\\xBF]+' . '|([\\xC0\\xC1]|[\\xF0-\\xFF])[\\x80-\\xBF]*' . '|[\\xC2-\\xDF]((?![\\x80-\\xBF])|[\\x80-\\xBF]{2,})' . '|[\\xE0-\\xEF](([\\x80-\\xBF](?![\\x80-\\xBF]))|(?![\\x80-\\xBF]{2})|[\\x80-\\xBF]{3,})/S', '--', $tweet->text);
$title_tweet_text = preg_replace('/\\xE0[\\x80-\\x9F][\\x80-\\xBF]' . '|\\xED[\\xA0-\\xBF][\\x80-\\xBF]/S', '--', $title_tweet_text);
$node->title = substr($tweet->user->screen_name . ': ' . $title_tweet_text, 0, 255);
$node->field_tweet_contents[$node->language][0] = array(
'value' => utf8_encode(htmlspecialchars_decode($tweet_html)),
'format' => 'full_html',
);
$node->field_tweet_feed_id[$node->language][0]['value'] = $feed->fid;
if (!empty($tweet->place->full_name)) {
$node->field_geographic_place[$node->language][0] = array(
'value' => $tweet->place->full_name,
'safe_value' => $tweet->place->full_name,
);
if (!empty($tweet->place->country)) {
$node->field_geographic_place[$node->language][0]['value'] .= ', ' . $tweet->place->country;
$node->field_geographic_place[$node->language][0]['safe_value'] .= ', ' . $tweet->place->country;
}
}
$node->field_tweet_author[$node->language][0] = array(
'value' => $tweet->user->screen_name,
'safe_value' => $tweet->user->screen_name,
);
$node->field_tweet_author_id[$node->language][0] = array(
'value' => $tweet->user->id,
'safe_value' => $tweet->user->id,
);
$node->field_tweet_creation_date[$node->language][0] = array(
'value' => date('Y-m-d H:i:s', strtotime($tweet->created_at)),
'timezone' => 'UTC/GMT',
'timezone_db' => 'UTC/GMT',
'datatype' => 'datetime',
);
$node->field_tweet_id[$node->language][0] = array(
'value' => $tweet->id,
'safe_value' => (int) $tweet->id,
);
$node->field_twitter_favorite_count['unc'][0]['value'] = $tweet->favorite_count;
foreach ($hashtags as $hashtag) {
$node->field_twitter_hashtags[$node->language][] = array(
'target_id' => $hashtag,
);
}
$node->field_twitter_retweet_count[$node->language][0]['value'] = $tweet->retweet_count;
$node->field_tweet_source[$node->language][0] = array(
'value' => $tweet->source,
'safe_value' => strip_tags($tweet->source),
);
$node->field_link_to_tweet[$node->language][0]['value'] = 'https://twitter.com/' . $tweet->user->screen_name . '/status/' . (int) $tweet->id;
if (!empty($tweet->entities->user_mentions) && is_array($tweet->entities->user_mentions)) {
foreach ($tweet->entities->user_mentions as $key => $mention) {
$node->field_tweet_user_mentions[$node->language][$key] = array(
'tweet_feed_mention_name' => $mention->name,
'tweet_feed_mention_screen_name' => $mention->screen_name,
'tweet_feed_mention_id' => $mention->id,
);
}
}
$tweet->user->profile_image_url = str_replace('_normal', '', $tweet->user->profile_image_url);
$file = tweet_feed_process_twitter_image($tweet->user->profile_image_url, 'tweet-feed-profile-image', $tweet->id);
if ($file !== NULL) {
$node->field_profile_image[$node->language][0] = (array) $file;
}
drupal_alter('tweet_feed_tweet_save', $node, $tweet);
node_save($node);
$nid = $node->nid;
db_delete('tweet_hashes')
->condition('nid', $node->nid)
->execute();
$hash_insert = array(
'tid' => $tweet->id,
'nid' => $node->nid,
'hash' => $tweet_hash,
);
drupal_write_record('tweet_hashes', $hash_insert);
unset($node);
if (variable_get('tweet_feed_get_tweeter_profiles', FALSE) == TRUE) {
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'twitter_user_profile')
->fieldCondition('field_twitter_user_id', 'value', $tweet->user->id, '=')
->execute();
if (isset($result['node'])) {
$user_hash = md5(serialize($tweet->user));
$result = db_select('tweet_user_hashes', 'h')
->fields('h', array(
'nid',
'tuid',
'hash',
))
->condition('h.tuid', $tweet->user->id)
->execute();
if ($result
->rowCount() > 0) {
$tdata = $result
->fetchObject();
if ($user_hash == $tdata->hash) {
return;
}
else {
$update_node_id = $tdata->nid;
}
}
}
$node = new stdClass();
if ($update_node_id > 0) {
$node->nid = $update_node_id;
}
$node->type = 'twitter_user_profile';
$node->uid = 1;
$node->created = $creation_timestamp;
$node->status = 1;
$node->comment = 0;
$node->promote = 0;
$node->moderate = 0;
$node->sticky = 0;
$node->language = LANGUAGE_NONE;
$node->field_twitter_user_id[$node->language][0]['value'] = $tweet->user->id_str;
$node->title = $tweet->user->name;
$node->body[$node->language][0]['value'] = $tweet->user->description;
$node->field_twitter_a_screen_name[$node->language][0]['value'] = $tweet->user->screen_name;
$node->field_twitter_location[$node->language][0]['value'] = $tweet->user->location;
$node->field_twitter_a_profile_url[$node->language][0]['value'] = $tweet->user->entities->url->urls[0]->url;
$node->field_twitter_profile_url[$node->language][0]['value'] = $tweet->user->entities->url->urls[0]->display_url;
$node->field_twitter_followers[$node->language][0]['value'] = $tweet->user->followers_count;
$node->field_twitter_following[$node->language][0]['value'] = $tweet->user->friends_count;
$node->field_twitter_favorites_count[$node->language][0]['value'] = $tweet->user->favourites_count;
$node->field_twitter_tweet_count[$node->language][0]['value'] = $tweet->user->statuses_count;
$file = tweet_feed_process_twitter_image($tweet->user->profile_background_image_url, 'tweet-feed-profile-background-image', $tweet->user->id_str);
if ($file !== NULL) {
$node->field_background_image[$node->language][0] = (array) $file;
}
$file = tweet_feed_process_twitter_image($tweet->user->profile_image_url, 'tweet-feed-profile-user-profile-image', $tweet->user->id_str);
if ($file !== NULL) {
$node->field_profile_image[$node->language][0] = (array) $file;
}
$file = tweet_feed_process_twitter_image($tweet->user->profile_banner_url, 'tweet-feed-profile-banner-image', $tweet->user->id_str);
if ($file !== NULL) {
$node->field_banner_image[$node->language][0] = (array) $file;
}
$node->field_background_color[$node->language][0]['value'] = $tweet->user->profile_background_color;
$node->field_profile_text_color[$node->language][0]['value'] = $tweet->user->profile_text_color;
$node->field_link_color[$node->language][0]['value'] = $tweet->user->profile_link_color;
$node->field_sidebar_border_color[$node->language][0]['value'] = $tweet->user->profile_sidebar_border_color;
$node->field_sidebar_fill_color[$node->language][0]['value'] = $tweet->user->profile_sidebar_fill_color;
node_save($node);
db_delete('tweet_user_hashes')
->condition('nid', $node->nid)
->execute();
$hash_insert = array(
'tuid' => $tweet->user->id_str,
'nid' => $node->nid,
'hash' => $user_hash,
);
drupal_write_record('tweet_user_hashes', $hash_insert);
}
}
function tweet_feed_node_delete($node) {
switch ($node->type) {
case 'twitter_tweet_feed':
db_delete('tweet_hashes')
->condition('nid', $node->nid)
->execute();
break;
case 'twitter_user_profile':
db_delete('tweet_user_hashes')
->condition('nid', $node->nid)
->execute();
break;
default:
break;
}
}
function tweet_feed_node_presave($node) {
if ($node->type == 'twitter_tweet_feed') {
$node->changed = $node->created;
}
}
function tweet_feed_process_twitter_image($url, $type, $tid = NULL) {
if (!isset($url)) {
return NULL;
}
if (!file_exists('public://' . $type)) {
drupal_mkdir('public://' . $type);
}
$contents = @file_get_contents($url);
if (empty($contents)) {
return NULL;
}
$file = file_save_data($contents, 'public://' . $type . '/' . md5($url) . '.jpg', FILE_EXISTS_REPLACE);
if ($file === FALSE) {
watchdog('tweet_feed', 'The :type for :tid could not be properly saved.', array(
':type' => $type,
':tid' => $tid,
), WATCHDOG_ERROR, NULL);
return NULL;
}
$file->uid = 1;
$file->status = 1;
file_save($file);
file_usage_add($file, 'tweet_feed', 'file', $file->fid);
return $file;
}
function tweet_feed_bail($admin = FALSE) {
if (function_exists('drush_print')) {
drush_set_error('Exiting.', 'fatal');
}
else {
if (!empty($admin)) {
drupal_goto('admin/config/services/tweet_feed');
}
else {
drupal_goto('<front>');
}
}
}