/[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.2.1 by evoltech, Tue Sep 22 22:26:47 2009 UTC revision 1.1.2.2 by evoltech, Wed Sep 23 22:58:47 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id:$  // $Id: comms_cron.inc,v 1.1.2.1 2009/09/22 22:26:47 evoltech Exp $
3    
4  function comms_add_user_by_name_if_not_exist($follower) {  function comms_add_user_by_name_if_not_exist($follower) {
5    if (variable_get('comms_debuglevel', 0) > 0) {    if (variable_get('comms_debuglevel', 0) > 0) {
# Line 50  function comms_add_twitter_tweets() { Line 50  function comms_add_twitter_tweets() {
50      next($groups);      next($groups);
51    }    }
52    
53    $messages = comms_get_twitter_tweets();    $messages = comms_get_twitter_tweets('handle_comms_from_twitter');
   
   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  
54  }//comms_add_twitter_tweets  }//comms_add_twitter_tweets
55    
56    /**
57     * Handles a comms item from twitter.
58     *
59     * Adds a new comms node and updates the the users comms_twitterfrommsgmaxid.
60     *
61     * @param $message an array with at least the following keys:
62     * array(
63     *   'message' => <twitter_message>,
64     *   'user' => <twitter_user that has already been added to the system>,
65     *   'time' => <timestamp of when the tweet was receved>,
66     * );
67     *
68     * @reutrn TRUE on success else FALSE
69     */
70    function handle_comms_from_twitter($message) {
71      $errors = comms_add_comms_from_twitter($message['message'],
72        $message['user'], $message['time']);
73    
74      if (count($errors)) {
75        watchdog('comms_group', 'handle_comms_from_twitter() did not succeed: '.
76          implode(', ', $errors));
77        return FALSE;
78      }
79      else {
80        $user = user_load(array('name' => $message['user']));
81        if ($user->comms_twitterfrommsgmaxid < $message['id']) {
82          user_save($user,array('comms_twitterfrommsgmaxid' => $message['id']));
83        }
84      }
85    
86      return TRUE;
87    }
88    
89  //gets all users that were added from twitter.  Returns an array of twitter  //gets all users that were added from twitter.  Returns an array of twitter
90  //user names.  //user names.
91  function comms_get_twitter_users() {  function comms_get_twitter_users() {
# Line 198  function comms_get_twitter_followers($gi Line 212  function comms_get_twitter_followers($gi
212    }//while    }//while
213  }//comms_get_twitter_followers  }//comms_get_twitter_followers
214    
215  //TODO: determine what the limit is on the number of from: searches we can do  /**
216  //at one time and break the search up into multiple requests   * Retrives tweets for all twitter users in the system using the Twitter
217  //TODO: maybe we should limit this to a specific time as well ... for example   * Search API.  Messages will be newer than the most recent message that
218  //maybe we only search for tweets newer then today   * was pulled for the user.  Users that do not have a maxmsgid will only
219     * retrieve the users first message.
220  //gets all tweets for all twitter users that have been added to the   *
221  //installation.  returns an array of associative arrays in the form:   * @param $callback A call back function that will be passed each tweet.
222  /*   *
223    Array   * @todo Ideally we would like to only retrieve messages for new users since
224    (   * they started following one of our groups, but I am not sure if there is a
225      Array   * way to determine this.
226      (   *
227        [user] => <twitter username>,   * @todo determine what the limit is on the number of from: searches we can do
228        [message] => <tweet>,   * at one time and break the search up into multiple requests
229        [id] => <twitter message id>,   *
230        [twitterUID] => <twitter sender uid>   * @todo: maybe we should limit this to a specific time as well ... for
231      )[, ...]   * example maybe we only search for tweets newer then today.
232    )   *
233  */   * @return If $callback was specified TRUE will be returned on success or a
234  function comms_get_twitter_tweets() {   * count of the number of errors that were recieved.  If $callback is not
235     * specified then an array of messages in the following format will be
236     * retuned:
237     * array(
238     *   Array (
239     *     [user] => <twitter username>,
240     *     [message] => <tweet>,
241     *     [id] => <twitter message id>,
242     *     [created_at] => <time reported by twitter that the tweet was created>,
243     *     [time] => strtotime([created_at]),
244     *     [twitterUID] => <twitter sender uid>
245     *   )[, ...]
246     * );
247     */
248    function comms_get_twitter_tweets($callback = null) {
249    if (variable_get('comms_debuglevel', 0) > 0) {    if (variable_get('comms_debuglevel', 0) > 0) {
250      drupal_set_message('comms_get_twitter_tweets()');      drupal_set_message('comms_get_twitter_tweets()');
251    }    }
252    
253    $return = NULL;    $return = NULL;
254      $errorct = 0;
255    
256    $tusers = comms_get_twitter_users();    $tusers = comms_get_twitter_users();
257    
# Line 256  function comms_get_twitter_tweets() { Line 285  function comms_get_twitter_tweets() {
285            $message);            $message);
286    
287          $break = 1;          $break = 1;
288            $errorct++;
289          continue;          continue;
290        }        }
291    
# Line 276  function comms_get_twitter_tweets() { Line 306  function comms_get_twitter_tweets() {
306            $message);            $message);
307    
308          $break = 1;          $break = 1;
309            $errorct++;
310          continue;          continue;
311        }        }
312        else {        else {
# Line 284  function comms_get_twitter_tweets() { Line 315  function comms_get_twitter_tweets() {
315    
316          if (is_array($message_list)) {          if (is_array($message_list)) {
317            $msgct = 0;            $msgct = 0;
318              print_r('$message_list for '. $tuser .': '. $message_list);
319            foreach ($message_list as $message) {            foreach ($message_list as $message) {
320              $msgct++;              $msgct++;
321              $text = $message['text'];              $text = $message['text'];
322              $sender = $message['from_user'];              $sender = $message['from_user'];
323              $uid = $message['from_user_id'];              $uid = $message['from_user_id'];
324              $id = $message['id'];              $id = $message['id'];
325              $return[] = array(              //$return[] = array(
326                $parsed_message = array(
327                'user' => $sender,                'user' => $sender,
328                'message' => $text,                'message' => $text,
329                'id' => $id,                'id' => $id,
330                'time' => strtotime($message['created_at']),                'time' => strtotime($message['created_at']),
331                'created_at' => $message['created_at'],                'created_at' => $message['created_at'],
332                'twitterUID' => $id);                'twitterUID' => $id);
333                if ($callback) {
334                  $callback($parsed_message);
335                }
336                else {
337                  $return[] = $parsed_message;
338                }
339    
340                // If the user did not have a max msg id then we are only
341                // going to take their first post.
342                // @see http://drupal.org/node/567476 #20.
343                if ($maxmsgid == 0) {
344                  $break =1;
345                  break;
346                }
347            }//foreach            }//foreach
348    
349            ($msgct >= 100) ? $page++ : $break=1;            ($msgct >= 100) ? $page++ : $break=1;
# Line 306  function comms_get_twitter_tweets() { Line 353  function comms_get_twitter_tweets() {
353            $break=1;            $break=1;
354          }          }
355    
356          $message = t('Got %count tweets for user: @user', array(          if (!$callback) {
357            '%count' => count($return), '@user' => $tuser));            $message = t('Got %count tweets for user: @user', array(
358                '%count' => count($return), '@user' => $tuser));
359            }
360            else {
361              $message = t('Requested tweets for user: @user', array(
362                '@user' => $tuser));
363            }
364          comms_add_twitter_api_call(-1, -1, 'twitterSearchAPI::from()', 0, 1,          comms_add_twitter_api_call(-1, -1, 'twitterSearchAPI::from()', 0, 1,
365            $message);            $message);
366        }//else        }//else
367      }//while      }//while
368    }//foreach    }//foreach
369    
370      if ($callback) {
371        return $errorct ? $errorct : TRUE;
372      }
373    
374    $message = t('Got %count tweets for users: @user', array(    $message = t('Got %count tweets for users: @user', array(
375      '%count' => count($return), '@user' => implode(', ', $tusers)));      '%count' => count($return), '@user' => implode(', ', $tusers)));
376    watchdog("comms_group", $message);    watchdog("comms_group", $message);

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

  ViewVC Help
Powered by ViewVC 1.1.3