Esempio n. 1
0
 def func_bind(self, args, oauth_token=None):
     if len(args) == 1 and args[0].isalnum():
         jid = self._google_user.jid
         if oauth_token is None:
             twitter_user = TwitterUser.get_by_twitter_name(None, jid)
             if twitter_user is None:
                 return _("INVALID_PIN_CODE") % ""
             oauth_token = twitter_user.access_token_key
         token = oauth.Token(oauth_token)
         token.set_verifier(args[0].encode("UTF8"))
         consumer = oauth.Consumer(config.OAUTH_CONSUMER_KEY, config.OAUTH_CONSUMER_SECRET)
         client = oauth.Client(consumer, token)
         resp = client.request(twitter.ACCESS_TOKEN_URL, "POST")
         if not resp:
             return _("NETWORK_ERROR")
         access_token = dict(cgi.parse_qsl(resp))
         if "oauth_token" not in access_token:
             return _("INVALID_PIN_CODE") % args[0]
         if oauth_token is None:
             twitter_user.delete()
         TwitterUser.add(
             jid, access_token["oauth_token"], access_token["oauth_token_secret"], access_token["screen_name"]
         )
         if self._google_user.enabled_user == "":
             self._google_user.enabled_user = access_token["screen_name"]
         if IdList.get_by_jid(jid, self._google_user.shard) is None:
             IdList.add(jid, self._google_user.shard)
         return _("SUCCESSFULLY_BIND") % access_token["screen_name"]
     else:
         raise NotImplementedError
Esempio n. 2
0
 def func_bind(self, args, oauth_token=None):
     if len(args) == 1 and args[0].isalnum():
         jid = self._google_user.jid
         if oauth_token is None:
             twitter_user = TwitterUser.get_by_twitter_name(None, jid)
             if twitter_user is None:
                 return _('INVALID_PIN_CODE') % ''
             oauth_token = twitter_user.access_token_key
         token = oauth.Token(oauth_token)
         token.set_verifier(args[0].encode('UTF8'))
         consumer = oauth.Consumer(config.OAUTH_CONSUMER_KEY,
                                   config.OAUTH_CONSUMER_SECRET)
         client = oauth.Client(consumer, token)
         resp = client.request(twitter.ACCESS_TOKEN_URL, "POST")
         if not resp:
             return _('NETWORK_ERROR')
         access_token = dict(cgi.parse_qsl(resp))
         if 'oauth_token' not in access_token:
             return _('INVALID_PIN_CODE') % args[0]
         if oauth_token is None:
             twitter_user.delete()
         TwitterUser.add(jid, access_token['oauth_token'],
                         access_token['oauth_token_secret'],
                         access_token['screen_name'])
         if self._google_user.enabled_user == '':
             self._google_user.enabled_user = access_token['screen_name']
         if IdList.get_by_jid(jid, self._google_user.shard) is None:
             IdList.add(jid, self._google_user.shard)
         return _('SUCCESSFULLY_BIND') % access_token['screen_name']
     else:
         raise NotImplementedError
Esempio n. 3
0
 def func_bind(self, args, oauth_token=None):
   if len(args) == 1 and args[0].isdigit():
     twitter_user = None
     if oauth_token is None:
       twitter_user = TwitterUser.get_by_twitter_name(None, self._google_user.jid)
       if twitter_user is None:
         return _('INVALID_PIN_CODE') % ''
       oauth_token = twitter_user.access_token_key
     token = oauth.Token(oauth_token)
     if type(args[0]) != unicode:
       args[0] = args[0].encode('UTF8')
     token.set_verifier(args[0])
     consumer = oauth.Consumer(config.OAUTH_CONSUMER_KEY, config.OAUTH_CONSUMER_SECRET)
     client = oauth.Client(consumer, token)
     resp = client.request(twitter.ACCESS_TOKEN_URL, "POST")
     if not resp:
       return _('NETWORK_ERROR')
     access_token = dict(cgi.parse_qsl(resp))
     if 'oauth_token' not in access_token:
       return _('INVALID_PIN_CODE') % args[0]
     if twitter_user is not None:
       twitter_user.delete()
     TwitterUser.add(self._google_user.jid, access_token['oauth_token'], access_token['oauth_token_secret'],
                     access_token['screen_name'])
     if self._google_user.enabled_user == '':
       self._google_user.enabled_user = access_token['screen_name']
     if IdList.get_by_jid(self._google_user.jid, self._google_user.shard) is None:
       IdList.add(self._google_user.jid, self._google_user.shard)
     return _('SUCCESSFULLY_BIND') % access_token['screen_name']
   else:
     raise NotImplementedError
Esempio n. 4
0
 def process(self, message):
   global _locale
   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
   self._twitter_user = Dummy()
   self._api = Dummy()
   if self._google_user.enabled_user:
     self._twitter_user = TwitterUser.get_by_twitter_name(self._google_user.enabled_user, self._google_user.jid)
     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)
   self._utils = Utils(self._google_user.jid)
   try:
     result = self.parse_command(message.body)
   except NotImplementedError:
     result = _('INVALID_COMMAND')
   except twitter.TwitterInternalServerError:
     result = _('INTERNAL_SERVER_ERROR')
   except twitter.TwitterAuthenticationError:
     self._google_user.retry += 1
     if self._google_user.retry >= config.MAX_RETRY:
       GoogleUser.disable(self._google_user.jid)
     else:
       Db.set_datastore(self._google_user)
     result = _('NO_AUTHENTICATION')
   return result
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
0
 def func_remove(self, args):
     length = len(args)
     msg = ""
     if length <= 1:
         if not length:
             user = self._google_user.enabled_user
         else:
             user = args[0]
         if user != "":
             twitter_user = TwitterUser.get_by_twitter_name(user, self._google_user.jid)
             if twitter_user is not None:
                 twitter_user.delete()
                 memcache.delete(self._google_user.jid + ":" + user, namespace="twitter_name")
                 if self._google_user.enabled_user == user:
                     self._google_user.enabled_user = ""
                 msg += _("TWITTER_USER_DELETED") % user
         msg += _("SPECIFY_ANOTHER_USER")
         return msg
     raise NotImplementedError
Esempio n. 8
0
 def func_remove(self, args):
   length = len(args)
   msg = ''
   if length <= 1:
     if not length:
       user = self._google_user.enabled_user
     else:
       user = args[0]
     if user != '':
       twitter_user = TwitterUser.get_by_twitter_name(user, self._google_user.jid)
       if twitter_user is not None:
         twitter_user.delete()
         memcache.delete(self._google_user.jid + ':' + user, namespace='twitter_name')
         if self._google_user.enabled_user == user:
           self._google_user.enabled_user = ''
         msg += _('TWITTER_USER_DELETED') % user
     msg += _('SPECIFY_ANOTHER_USER')
     return msg
   raise NotImplementedError
Esempio n. 9
0
 def func_remove(self, args):
     length = len(args)
     msg = ''
     if length <= 1:
         if not length:
             user = self._google_user.enabled_user
         else:
             user = args[0]
         if user != '':
             twitter_user = TwitterUser.get_by_twitter_name(
                 user, self._google_user.jid)
             if twitter_user is not None:
                 twitter_user.delete()
                 memcache.delete(self._google_user.jid + ':' + user,
                                 namespace='twitter_name')
                 if self._google_user.enabled_user == user:
                     self._google_user.enabled_user = ''
                 msg += _('TWITTER_USER_DELETED') % user
         msg += _('SPECIFY_ANOTHER_USER')
         return msg
     raise NotImplementedError
Esempio n. 10
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())
Esempio n. 11
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)
Esempio n. 12
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)
Esempio n. 13
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())
Esempio n. 14
0
  def process(self, google_user):
    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)
    self._utils = Utils(google_user.jid)
    self._utils.allow_duplicate = False
    at_username = '******' + google_user.enabled_user
    statuses_id = []
    mention_statuses = []
    list_content = ''
    dm_content = ''
    mention_content = ''
    home_content = ''

    if google_user.display_timeline & MODE_DM:
      try:
        statuses = api.get_direct_messages(since_id=google_user.last_dm_id)
        if statuses and statuses[0]['id'] > google_user.last_dm_id:
          google_user.last_dm_id = statuses[0]['id']
        dm_content = self._utils.parse_statuses(statuses)
      except twitter.TwitterInternalServerError:
        pass
      except BaseException:
        err = StringIO()
        traceback.print_exc(file=err)
        logging.error(google_user.jid + ' DM:\n' + err.getvalue())

    if google_user.display_timeline & MODE_LIST:
      try:
        list_statuses = api.get_list_statuses(user=google_user.list_user, id=google_user.list_id, since_id=google_user.last_list_id)
        if list_statuses:
          if list_statuses[0]['id'] > google_user.last_list_id:
            google_user.last_list_id = list_statuses[0]['id']
          if google_user.display_timeline & MODE_MENTION:
            for i in range(len(list_statuses) - 1, -1, -1):
              x = list_statuses[i]
              statuses_id.append(x['id'])
              if at_username in x['text']:
                mention_statuses.append(x)
                del list_statuses[i]
          list_content = self._utils.parse_statuses(list_statuses, filter_self=True)
      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())