Exemple #1
0
 def process(self):
     global _locale
     try:
         message = xmpp.Message(self.request.POST)
     except xmpp.InvalidMessageError:
         return
     jid = message.sender.split("/")[0]
     self._google_user = GoogleUser.get_by_jid(jid)
     if self._google_user is None:
         self._google_user = GoogleUser.add(jid)
     _locale = self._google_user.locale
     if self._google_user.enabled_user:
         self._twitter_user = TwitterUser.get_by_twitter_name(self._google_user.enabled_user, self._google_user.jid)
         self._api = Dummy()
         if self._twitter_user is None:
             self._google_user.enabled_user = ""
         else:
             self._api = twitter.Api(
                 consumer_key=config.OAUTH_CONSUMER_KEY,
                 consumer_secret=config.OAUTH_CONSUMER_SECRET,
                 access_token_key=self._twitter_user.access_token_key,
                 access_token_secret=self._twitter_user.access_token_secret,
             )
             try:
                 self._user = self._api.verify_credentials()
                 if not self._user:
                     raise twitter.TwitterAuthenticationError
             except twitter.TwitterAuthenticationError:
                 self._google_user.retry += 1
                 if self._google_user.retry >= config.MAX_RETRY:
                     GoogleUser.disable(self._google_user.jid)
                     xmpp.send_message(self._google_user.jid, _("NO_AUTHENTICATION"))
                 else:
                     Db.set_datastore(self._google_user)
                 return
             else:
                 if self._google_user.retry > 0:
                     self._google_user.retry = 0
                 if self._twitter_user.twitter_name != self._user["screen_name"]:
                     self._twitter_user.twitter_name = self._user["screen_name"]
                     self._google_user.enabled_user = self._user["screen_name"]
     else:
         self._twitter_user = Dummy()
         self._api = Dummy()
         self._user = Dummy()
     utils.set_jid(self._google_user.jid)
     result = self.parse_command(message.body)
     if result is None:
         return
     if result:
         while CapabilitySet("xmpp").is_enabled():
             try:
                 message.reply(result)
             except xmpp.Error:
                 pass
             else:
                 break
     IdList.flush(self._google_user.jid)
     Db.set_datastore(self._google_user)
Exemple #2
0
 def post(self):
   global _locale
   try:
     message = xmpp.Message(self.request.POST)
   except xmpp.InvalidMessageError:
     return
   jid = message.sender.split('/')[0]
   self._google_user = GoogleUser.get_by_jid(jid)
   if self._google_user is None:
     self._google_user = GoogleUser.add(jid)
   _locale = self._google_user.locale
   if self._google_user.enabled_user:
     self._twitter_user = TwitterUser.get_by_twitter_name(self._google_user.enabled_user, self._google_user.jid)
     self._api = Dummy()
     if self._twitter_user is None:
       self._google_user.enabled_user = ''
     else:
       self._api = twitter.Api(consumer_key=config.OAUTH_CONSUMER_KEY,
                               consumer_secret=config.OAUTH_CONSUMER_SECRET,
                               access_token_key=self._twitter_user.access_token_key,
                               access_token_secret=self._twitter_user.access_token_secret)
       try:
         self._user = self._api.verify_credentials()
       except twitter.TwitterAuthenticationError:
         self._google_user.retry += 1
         if self._google_user.retry >= config.MAX_RETRY:
           GoogleUser.disable(self._google_user.jid)
           xmpp.send_message(self._google_user.jid, _('NO_AUTHENTICATION'))
         else:
           Db.set_datastore(self._google_user)
         return
       else:
         if self._google_user.retry > 0:
           self._google_user.retry = 0
         try:
           if self._twitter_user.twitter_name != self._user['screen_name']:
             self._twitter_user.twitter_name = self._user['screen_name']
             self._google_user.enabled_user = self._user['screen_name']
         except TypeError:
           logging.error('%s: %s' % (jid, unicode(self._user)))
           return
   else:
     self._twitter_user = Dummy()
     self._api = Dummy()
     self._user = Dummy()
   utils.set_jid(self._google_user.jid)
   result = self.parse_command(message.body)
   if result is None:
     return
   if result:
     message.reply(result)
   IdList.flush(self._google_user.jid)
   Db.set_datastore(self._google_user)
Exemple #3
0
    def process(self, u):
        jid = u.key().name()
        try:
            flag = xmpp.get_presence(jid)
        except (xmpp.Error, DeadlineExceededError):
            flag = True
        if not flag:
            u.delete()
            return
        google_user = GoogleUser.get_by_jid(jid)
        if google_user is None:
            u.delete()
            return
        time_delta = int(time()) - google_user.last_update
        if time_delta < google_user.interval * 60 - 30:
            return
        _ = lambda x: gettext(x, locale=google_user.locale)
        twitter_user = TwitterUser.get_by_twitter_name(
            google_user.enabled_user, google_user.jid)
        if twitter_user is None:
            google_user.enabled_user = ''
            Db.set_datastore(google_user)
            return
        api = twitter.Api(consumer_key=config.OAUTH_CONSUMER_KEY,
                          consumer_secret=config.OAUTH_CONSUMER_SECRET,
                          access_token_key=twitter_user.access_token_key,
                          access_token_secret=twitter_user.access_token_secret)
        try:
            self._user = api.verify_credentials()
            if not self._user or 'screen_name' not in self._user:
                raise twitter.TwitterError
        except twitter.TwitterError:
            google_user.retry += 1
            if google_user.retry >= config.MAX_RETRY:
                GoogleUser.disable(jid=google_user.jid)
                xmpp.send_message(google_user.jid, _('NO_AUTHENTICATION'))
            else:
                Db.set_cache(google_user)
            return
        finally:
            if google_user.retry > 0:
                google_user.retry = 0
                Db.set_cache(google_user)
        if twitter_user.twitter_name != self._user['screen_name']:
            twitter_user.twitter_name = self._user['screen_name']
            Db.set_cache(twitter_user)
            google_user.enabled_user = self._user['screen_name']
            Db.set_cache(google_user)
        utils.set_jid(google_user.jid)
        home_statuses = []
        home_mention_statuses = []
        all_statuses = []
        at_username = '******' + google_user.enabled_user

        if google_user.display_timeline & MODE_HOME or google_user.display_timeline & MODE_MENTION:
            home_rpc = api.get_home_timeline(since_id=google_user.last_msg_id,
                                             async=True)
        else:
            home_rpc = None
        if google_user.display_timeline & MODE_LIST:
            list_rpc = api.get_list_statuses(user=google_user.list_user,
                                             id=google_user.list_id,
                                             since_id=google_user.last_list_id,
                                             async=True)
        else:
            list_rpc = None
        if google_user.display_timeline & MODE_MENTION:
            mention_rpc = api.get_mentions(
                since_id=google_user.last_mention_id, async=True)
        else:
            mention_rpc = None
        if google_user.display_timeline & MODE_DM:
            dm_rpc = api.get_direct_messages(since_id=google_user.last_dm_id,
                                             async=True)
        else:
            dm_rpc = None
        if google_user.display_timeline & MODE_HOME:
            try:
                home_statuses = api._process_result(home_rpc)
                if home_statuses:
                    all_statuses.extend(home_statuses)
                    if home_statuses[0]['id'] > google_user.last_msg_id:
                        google_user.last_msg_id = home_statuses[0]['id']
            except twitter.TwitterInternalServerError:
                pass
            except BaseException:
                err = StringIO('')
                traceback.print_exc(file=err)
                logging.error(google_user.jid + ' Home:\n' + err.getvalue())
        if google_user.display_timeline & MODE_MENTION:
            try:
                statuses = api._process_result(mention_rpc)
                if statuses:
                    all_statuses.extend(statuses)
                    if statuses[0]['id'] > google_user.last_mention_id:
                        google_user.last_mention_id = statuses[0]['id']
                if not google_user.display_timeline & MODE_HOME:
                    try:
                        home_statuses = api._process_result(home_rpc)
                    except twitter.TwitterInternalServerError:
                        pass
                    except BaseException:
                        err = StringIO('')
                        traceback.print_exc(file=err)
                        logging.error(google_user.jid + ' Home:\n' +
                                      err.getvalue())
                    else:
                        if home_statuses:
                            if home_statuses[0]['id'] > google_user.last_msg_id:
                                google_user.last_msg_id = home_statuses[0][
                                    'id']
                            home_mention_statuses = [
                                x for x in home_statuses
                                if at_username in x['text']
                                and x['id'] > google_user.last_mention_id
                            ]
                        if home_mention_statuses:
                            all_statuses.extend(home_mention_statuses)
            except twitter.TwitterInternalServerError:
                pass
            except BaseException:
                err = StringIO('')
                traceback.print_exc(file=err)
                logging.error(google_user.jid + ' Mention:\n' + err.getvalue())
        if google_user.display_timeline & MODE_LIST:
            try:
                statuses = api._process_result(list_rpc)
                if statuses:
                    if statuses[0]['id'] > google_user.last_list_id:
                        google_user.last_list_id = statuses[0]['id']
                    for i in range(len(statuses) - 1, -1, -1):
                        if at_username in statuses[i]['text'] and statuses[i][
                                'id'] <= google_user.last_mention_id:
                            del statuses[i]
                    all_statuses.extend(statuses)
            except twitter.TwitterInternalServerError:
                pass
            except BaseException, e:
                if 'Not found' not in e.message:
                    err = StringIO('')
                    traceback.print_exc(file=err)
                    logging.error(google_user.jid + ' List:\n' +
                                  err.getvalue())
Exemple #4
0
 def process(self):
     global _locale
     try:
         message = xmpp.Message(self.request.POST)
     except xmpp.InvalidMessageError:
         return
     jid = message.sender.split('/')[0]
     self._google_user = GoogleUser.get_by_jid(jid)
     if self._google_user is None:
         self._google_user = GoogleUser.add(jid)
     _locale = self._google_user.locale
     if self._google_user.enabled_user:
         self._twitter_user = TwitterUser.get_by_twitter_name(
             self._google_user.enabled_user, self._google_user.jid)
         self._api = Dummy()
         if self._twitter_user is None:
             self._google_user.enabled_user = ''
         else:
             self._api = twitter.Api(
                 consumer_key=config.OAUTH_CONSUMER_KEY,
                 consumer_secret=config.OAUTH_CONSUMER_SECRET,
                 access_token_key=self._twitter_user.access_token_key,
                 access_token_secret=self._twitter_user.access_token_secret)
             try:
                 self._user = self._api.verify_credentials()
                 if not self._user:
                     raise twitter.TwitterAuthenticationError
             except twitter.TwitterAuthenticationError:
                 self._google_user.retry += 1
                 if self._google_user.retry >= config.MAX_RETRY:
                     GoogleUser.disable(self._google_user.jid)
                     xmpp.send_message(self._google_user.jid,
                                       _('NO_AUTHENTICATION'))
                 else:
                     Db.set_datastore(self._google_user)
                 return
             else:
                 if self._google_user.retry > 0:
                     self._google_user.retry = 0
                 if self._twitter_user.twitter_name != self._user[
                         'screen_name']:
                     self._twitter_user.twitter_name = self._user[
                         'screen_name']
                     self._google_user.enabled_user = self._user[
                         'screen_name']
     else:
         self._twitter_user = Dummy()
         self._api = Dummy()
         self._user = Dummy()
     utils.set_jid(self._google_user.jid)
     result = self.parse_command(message.body)
     if result is None:
         return
     if result:
         while CapabilitySet('xmpp').is_enabled():
             try:
                 message.reply(result)
             except xmpp.Error:
                 pass
             else:
                 break
     IdList.flush(self._google_user.jid)
     Db.set_datastore(self._google_user)
Exemple #5
0
  def post(self):
    if db.WRITE_CAPABILITY:
      jids = self.request.get_all('jid')
      for jid in jids:
        try:
          google_user = GoogleUser.get_by_jid(jid)
        except db.Error:
          continue
        _ = lambda x: gettext(x, locale=google_user.locale)
        try:
          twitter_user = TwitterUser.get_by_twitter_name(google_user.enabled_user, google_user.jid)
        except db.Error:
          continue
        if twitter_user is None:
          google_user.enabled_user = ''
          Db.set_datastore(google_user)
          continue
        api = twitter.Api(consumer_key=config.OAUTH_CONSUMER_KEY,
                          consumer_secret=config.OAUTH_CONSUMER_SECRET,
                          access_token_key=twitter_user.access_token_key,
                          access_token_secret=twitter_user.access_token_secret)
        try:
          self._user = api.verify_credentials()
          if 'screen_name' not in self._user:
            raise twitter.TwitterError
        except twitter.TwitterError:
          google_user.retry += 1
          if google_user.retry >= config.MAX_RETRY:
            GoogleUser.disable(jid=google_user.jid)
            xmpp.send_message(google_user.jid, _('NO_AUTHENTICATION'))
          else:
            Db.set_cache(google_user)
          return
        finally:
          if google_user.retry > 0:
            google_user.retry = 0
            Db.set_cache(google_user)
        if twitter_user.twitter_name != self._user['screen_name']:
          twitter_user.twitter_name = self._user['screen_name']
          Db.set_cache(twitter_user)
          google_user.enabled_user = self._user['screen_name']
          Db.set_cache(google_user)
        utils.set_jid(google_user.jid)
        home_statuses = []
        home_mention_statuses = []
        all_statuses = []

        if google_user.display_timeline & MODE_HOME:
          home_rpc = api.get_home_timeline(since_id=google_user.last_msg_id, async=True)
        else:
          home_rpc = api.get_home_timeline(since_id=google_user.last_mention_id, async=True)
        if google_user.display_timeline & MODE_LIST:
          list_rpc = api.get_list_statuses(user=google_user.list_user, id=google_user.list_id,
                                           since_id=google_user.last_list_id, async=True)
        else:
          list_rpc = None
        if google_user.display_timeline & MODE_MENTION:
          mention_rpc = api.get_mentions(since_id=google_user.last_mention_id, async=True)
        else:
          mention_rpc = None
        if google_user.display_timeline & MODE_DM:
          dm_rpc = api.get_direct_messages(since_id=google_user.last_dm_id, async=True)
        else:
          dm_rpc = None
        if google_user.display_timeline & MODE_HOME:
          try:
            home_statuses = api._process_result(home_rpc)
            if home_statuses:
              all_statuses.extend(home_statuses)
              if home_statuses[0]['id'] > google_user.last_msg_id:
                google_user.last_msg_id = home_statuses[0]['id']
          except twitter.TwitterInternalServerError:
            pass
          except BaseException:
            err = StringIO('')
            traceback.print_exc(file=err)
            logging.error(google_user.jid + ' Home:\n' + err.getvalue())
        if google_user.display_timeline & MODE_LIST:
          try:
            statuses = api._process_result(list_rpc)
            if statuses:
              all_statuses.extend(statuses)
              if statuses[0]['id'] > google_user.last_list_id:
                google_user.last_list_id = statuses[0]['id']
          except twitter.TwitterInternalServerError:
            pass
          except BaseException, e:
            if 'Not found' not in e.message:
              err = StringIO('')
              traceback.print_exc(file=err)
              logging.error(google_user.jid + ' List:\n' + err.getvalue())
        if google_user.display_timeline & MODE_MENTION:
          try:
            statuses = api._process_result(mention_rpc)
            all_statuses.extend(statuses)
            if not google_user.display_timeline & MODE_HOME:
              try:
                home_statuses = api._process_result(home_rpc)
              except twitter.TwitterInternalServerError:
                pass
              except BaseException:
                err = StringIO('')
                traceback.print_exc(file=err)
                logging.error(google_user.jid + ' Home:\n' + err.getvalue())
            if home_statuses:
              if home_statuses[0]['id'] > google_user.last_mention_id:
                google_user.last_mention_id = home_statuses[0]['id']
              at_username = '******' + google_user.enabled_user
              home_mention_statuses = [x for x in home_statuses if at_username in x['text']]
            if home_mention_statuses:
              all_statuses.extend(home_mention_statuses)
            if home_statuses and home_statuses[0]['id'] > google_user.last_mention_id:
              google_user.last_mention_id = home_statuses[0]['id']
            if statuses and statuses[0]['id'] > google_user.last_mention_id:
              google_user.last_mention_id = statuses[0]['id']
          except twitter.TwitterInternalServerError:
            pass
          except BaseException:
            err = StringIO('')
            traceback.print_exc(file=err)
            logging.error(google_user.jid + ' Mention:\n' + err.getvalue())
        if all_statuses:
          all_statuses.sort(cmp=lambda x, y: cmp(x['id'], y['id']))
          last = all_statuses[-1]['id']
          for i in range(len(all_statuses) - 2, -1, -1):
            if last == all_statuses[i]['id']:
              del all_statuses[i]
            else:
              last = all_statuses[i]['id']
          content = utils.parse_statuses(all_statuses, filter_self=True, reverse=False)
          if content.strip():
            IdList.flush(google_user.jid)
            xmpp.send_message(google_user.jid, content)
        if google_user.display_timeline & MODE_DM:
          try:
            statuses = api._process_result(dm_rpc)
            content = utils.parse_statuses(statuses)
            if content.strip():
              IdList.flush(google_user.jid)
              xmpp.send_message(google_user.jid, _('DIRECT_MESSAGES') + '\n\n' + content)
              if statuses[-1]['id'] > google_user.last_dm_id:
                google_user.last_dm_id = statuses[-1]['id']
          except twitter.TwitterInternalServerError:
            pass
          except BaseException:
            err = StringIO('')
            traceback.print_exc(file=err)
            logging.error(google_user.jid + ' DM:\n' + err.getvalue())
        google_user.last_update = int(time())
        Db.set_datastore(google_user)
Exemple #6
0
  def process(self, u):
      jid = u.key().name()
      try:
        flag = xmpp.get_presence(jid)
      except (xmpp.Error, DeadlineExceededError):
        flag = True
      if not flag:
        u.delete()
        return
      google_user = GoogleUser.get_by_jid(jid)
      if google_user is None:
        u.delete()
        return
      time_delta = int(time()) - google_user.last_update
      if time_delta < google_user.interval * 60 - 30:
        return
      _ = lambda x: gettext(x, locale=google_user.locale)
      twitter_user = TwitterUser.get_by_twitter_name(google_user.enabled_user, google_user.jid)
      if twitter_user is None:
        google_user.enabled_user = ''
        Db.set_datastore(google_user)
        return
      api = twitter.Api(consumer_key=config.OAUTH_CONSUMER_KEY,
                        consumer_secret=config.OAUTH_CONSUMER_SECRET,
                        access_token_key=twitter_user.access_token_key,
                        access_token_secret=twitter_user.access_token_secret)
      try:
        self._user = api.verify_credentials()
        if not self._user or 'screen_name' not in self._user:
          raise twitter.TwitterError
      except twitter.TwitterError:
        google_user.retry += 1
        if google_user.retry >= config.MAX_RETRY:
          GoogleUser.disable(jid=google_user.jid)
          xmpp.send_message(google_user.jid, _('NO_AUTHENTICATION'))
        else:
          Db.set_cache(google_user)
        return
      finally:
        if google_user.retry > 0:
          google_user.retry = 0
          Db.set_cache(google_user)
      if twitter_user.twitter_name != self._user['screen_name']:
        twitter_user.twitter_name = self._user['screen_name']
        Db.set_cache(twitter_user)
        google_user.enabled_user = self._user['screen_name']
        Db.set_cache(google_user)
      utils.set_jid(google_user.jid)
      home_statuses = []
      home_mention_statuses = []
      all_statuses = []
      at_username = '******' + google_user.enabled_user

      if google_user.display_timeline & MODE_HOME or google_user.display_timeline & MODE_MENTION:
        home_rpc = api.get_home_timeline(since_id=google_user.last_msg_id, async=True)
      else:
        home_rpc = None
      if google_user.display_timeline & MODE_LIST:
        list_rpc = api.get_list_statuses(user=google_user.list_user, id=google_user.list_id,
                                         since_id=google_user.last_list_id, async=True)
      else:
        list_rpc = None
      if google_user.display_timeline & MODE_MENTION:
        mention_rpc = api.get_mentions(since_id=google_user.last_mention_id, async=True)
      else:
        mention_rpc = None
      if google_user.display_timeline & MODE_DM:
        dm_rpc = api.get_direct_messages(since_id=google_user.last_dm_id, async=True)
      else:
        dm_rpc = None
      if google_user.display_timeline & MODE_HOME:
        try:
          home_statuses = api._process_result(home_rpc)
          if home_statuses:
            all_statuses.extend(home_statuses)
            if home_statuses[0]['id'] > google_user.last_msg_id:
              google_user.last_msg_id = home_statuses[0]['id']
        except twitter.TwitterInternalServerError:
          pass
        except BaseException:
          err = StringIO('')
          traceback.print_exc(file=err)
          logging.error(google_user.jid + ' Home:\n' + err.getvalue())
      if google_user.display_timeline & MODE_MENTION:
        try:
          statuses = api._process_result(mention_rpc)
          if statuses:
            all_statuses.extend(statuses)
            if statuses[0]['id'] > google_user.last_mention_id:
              google_user.last_mention_id = statuses[0]['id']
          if not google_user.display_timeline & MODE_HOME:
            try:
              home_statuses = api._process_result(home_rpc)
            except twitter.TwitterInternalServerError:
              pass
            except BaseException:
              err = StringIO('')
              traceback.print_exc(file=err)
              logging.error(google_user.jid + ' Home:\n' + err.getvalue())
            else:
              if home_statuses:
                if home_statuses[0]['id'] > google_user.last_msg_id:
                  google_user.last_msg_id = home_statuses[0]['id']
                home_mention_statuses = [x for x in home_statuses if
                                         at_username in x['text'] and x['id'] > google_user.last_mention_id]
              if home_mention_statuses:
                all_statuses.extend(home_mention_statuses)
        except twitter.TwitterInternalServerError:
          pass
        except BaseException:
          err = StringIO('')
          traceback.print_exc(file=err)
          logging.error(google_user.jid + ' Mention:\n' + err.getvalue())
      if google_user.display_timeline & MODE_LIST:
        try:
          statuses = api._process_result(list_rpc)
          if statuses:
            if statuses[0]['id'] > google_user.last_list_id:
              google_user.last_list_id = statuses[0]['id']
            for i in range(len(statuses) - 1, -1, -1):
              if at_username in statuses[i]['text'] and statuses[i]['id'] <= google_user.last_mention_id:
                del statuses[i]
            all_statuses.extend(statuses)
        except twitter.TwitterInternalServerError:
          pass
        except BaseException, e:
          if 'Not found' not in e.message:
            err = StringIO('')
            traceback.print_exc(file=err)
            logging.error(google_user.jid + ' List:\n' + err.getvalue())