| 1 |
<?php |
<?php |
| 2 |
// $Id: comms.module,v 1.1.4.19 2009/09/20 00:07:22 evoltech Exp $ |
// $Id: comms.module,v 1.1.4.20 2009/09/22 01:04:54 evoltech Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 751 |
//comms_admin form. Drupal checks all input, but it is a good idea to |
//comms_admin form. Drupal checks all input, but it is a good idea to |
| 752 |
//provide additional checks, cause, ya know hackers and stuff |
//provide additional checks, cause, ya know hackers and stuff |
| 753 |
function comms_admin_validate($node, &$form) { |
function comms_admin_validate($node, &$form) { |
| 754 |
$dispatchlevel = $form['comms_dispatchlevel']; |
$dispatchlevel = $form['comms_dispatchlevel']; |
| 755 |
|
/* |
| 756 |
if (!is_numeric($dispatchlevel)) { |
if (!is_numeric($dispatchlevel)) { |
| 757 |
form_set_error('comms_dispatchlevel', t('You must select a number for the dispatch level.')); |
form_set_error('comms_dispatchlevel', t('You must select a number for the dispatch level.')); |
| 758 |
} |
} |
| 761 |
} |
} |
| 762 |
else if ($dispatchlevel > 10) { |
else if ($dispatchlevel > 10) { |
| 763 |
form_set_error('comms_dispatchlevel', t('Maximum dispatch level must be less then or equal to 10.')); |
form_set_error('comms_dispatchlevel', t('Maximum dispatch level must be less then or equal to 10.')); |
| 764 |
} |
} |
| 765 |
|
*/ |
| 766 |
|
|
| 767 |
$debuglevel = $form['comms_debuglevel']; |
$debuglevel = $form['comms_debuglevel']; |
| 768 |
if (!is_numeric($debuglevel)) { |
if (!is_numeric($debuglevel)) { |
| 1157 |
$_SESSION['comms']['timeframe'] != '') ? |
$_SESSION['comms']['timeframe'] != '') ? |
| 1158 |
$_SESSION['comms']['timeframe'] : 'All'; |
$_SESSION['comms']['timeframe'] : 'All'; |
| 1159 |
|
|
| 1160 |
$rating = (isset($_SESSION['comms']['rating']) && |
$rating = 'All'; |
|
$_SESSION['comms']['rating'] != '') ? |
|
|
$_SESSION['comms']['rating'] : 'All'; |
|
| 1161 |
|
|
| 1162 |
$dispatched = 'not'; |
$dispatched = 'not'; |
| 1163 |
//we check to see if either they are both set or if they are not set, I know its funky |
//we check to see if either they are both set or if they are not set, I know its funky |
| 1194 |
|
|
| 1195 |
$args = array($timeframe, $rating, $dispatched, $categorized); |
$args = array($timeframe, $rating, $dispatched, $categorized); |
| 1196 |
|
|
|
if ($_SESSION['comms']['urgency']['name']) { |
|
|
$args[] = $_SESSION['comms']['urgency']['name']; |
|
|
} |
|
|
|
|
| 1197 |
if ($_SESSION['comms']['display']) { |
if ($_SESSION['comms']['display']) { |
| 1198 |
drupal_add_js(drupal_get_path('module', 'comms') .'/js/comms.slim.js'); |
drupal_add_js(drupal_get_path('module', 'comms') .'/js/comms.slim.js'); |
| 1199 |
$view = views_get_view('comms_slim'); |
$view = views_get_view('comms_slim'); |
| 1232 |
$_SESSION['comms']['dispatched'] : array('dispatched', 'notdispatched'), |
$_SESSION['comms']['dispatched'] : array('dispatched', 'notdispatched'), |
| 1233 |
); |
); |
| 1234 |
|
|
|
//rating - should be 1 - max |
|
|
$form['rating'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('Current vote value'), |
|
|
'#default_value' => isset($_SESSION['comms']['rating']) ? $_SESSION['comms']['rating'] : '', |
|
|
'#size' => 3, |
|
|
'#maxlength' => 3, |
|
|
'#description' => t('Filter the intel by its vote value'), |
|
|
); |
|
|
|
|
|
// |
|
| 1235 |
$form['categorized'] = array( |
$form['categorized'] = array( |
| 1236 |
'#type' => 'checkboxes', |
'#type' => 'checkboxes', |
| 1237 |
'#title' => t('Categorized'), |
'#title' => t('Categorized'), |
| 1243 |
'#description' => t('Show only content that has a been categorized. Note that checking "Not categorized" is an easy way to see all new nodes'), |
'#description' => t('Show only content that has a been categorized. Note that checking "Not categorized" is an easy way to see all new nodes'), |
| 1244 |
); |
); |
| 1245 |
|
|
|
//urgency |
|
|
//TODO: this needs to be a look up by the vocabulary name |
|
|
$form['urgency'] = taxonomy_form(1); |
|
|
$form['urgency']['#weight'] = 1; |
|
|
$form['urgency']['#default_value'] = $_SESSION['comms']['urgency']['tid'] ? |
|
|
$_SESSION['comms']['urgency']['tid'] : ''; |
|
|
|
|
| 1246 |
$form['display'] = array( |
$form['display'] = array( |
| 1247 |
'#type' => 'radios', |
'#type' => 'radios', |
| 1248 |
'#title' => t('Display Option'), |
'#title' => t('Display Option'), |
| 2221 |
return 1; |
return 1; |
| 2222 |
} |
} |
| 2223 |
|
|
| 2224 |
//this fucntion will turn 1 if the node can be dispatched |
// This function will turn 1 if the node can be dispatched |
| 2225 |
//and a string containing a error message if not |
// and a string containing a error message if not |
| 2226 |
function comms_group_dispatch_validate($node) { |
function comms_group_dispatch_validate($node) { |
| 2227 |
//first we verify that this is a comms node |
//first we verify that this is a comms node |
| 2228 |
if ($node->type != 'comms') { |
if ($node->type != 'comms') { |
| 2297 |
//messages sent by each follower (creating drupal accounts as needed) |
//messages sent by each follower (creating drupal accounts as needed) |
| 2298 |
//TODO: find out if we are going to get timeouts on this function |
//TODO: find out if we are going to get timeouts on this function |
| 2299 |
function comms_cron() { |
function comms_cron() { |
| 2300 |
|
// Pull in the cron functions. |
| 2301 |
|
// @todo This should be a configureable setting once we get the daemon up. |
| 2302 |
|
require_once(drupal_get_path('module', 'comms') .'/comms_cron.inc'); |
| 2303 |
|
|
| 2304 |
//this function is never called in the normal drupal context so this |
//this function is never called in the normal drupal context so this |
| 2305 |
//code section is only useful when using the developer module |
//code section is only useful when using the developer module |
| 2306 |
if (variable_get('comms_debuglevel', 0) > 0) { |
if (variable_get('comms_debuglevel', 0) > 0) { |
| 2324 |
foreach ($groups as $gid => $group) { |
foreach ($groups as $gid => $group) { |
| 2325 |
|
|
| 2326 |
//TODO: calling of this needs to be controlled |
//TODO: calling of this needs to be controlled |
| 2327 |
$followers = comms_get_twitter_followers($gid); |
//$followers = comms_get_twitter_followers($gid); |
| 2328 |
|
$followers = array(); |
| 2329 |
if (is_array($followers)) { |
if (is_array($followers)) { |
| 2330 |
foreach ($followers as $follower) { |
foreach ($followers as $follower) { |
| 2331 |
comms_add_user_by_name_if_not_exist($follower); |
comms_add_user_by_name_if_not_exist($follower); |
| 2341 |
//TODO: this should be split out into two parts, each of which can be |
//TODO: this should be split out into two parts, each of which can be |
| 2342 |
//turned on/off. They are adding messages sent to our groups, and |
//turned on/off. They are adding messages sent to our groups, and |
| 2343 |
//adding messages sent by our groups followers |
//adding messages sent by our groups followers |
| 2344 |
comms_add_twitter_tweets(); |
//comms_add_twitter_tweets(); |
| 2345 |
}//if |
}//if |
| 2346 |
else if (variable_get('comms_addfromgetfollowers', 0)) { |
else if (variable_get('comms_addfromgetfollowers', 0)) { |
| 2347 |
comms_add_from_get_followers($all_followers); |
comms_add_from_get_followers($all_followers); |
| 2779 |
return $twitter; |
return $twitter; |
| 2780 |
} |
} |
| 2781 |
|
|
|
//Helper function for processing and adding status messages returned |
|
|
//from comms_get_twitter_followers() |
|
|
//@todo this needs to have the time added to it. |
|
|
function comms_add_from_get_followers($followers) { |
|
|
if (variable_get('comms_debuglevel', 0) > 0) { |
|
|
drupal_set_message('comms_add_from_get_followers()'); |
|
|
} |
|
|
|
|
|
//we don't want to pull in data older then what we already have. With the |
|
|
//exception that if we have not pulled anything in with this method before |
|
|
//message id's from followers are not ordered |
|
|
$max_msg_id = variable_get('comms_twitterfollowermsgmaxid', 0); |
|
|
$new_max_msg_id = 0; |
|
|
if (is_array($followers)) { |
|
|
foreach ($followers as $name => $follower) { |
|
|
|
|
|
//some of the followers will not have a status associated with them |
|
|
//we will just skip them |
|
|
if (!isset($follower['message']) || |
|
|
!isset($follower['message_id']) || |
|
|
!isset($follower['name'])) { |
|
|
continue; |
|
|
}//if |
|
|
|
|
|
if ($follower['message_id'] > $max_msg_id) { |
|
|
$errors = comms_add_comms_from_twitter($follower['message'], |
|
|
$follower['name']); |
|
|
|
|
|
if (count($errors)) { |
|
|
watchdog('comms_group', |
|
|
t('comms_cron() did not succeed: %errors', |
|
|
array('%errors' => print_r(check_plain($errors), 1)))); |
|
|
} |
|
|
else if ($follower['message_id'] > $new_max_msg_id) { |
|
|
$new_max_msg_id = $follower['message_id']; |
|
|
} |
|
|
} |
|
|
}//foreach |
|
|
|
|
|
if ($new_max_msg_id > $max_msg_id) { |
|
|
variable_set('comms_twitterfollowermsgmaxid', $new_max_msg_id); |
|
|
}//if |
|
|
}//if |
|
|
} |
|
|
|
|
|
function comms_add_comms_from_twitter($message, $user, $time = NULL, $reset = NULL) { |
|
|
if (!isset($message) || !isset($user)) { |
|
|
return; |
|
|
} |
|
|
|
|
|
static $tusers; |
|
|
if (!isset($tusers) || $reset) { |
|
|
//get the list of our groups |
|
|
$tusers = array(); |
|
|
$groups = module_invoke('og', 'all_groups_options'); |
|
|
while ($name = current($groups)) { |
|
|
$account = comms_get_group_twitter_account(key($groups)); |
|
|
$tusers = array_merge($tusers, array($account['username'] => key($groups))); |
|
|
next($groups); |
|
|
} |
|
|
} |
|
|
|
|
|
$values = array(); |
|
|
// @todo use the drupal time function for this. |
|
|
$values['body'] = t('message (picked up at %date) recieved from twitter: ', array('%date' => date('Y-m-d h:i:s a'))); |
|
|
|
|
|
//there can be encoded html characters in the message, we handle |
|
|
//them here. |
|
|
$message = htmlspecialchars_decode($message); |
|
|
|
|
|
//see simplefeed_item.module for an example of this |
|
|
//we want to make sure here that this is nt a tweet meant for a |
|
|
//group that we are not trancking |
|
|
//then if this is a message directed at another user that is not |
|
|
//one of the groups we care about we ignore it |
|
|
//http://help.twitter.com/index.php?pg=kb.page&id=75 |
|
|
if (preg_match('/^@(\w+)\s(.*)$/', $message, $matches)) { |
|
|
if (array_key_exists($matches[1], $tusers)) { |
|
|
$values['sms_message'] = $matches[2]; |
|
|
$values['body'] .= $matches[2]; |
|
|
$values['to'] = $matches[1]; |
|
|
} |
|
|
else { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
}//if |
|
|
else { |
|
|
$values['body'] .= $message; |
|
|
$values['sms_message'] = $message; |
|
|
} |
|
|
|
|
|
$node->type = 'comms'; |
|
|
|
|
|
//for the title we want the first 8 words |
|
|
$values['title'] = split(' ', $values['sms_message'], 8); |
|
|
if (count($values['title']) == 8) { |
|
|
array_pop($values['title']); |
|
|
} |
|
|
$values['title'] = implode(' ', $values['title']); |
|
|
|
|
|
//we want to add the group membership if there is one |
|
|
if (!empty($values['to'])) { |
|
|
$values['og_groups'] = array($tusers[$values['to']] => $tusers[$values['to']]); |
|
|
} |
|
|
|
|
|
$values['name'] = $user; |
|
|
$time = $time ? $time : time(); |
|
|
$values['date'] = date('Y-m-d H:i:s', $time); |
|
|
|
|
|
|
|
|
drupal_execute('comms_node_form', $values, $node); |
|
|
$errors = form_get_errors(); |
|
|
|
|
|
return $errors; |
|
|
} |
|
|
|
|
| 2782 |
function comms_has_been_dispatched($nid) { |
function comms_has_been_dispatched($nid) { |
| 2783 |
if (variable_get('comms_debuglevel', 0) > 0) { |
if (variable_get('comms_debuglevel', 0) > 0) { |
| 2784 |
drupal_set_message('comms_has_been_dispatched()'); |
drupal_set_message('comms_has_been_dispatched()'); |
| 2793 |
return (db_num_rows($result) == 0) ? 0 : $result; |
return (db_num_rows($result) == 0) ? 0 : $result; |
| 2794 |
} |
} |
| 2795 |
|
|
|
function comms_add_user_by_name_if_not_exist($follower) { |
|
|
if (variable_get('comms_debuglevel', 0) > 0) { |
|
|
drupal_set_message('comms_add_user_if_not_exist()'); |
|
|
} |
|
|
|
|
|
if (user_load(array('name' => $follower['name']))) { |
|
|
return; |
|
|
} |
|
|
|
|
|
//add the user if it does not exist |
|
|
$account = (object) array(); |
|
|
$options['name'] = $follower['name']; |
|
|
$options['mail'] = $follower['name'] .'@twitter.com'; |
|
|
|
|
|
if ($follower['twitterUID']) { |
|
|
$options['twitterUID'] = $follower['twitterUID']; |
|
|
} |
|
|
|
|
|
$result = user_save($account, $options); |
|
|
if ($result) { |
|
|
watchdog('comms', t('Adding new twitter follower, @name.', array('@name' => $result->name))); |
|
|
} |
|
|
else { |
|
|
watchdog('comms', t('Failed adding the twitter follower, @name.', array('@name' => $follower['name']))); |
|
|
} |
|
|
return $result; |
|
|
} |
|
|
|
|
|
//will try to get all new from messages (random tweets) and add them to the |
|
|
//system |
|
|
function comms_add_twitter_tweets() { |
|
|
if (variable_get('comms_debuglevel', 0) > 0) { |
|
|
drupal_set_message('comms_add_twitter_tweets()'); |
|
|
} |
|
|
|
|
|
//for each twitter user |
|
|
//get all messages sent through the search api |
|
|
//note we should not add the message id here. If we do we need to rethink |
|
|
//the logic above |
|
|
|
|
|
//get the list of our groups |
|
|
$tusers = array(); |
|
|
$groups = module_invoke('og', 'all_groups_options'); |
|
|
while ($name = current($groups)) { |
|
|
$account = comms_get_group_twitter_account(key($groups)); |
|
|
$tusers = array_merge($tusers, array($account['username'] => key($groups))); |
|
|
next($groups); |
|
|
} |
|
|
|
|
|
$messages = comms_get_twitter_tweets(); |
|
|
|
|
|
if (is_array($messages)) { |
|
|
foreach ($messages as $message) { |
|
|
//$errors = comms_add_from_twitter($message['message'], |
|
|
$errors = comms_add_comms_from_twitter($message['message'], |
|
|
$message['user'], $message['time']); |
|
|
|
|
|
if (count($errors)) { |
|
|
watchdog('comms_group', 'comms_cron() did not succeed: '. |
|
|
$errors); |
|
|
} |
|
|
else { |
|
|
$tuser_object = user_load(array('name' => $message['user'])); |
|
|
if ($tuser_object->comms_twitterfrommsgmaxid < $message['id']) { |
|
|
user_save($tuser_object,array('comms_twitterfrommsgmaxid' => $message['id'])); |
|
|
} |
|
|
} |
|
|
}//foreach |
|
|
}//if |
|
|
}//comms_add_twitter_tweets |
|
|
|
|
|
//gets all users that were added from twitter. Returns an array of twitter |
|
|
//user names. |
|
|
function comms_get_twitter_users() { |
|
|
if (variable_get('comms_debuglevel', 0) > 0) { |
|
|
drupal_set_message('comms_get_twitter_users()'); |
|
|
} |
|
|
|
|
|
$return = NULL; |
|
|
|
|
|
if (! ($result = db_query('SELECT uid from {users}'))) { |
|
|
watchdog('comms', |
|
|
t('Could not select the twitter users from the database.')); |
|
|
return $return; |
|
|
} |
|
|
|
|
|
while ($dbuser = db_fetch_array($result)) { |
|
|
$user = user_load(array('uid' => $dbuser['uid'])); |
|
|
if ($user === FALSE) { |
|
|
continue; |
|
|
} |
|
|
|
|
|
if ($user->twitterUID) { |
|
|
// This is where we can filter out pulling tweets for users below a |
|
|
// certain point value. |
|
|
$point_threshold = variable_get('comms_disablepostbelowpoint', 0); |
|
|
if ($point_threshold != 0) { |
|
|
if (userpoints_get_current_points($user->uid) > $point_threshold) { |
|
|
$return[] = $user->name; |
|
|
} |
|
|
} |
|
|
else { |
|
|
$return[] = $user->name; |
|
|
} |
|
|
} |
|
|
}//while |
|
|
return $return; |
|
|
} |
|
|
|
|
|
//gets all the followers for a a group that has a twitter account associated |
|
|
//with it. returns an an array of associative arrays in the form: |
|
|
/* |
|
|
Array |
|
|
( |
|
|
Array |
|
|
( |
|
|
[name] => <twitter username>, |
|
|
[twitterUID] => <twitter user uid> |
|
|
)[,...] |
|
|
) |
|
|
|
|
|
*/ |
|
|
function comms_get_twitter_followers($gid) { |
|
|
if (variable_get('comms_debuglevel', 0) > 0) { |
|
|
drupal_set_message('comms_get_twitter_followers()'); |
|
|
} |
|
|
|
|
|
$return = NULL; |
|
|
|
|
|
//if there is no twitter account associated borf! |
|
|
$ta = comms_get_group_twitter_account($gid); |
|
|
if (is_null($ta)) { |
|
|
return $ta; |
|
|
} |
|
|
|
|
|
//there is a situation here were we will only get 100 users from a request in order |
|
|
//that they created their accounts. If we have more then 100 users we would like to |
|
|
//get all of them |
|
|
$twitter = _comms_twitterAPI_load($ta['username'], $ta['password']); |
|
|
$page = 1; |
|
|
while (1) { |
|
|
try { |
|
|
$messages = $twitter->getFollowers('json', $page); |
|
|
}catch (Exception $e) { |
|
|
watchdog('comms_group', t('unable to get followers for gid:@gid, %mess', |
|
|
array('@gid' => $gid, '%mess' => $e->getMessage()))); |
|
|
|
|
|
comms_add_twitter_api_call(-1, $gid, 'getFollowers', 1, 0, |
|
|
$e->getMessage()); |
|
|
|
|
|
return $return; |
|
|
} |
|
|
|
|
|
if ($messages->isError()) { |
|
|
$error_message_web = scraping_twitter_error_page($messages->getData()); |
|
|
$error_message_json = json_decode($messages->getData(), 1); |
|
|
|
|
|
$message = t("There was a problem trying to get the followers associated with gid:@gid. Error: @errorweb. Error: @errorjson", array( |
|
|
'@gid' => $gid, |
|
|
'@errorweb' => $error_message_web, |
|
|
'@errorjson' => $error_message_json['error'])); |
|
|
|
|
|
watchdog("comms_group", $message); |
|
|
|
|
|
comms_add_twitter_api_call(-1, $gid, 'getFollowers', 1, 0, $message); |
|
|
|
|
|
return $return; |
|
|
} |
|
|
else { |
|
|
$userct = 0; |
|
|
foreach (json_decode($messages->getData(), 1) as $message) { |
|
|
$userct++; |
|
|
$name = $message['screen_name']; |
|
|
$uid = $message['id']; |
|
|
$msg = isset($message['status']['text']) ? $message['status']['text'] : NULL; |
|
|
$msgid = isset($message['status']['id']) ? $message['status']['id'] : NULL; |
|
|
$return[] = array( |
|
|
'name' => $name, |
|
|
'twitterUID' => $uid, |
|
|
'message' => $msg, |
|
|
'message_id' => $msgid |
|
|
); |
|
|
}//foreach |
|
|
|
|
|
if ($userct == 100) { |
|
|
$page++; |
|
|
} |
|
|
else { |
|
|
$message = t('Got %count followers for gid: @gid', |
|
|
array('%count' => count($return), '@gid' => $gid)); |
|
|
comms_add_twitter_api_call(-1, $gid, 'getFollowers', 1, 1, $message); |
|
|
return $return ? $return : array(); |
|
|
}//else |
|
|
}//else |
|
|
}//while |
|
|
}//comms_get_twitter_followers |
|
|
|
|
|
//TODO: determine what the limit is on the number of from: searches we can do |
|
|
//at one time and break the search up into multiple requests |
|
|
//TODO: maybe we should limit this to a specific time as well ... for example |
|
|
//maybe we only search for tweets newer then today |
|
|
|
|
|
//gets all tweets for all twitter users that have been added to the |
|
|
//installation. returns an array of associative arrays in the form: |
|
|
/* |
|
|
Array |
|
|
( |
|
|
Array |
|
|
( |
|
|
[user] => <twitter username>, |
|
|
[message] => <tweet>, |
|
|
[id] => <twitter message id>, |
|
|
[twitterUID] => <twitter sender uid> |
|
|
)[, ...] |
|
|
) |
|
|
*/ |
|
|
function comms_get_twitter_tweets() { |
|
|
if (variable_get('comms_debuglevel', 0) > 0) { |
|
|
drupal_set_message('comms_get_twitter_tweets()'); |
|
|
} |
|
|
|
|
|
$return = NULL; |
|
|
|
|
|
$tusers = comms_get_twitter_users(); |
|
|
|
|
|
if (count($tusers) <= 0) { |
|
|
return $return; |
|
|
} |
|
|
|
|
|
$twitter = _comms_twitterSearchAPI_load(); |
|
|
|
|
|
// Run a separate twitter search for each user. |
|
|
foreach($tusers as $tuser) { |
|
|
$tuser_object = user_load(array('name' => $tuser)); |
|
|
$maxmsgid = $tuser_object->comms_twitterfrommsgmaxid; |
|
|
if(!$maxmsgid) { |
|
|
$maxmsgid = 0; |
|
|
} |
|
|
$page=1; |
|
|
$break=0; |
|
|
//There is a situation where we may have more then 100 tweets for this user |
|
|
//since the last check |
|
|
while (!$break && $page<5) { |
|
|
try { |
|
|
$messages = $twitter->getTweetsForUsers(array($tuser), 'json', $maxmsgid, $page); |
|
|
}catch (Exception $e) { |
|
|
$message = t("There was a problem trying to get the tweets associated with user: @user. Error Message: @mess", array( |
|
|
'@user' => $tuser, '@mess' => $e->getMessage())); |
|
|
|
|
|
watchdog('comms_group', $message); |
|
|
|
|
|
comms_add_twitter_api_call(-1, -1, 'twitterSearchAPI::from()', 0, 0, |
|
|
$message); |
|
|
|
|
|
$break = 1; |
|
|
continue; |
|
|
} |
|
|
|
|
|
if ($messages->isError()) { |
|
|
|
|
|
//needed to get the error message when twitter can't even give a proper error |
|
|
$error_message_web = scraping_twitter_error_page($messages->getData()); |
|
|
$error_message_json = json_decode($messages->getData(), 1); |
|
|
|
|
|
$message = t("There was a problem trying to get the tweets associated with user: @user.Error Message: @errorweb. Error Message: @errorjson.", array( |
|
|
'@user' => $tuser, |
|
|
'@errorweb' => $error_message_web, |
|
|
'@errorjson' => $error_message_json['error'])); |
|
|
|
|
|
watchdog("comms_group", $message); |
|
|
|
|
|
comms_add_twitter_api_call(-1, -1, 'twitterSearchAPI::from()', 0, 0, |
|
|
$message); |
|
|
|
|
|
$break = 1; |
|
|
continue; |
|
|
} |
|
|
else { |
|
|
$message_list = json_decode($messages->getData(), 1); |
|
|
$message_list = $message_list['results']; |
|
|
|
|
|
if (is_array($message_list)) { |
|
|
$msgct = 0; |
|
|
foreach ($message_list as $message) { |
|
|
$msgct++; |
|
|
$text = $message['text']; |
|
|
$sender = $message['from_user']; |
|
|
$uid = $message['from_user_id']; |
|
|
$id = $message['id']; |
|
|
$return[] = array( |
|
|
'user' => $sender, |
|
|
'message' => $text, |
|
|
'id' => $id, |
|
|
'time' => strtotime($message['created_at']), |
|
|
'created_at' => $message['created_at'], |
|
|
'twitterUID' => $id); |
|
|
}//foreach |
|
|
|
|
|
($msgct >= 100) ? $page++ : $break=1; |
|
|
|
|
|
} |
|
|
else { |
|
|
$break=1; |
|
|
} |
|
|
|
|
|
$message = t('Got %count tweets for user: @user', array( |
|
|
'%count' => count($return), '@user' => $tuser)); |
|
|
comms_add_twitter_api_call(-1, -1, 'twitterSearchAPI::from()', 0, 1, |
|
|
$message); |
|
|
}//else |
|
|
}//while |
|
|
}//foreach |
|
|
|
|
|
$message = t('Got %count tweets for users: @user', array( |
|
|
'%count' => count($return), '@user' => implode(', ', $tusers))); |
|
|
watchdog("comms_group", $message); |
|
|
|
|
|
return $return; |
|
|
} |
|
|
|
|
|
//gets all direct messages for a group that has a twitter account associated |
|
|
//with it. returns an array of associative arrays in the form: |
|
|
/* |
|
|
Array |
|
|
( |
|
|
Array |
|
|
( |
|
|
[user] => <twitter username>, |
|
|
[message] => <twitter direct message>, |
|
|
[id] => <twitter message id>, |
|
|
[twitterUID] => <twitter sender uid> |
|
|
)[, ...] |
|
|
) |
|
|
*/ |
|
|
function comms_get_twitter_group_direct_messages($gid) { |
|
|
if (variable_get('comms_debuglevel', 0) > 0) { |
|
|
drupal_set_message('comms_get_twitter_group_direct_messages()'); |
|
|
} |
|
|
|
|
|
$return = NULL; |
|
|
|
|
|
//if there is no twitter account associated borf! |
|
|
$ta = comms_get_group_twitter_account($gid); |
|
|
if (is_null($ta)) { |
|
|
return $ta; |
|
|
} |
|
|
|
|
|
$twitter = _comms_twitterAPI_load($ta['username'], $ta['password']); |
|
|
|
|
|
//get the most recently recorded twitter direct message id |
|
|
$msgid = variable_get('comms_twitterdirectmsgmaxid', 0); |
|
|
|
|
|
//this function call returns that last 20 direct messages that were recieved |
|
|
//there is a possibility that we have recieved more then 20 direct messages |
|
|
//since the last time that we checked, so we should continue to pull messages |
|
|
//until we recieve one that is < comms_twitterdirectmsgmaxid |
|
|
$page = 1; |
|
|
while (1) { |
|
|
try { |
|
|
$messages = $twitter->getMessages('json', '', $msgid, $page); |
|
|
}catch (Exception $e) { |
|
|
watchdog('comms_group', t('unable to get direct messages for gid: @gid, message: @mess', array( |
|
|
array('@gid' => $gid, '@mess' => $e->getMessage())))); |
|
|
|
|
|
comms_add_twitter_api_call(-1, $gid, 'getMessages', 1, 0, |
|
|
$e->getMessage()); |
|
|
|
|
|
return $return; |
|
|
} |
|
|
|
|
|
if ($messages->isError()) { |
|
|
$error_message_web = scraping_twitter_error_page($messages->getData()); |
|
|
$error_message_json = json_decode($messages->getData(), 1); |
|
|
|
|
|
$message = t("There was a problem trying to get the followers associated with gid:@gid. Error: @errorweb. Error: @errorjson", array( |
|
|
'@gid' => $gid, |
|
|
'@errorweb' => $error_message_web, |
|
|
'@errorjson' => $error_message_json['error'])); |
|
|
|
|
|
watchdog("comms_group", $message); |
|
|
|
|
|
comms_add_twitter_api_call(-1, $gid, 'getMessages', 1, 0, $message); |
|
|
|
|
|
return $return; |
|
|
} |
|
|
else { |
|
|
$messagect = 0; |
|
|
foreach (json_decode($messages->getData(), 1) as $message) { |
|
|
$messagect++; |
|
|
$text = $message['text']; |
|
|
$sender = $message['sender']['screen_name']; |
|
|
$uid = $message['sender']['id']; |
|
|
$id = $message['id']; |
|
|
$return[] = array( |
|
|
'user' => $sender, |
|
|
'message' => $text, |
|
|
'id' => $id, |
|
|
'twitterUID' => $id); |
|
|
}//foreach |
|
|
|
|
|
if ($messagect == 20) { |
|
|
$page++; |
|
|
} |
|
|
else { |
|
|
$message = t('Got %count tweets for users: @users', array( |
|
|
'%count' => count($return), '@users' => $tuser)); |
|
|
watchdog("comms_group", $message); |
|
|
comms_add_twitter_api_call(-1, $gid, 'getMessages', 1, 1, $message); |
|
|
return $return; |
|
|
} |
|
|
}//else |
|
|
}//while |
|
|
|
|
|
} |
|
| 2796 |
|
|
| 2797 |
function comms_get_sms_message($nid = NULL) { |
function comms_get_sms_message($nid = NULL) { |
| 2798 |
if (variable_get('comms_debuglevel', 0) > 0) { |
if (variable_get('comms_debuglevel', 0) > 0) { |