/[drupal]/contributions/modules/tapatio/comms_cron.inc
ViewVC logotype

Diff of /contributions/modules/tapatio/comms_cron.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1 by evoltech, Tue Sep 22 22:26:47 2009 UTC revision 1.1.2.1 by evoltech, Tue Sep 22 22:26:47 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id:$
3    
4    function comms_add_user_by_name_if_not_exist($follower) {
5      if (variable_get('comms_debuglevel', 0) > 0) {
6        drupal_set_message('comms_add_user_if_not_exist()');
7      }
8    
9      if (user_load(array('name' => $follower['name']))) {
10        return;
11      }
12    
13      //add the user if it does not exist
14      $account = (object) array();
15      $options['name'] = $follower['name'];
16      $options['mail'] = $follower['name'] .'@twitter.com';
17    
18      if ($follower['twitterUID']) {
19        $options['twitterUID'] = $follower['twitterUID'];
20      }
21    
22      $result = user_save($account, $options);
23      if ($result) {
24        watchdog('comms', t('Adding new twitter follower, @name.', array('@name' => $result->name)));
25      }
26      else {
27        watchdog('comms', t('Failed adding the twitter follower, @name.', array('@name' => $follower['name'])));
28      }
29      return $result;
30    }
31    
32    //will try to get all new from messages (random tweets) and add them to the
33    //system
34    function comms_add_twitter_tweets() {
35      if (variable_get('comms_debuglevel', 0) > 0) {
36        drupal_set_message('comms_add_twitter_tweets()');
37      }
38    
39      //for each twitter user
40      //get all messages sent through the search api
41      //note we should not add the message id here.  If we do we need to rethink
42      //the logic above
43    
44      //get the list of our groups
45      $tusers = array();
46      $groups = module_invoke('og', 'all_groups_options');
47      while ($name = current($groups)) {
48        $account = comms_get_group_twitter_account(key($groups));
49        $tusers = array_merge($tusers, array($account['username'] => key($groups)));
50        next($groups);
51      }
52    
53      $messages = comms_get_twitter_tweets();
54    
55      if (is_array($messages)) {
56        foreach ($messages as $message) {
57          //$errors = comms_add_from_twitter($message['message'],
58          $errors = comms_add_comms_from_twitter($message['message'],
59            $message['user'], $message['time']);
60    
61          if (count($errors)) {
62            watchdog('comms_group', 'comms_cron() did not succeed: '.
63              $errors);
64          }
65        else {
66          $tuser_object = user_load(array('name' => $message['user']));
67          if ($tuser_object->comms_twitterfrommsgmaxid < $message['id']) {
68            user_save($tuser_object,array('comms_twitterfrommsgmaxid' => $message['id']));
69            }
70          }
71        }//foreach
72      }//if
73    }//comms_add_twitter_tweets
74    
75    //gets all users that were added from twitter.  Returns an array of twitter
76    //user names.
77    function comms_get_twitter_users() {
78      if (variable_get('comms_debuglevel', 0) > 0) {
79        drupal_set_message('comms_get_twitter_users()');
80      }
81    
82      $return = NULL;
83    
84      if (! ($result = db_query('SELECT uid from {users}'))) {
85        watchdog('comms',
86          t('Could not select the twitter users from the database.'));
87        return $return;
88      }
89    
90      while ($dbuser = db_fetch_array($result)) {
91        $user = user_load(array('uid' => $dbuser['uid']));
92        if ($user === FALSE) {
93          continue;
94        }
95    
96        if ($user->twitterUID) {
97          // This is where we can filter out pulling tweets for users below a
98          // certain point value.
99          $point_threshold = variable_get('comms_disablepostbelowpoint', 0);
100          if ($point_threshold != 0) {
101            if (userpoints_get_current_points($user->uid) > $point_threshold) {
102              $return[] = $user->name;
103            }
104          }
105          else {
106            $return[] = $user->name;
107          }
108        }
109      }//while
110      return $return;
111    }
112    
113    //gets all the followers for a a group that has a twitter account associated
114    //with it.  returns an an array of associative arrays in the form:
115    /*
116      Array
117      (
118        Array
119        (
120          [name] => <twitter username>,
121          [twitterUID] => <twitter user uid>
122        )[,...]
123      )
124    
125     */
126    function comms_get_twitter_followers($gid) {
127      if (variable_get('comms_debuglevel', 0) > 0) {
128        drupal_set_message('comms_get_twitter_followers()');
129      }
130    
131      $return = NULL;
132    
133      //if there is no twitter account associated borf!
134      $ta = comms_get_group_twitter_account($gid);
135      if (is_null($ta)) {
136        return $ta;
137      }
138    
139      //there is a situation here were we will only get 100 users from a request in order
140      //that they created their accounts.  If we have more then 100 users we would like to
141      //get all of them
142      $twitter = _comms_twitterAPI_load($ta['username'], $ta['password']);
143      $page = 1;
144      while (1) {
145        try {
146          $messages = $twitter->getFollowers('json', $page);
147        }catch (Exception $e) {
148          watchdog('comms_group', t('unable to get followers for gid:@gid, %mess',
149            array('@gid' => $gid, '%mess' => $e->getMessage())));
150    
151          comms_add_twitter_api_call(-1, $gid, 'getFollowers', 1, 0,
152            $e->getMessage());
153    
154          return $return;
155        }
156    
157        if ($messages->isError()) {
158          $error_message_web = scraping_twitter_error_page($messages->getData());
159          $error_message_json = json_decode($messages->getData(), 1);
160    
161          $message = t("There was a problem trying to get the followers associated with gid:@gid. Error: @errorweb. Error: @errorjson", array(
162            '@gid' => $gid,
163            '@errorweb' => $error_message_web,
164            '@errorjson' => $error_message_json['error']));
165    
166          watchdog("comms_group", $message);
167    
168          comms_add_twitter_api_call(-1, $gid, 'getFollowers', 1, 0, $message);
169    
170          return $return;
171        }
172      else {
173          $userct = 0;
174          foreach (json_decode($messages->getData(), 1) as $message) {
175            $userct++;
176            $name = $message['screen_name'];
177            $uid = $message['id'];
178            $msg = isset($message['status']['text']) ? $message['status']['text'] : NULL;
179            $msgid = isset($message['status']['id']) ? $message['status']['id'] : NULL;
180            $return[] = array(
181              'name' => $name,
182              'twitterUID' => $uid,
183              'message' => $msg,
184              'message_id' => $msgid
185            );
186          }//foreach
187    
188          if ($userct == 100) {
189            $page++;
190          }
191          else {
192            $message = t('Got %count followers for gid: @gid',
193              array('%count' => count($return), '@gid' => $gid));
194            comms_add_twitter_api_call(-1, $gid, 'getFollowers', 1, 1, $message);
195            return $return ? $return : array();
196          }//else
197        }//else
198      }//while
199    }//comms_get_twitter_followers
200    
201    //TODO: determine what the limit is on the number of from: searches we can do
202    //at one time and break the search up into multiple requests
203    //TODO: maybe we should limit this to a specific time as well ... for example
204    //maybe we only search for tweets newer then today
205    
206    //gets all tweets for all twitter users that have been added to the
207    //installation.  returns an array of associative arrays in the form:
208    /*
209      Array
210      (
211        Array
212        (
213          [user] => <twitter username>,
214          [message] => <tweet>,
215          [id] => <twitter message id>,
216          [twitterUID] => <twitter sender uid>
217        )[, ...]
218      )
219    */
220    function comms_get_twitter_tweets() {
221      if (variable_get('comms_debuglevel', 0) > 0) {
222        drupal_set_message('comms_get_twitter_tweets()');
223      }
224    
225      $return = NULL;
226    
227      $tusers = comms_get_twitter_users();
228    
229      if (count($tusers) <= 0) {
230        return $return;
231      }
232    
233      $twitter = _comms_twitterSearchAPI_load();
234    
235      // Run a separate twitter search for each user.
236      foreach($tusers as $tuser) {
237            $tuser_object = user_load(array('name' => $tuser));
238        $maxmsgid = $tuser_object->comms_twitterfrommsgmaxid;
239        if(!$maxmsgid) {
240          $maxmsgid = 0;
241        }
242        $page=1;
243        $break=0;
244        //There is a situation where we may have more then 100 tweets for this user
245        //since the last check
246        while (!$break && $page<5) {
247          try {
248            $messages = $twitter->getTweetsForUsers(array($tuser), 'json', $maxmsgid, $page);
249          }catch (Exception $e) {
250            $message =  t("There was a problem trying to get the tweets associated with user: @user. Error Message: @mess", array(
251              '@user' => $tuser, '@mess' => $e->getMessage()));
252    
253            watchdog('comms_group', $message);
254    
255            comms_add_twitter_api_call(-1, -1, 'twitterSearchAPI::from()', 0, 0,
256              $message);
257    
258            $break = 1;
259            continue;
260          }
261    
262          if ($messages->isError()) {
263    
264            //needed to get the error message when twitter can't even give a proper error
265            $error_message_web = scraping_twitter_error_page($messages->getData());
266            $error_message_json = json_decode($messages->getData(), 1);
267    
268            $message =  t("There was a problem trying to get the tweets associated with user: @user.Error Message: @errorweb. Error Message: @errorjson.", array(
269              '@user' => $tuser,
270              '@errorweb' => $error_message_web,
271              '@errorjson' => $error_message_json['error']));
272    
273            watchdog("comms_group", $message);
274    
275            comms_add_twitter_api_call(-1, -1, 'twitterSearchAPI::from()', 0, 0,
276              $message);
277    
278            $break = 1;
279            continue;
280          }
281          else {
282            $message_list = json_decode($messages->getData(), 1);
283            $message_list = $message_list['results'];
284    
285            if (is_array($message_list)) {
286              $msgct = 0;
287              foreach ($message_list as $message) {
288                $msgct++;
289                $text = $message['text'];
290                $sender = $message['from_user'];
291                $uid = $message['from_user_id'];
292                $id = $message['id'];
293                $return[] = array(
294                  'user' => $sender,
295                  'message' => $text,
296                  'id' => $id,
297                  'time' => strtotime($message['created_at']),
298                  'created_at' => $message['created_at'],
299                  'twitterUID' => $id);
300              }//foreach
301    
302              ($msgct >= 100) ? $page++ : $break=1;
303    
304            }
305            else {
306              $break=1;
307            }
308    
309            $message = t('Got %count tweets for user: @user', array(
310              '%count' => count($return), '@user' => $tuser));
311            comms_add_twitter_api_call(-1, -1, 'twitterSearchAPI::from()', 0, 1,
312              $message);
313          }//else
314        }//while
315      }//foreach
316    
317      $message = t('Got %count tweets for users: @user', array(
318        '%count' => count($return), '@user' => implode(', ', $tusers)));
319      watchdog("comms_group", $message);
320    
321      return $return;
322    }
323    
324    //gets all direct messages for a group that has a twitter account associated
325    //with it.  returns an array of associative arrays in the form:
326    /*
327      Array
328      (
329        Array
330        (
331          [user] => <twitter username>,
332          [message] => <twitter direct message>,
333          [id] => <twitter message id>,
334          [twitterUID] => <twitter sender uid>
335        )[, ...]
336      )
337    */
338    function comms_get_twitter_group_direct_messages($gid) {
339      if (variable_get('comms_debuglevel', 0) > 0) {
340        drupal_set_message('comms_get_twitter_group_direct_messages()');
341      }
342    
343      $return = NULL;
344    
345      //if there is no twitter account associated borf!
346      $ta = comms_get_group_twitter_account($gid);
347      if (is_null($ta)) {
348        return $ta;
349      }
350    
351      $twitter = _comms_twitterAPI_load($ta['username'], $ta['password']);
352    
353      //get the most recently recorded twitter direct message id
354      $msgid = variable_get('comms_twitterdirectmsgmaxid', 0);
355    
356      //this function call returns that last 20 direct messages that were recieved
357      //there is a possibility that we have recieved more then 20 direct messages
358      //since the last time that we checked, so we should continue to pull messages
359      //until we recieve one that is < comms_twitterdirectmsgmaxid
360      $page = 1;
361      while (1) {
362        try {
363          $messages = $twitter->getMessages('json', '', $msgid, $page);
364        }catch (Exception $e) {
365          watchdog('comms_group', t('unable to get direct messages for gid: @gid, message: @mess', array(
366            array('@gid' => $gid, '@mess' => $e->getMessage()))));
367    
368          comms_add_twitter_api_call(-1, $gid, 'getMessages', 1, 0,
369            $e->getMessage());
370    
371          return $return;
372        }
373    
374        if ($messages->isError()) {
375          $error_message_web = scraping_twitter_error_page($messages->getData());
376          $error_message_json = json_decode($messages->getData(), 1);
377    
378          $message = t("There was a problem trying to get the followers associated with gid:@gid. Error: @errorweb. Error: @errorjson", array(
379            '@gid' => $gid,
380            '@errorweb' => $error_message_web,
381            '@errorjson' => $error_message_json['error']));
382    
383          watchdog("comms_group", $message);
384    
385          comms_add_twitter_api_call(-1, $gid, 'getMessages', 1, 0, $message);
386    
387          return $return;
388        }
389      else {
390          $messagect = 0;
391          foreach (json_decode($messages->getData(), 1) as $message) {
392            $messagect++;
393            $text = $message['text'];
394              $sender = $message['sender']['screen_name'];
395            $uid = $message['sender']['id'];
396            $id = $message['id'];
397            $return[] = array(
398              'user' => $sender,
399              'message' => $text,
400              'id' => $id,
401              'twitterUID' => $id);
402          }//foreach
403    
404          if ($messagect == 20) {
405            $page++;
406          }
407          else {
408            $message = t('Got %count tweets for users: @users', array(
409              '%count' => count($return), '@users' => $tuser));
410            watchdog("comms_group", $message);
411            comms_add_twitter_api_call(-1, $gid, 'getMessages', 1, 1, $message);
412            return $return;
413          }
414        }//else
415      }//while
416    
417    }
418    
419    //Helper function for processing and adding status messages returned
420    //from comms_get_twitter_followers()
421    //@todo this needs to have the time added to it.
422    function comms_add_from_get_followers($followers) {
423      if (variable_get('comms_debuglevel', 0) > 0) {
424        drupal_set_message('comms_add_from_get_followers()');
425      }
426    
427      //we don't want to pull in data older then what we already have. With the
428      //exception that if we have not pulled anything in with this method before
429      //message id's from followers are not ordered
430      $max_msg_id = variable_get('comms_twitterfollowermsgmaxid', 0);
431      $new_max_msg_id = 0;
432      if (is_array($followers)) {
433        foreach ($followers as $name => $follower) {
434    
435          //some of the followers will not have a status associated with them
436          //we will just skip them
437          if (!isset($follower['message']) ||
438            !isset($follower['message_id']) ||
439            !isset($follower['name'])) {
440            continue;
441          }//if
442    
443          if ($follower['message_id'] > $max_msg_id) {
444            $errors = comms_add_comms_from_twitter($follower['message'],
445              $follower['name']);
446    
447            if (count($errors)) {
448              watchdog('comms_group',
449                t('comms_cron() did not succeed: %errors',
450                  array('%errors' => print_r(check_plain($errors), 1))));
451            }
452      else if ($follower['message_id'] > $new_max_msg_id) {
453                $new_max_msg_id = $follower['message_id'];
454            }
455          }
456        }//foreach
457    
458        if ($new_max_msg_id > $max_msg_id) {
459          variable_set('comms_twitterfollowermsgmaxid', $new_max_msg_id);
460        }//if
461      }//if
462    }
463    
464    function comms_add_comms_from_twitter($message, $user, $time = NULL, $reset = NULL) {
465      if (!isset($message) || !isset($user)) {
466        return;
467      }
468    
469      static $tusers;
470      if (!isset($tusers) || $reset) {
471        //get the list of our groups
472        $tusers = array();
473        $groups = module_invoke('og', 'all_groups_options');
474        while ($name = current($groups)) {
475          $account = comms_get_group_twitter_account(key($groups));
476          $tusers = array_merge($tusers, array($account['username'] => key($groups)));
477          next($groups);
478        }
479      }
480    
481      $values = array();
482      // @todo use the drupal time function for this.
483      $values['body'] = t('pulled from twitter at %date.', array(
484        '%date' => date('D, m/d/Y - H:i:s T')));
485    
486      //there can be encoded html characters in the message, we handle
487      //them here.
488      $message = htmlspecialchars_decode($message);
489    
490      //see simplefeed_item.module for an example of this
491      //we want to make sure here that this is nt a tweet meant for a
492      //group that we are not trancking
493      //then if this is a message directed at another user that is not
494      //one of the groups we care about we ignore it
495      //http://help.twitter.com/index.php?pg=kb.page&id=75
496      if (preg_match('/^@(\w+)\s(.*)$/', $message, $matches)) {
497        if (array_key_exists($matches[1], $tusers)) {
498          $values['sms_message'] = $matches[2];
499          $values['body'] = htmlspecialchars_decode($message) ."\n".
500            $values['body'];
501          $values['to'] = $matches[1];
502        }
503      else {
504          return;
505        }
506    
507    
508      }//if
509      else {
510        $values['body'] = $message ."\n". $values['body'];
511        $values['sms_message'] = $message;
512      }
513    
514      $node->type = 'comms';
515    
516      //for the title we want the first 8 words
517      $values['title'] = split(' ', $values['sms_message'], 8);
518      if (count($values['title']) == 8) {
519        array_pop($values['title']);
520      }
521      $values['title'] = implode(' ', $values['title']);
522    
523      //we want to add the group membership if there is one
524      if (!empty($values['to'])) {
525        $values['og_groups'] = array($tusers[$values['to']] => $tusers[$values['to']]);
526      }
527    
528      $values['name'] = $user;
529      $time = $time ? $time+variable_get('date_default_timezone', 0) : time();
530      $values['date'] = date('Y-m-d H:i:s', $time);
531    
532    
533      drupal_execute('comms_node_form', $values, $node);
534      $errors = form_get_errors();
535    
536      return $errors;
537    }
538    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.3