Пример #1
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
Пример #2
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)
Пример #3
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)
Пример #4
0
  def get(self, cron_id):
    def add_task_by_jid(jid):
      self.jids.append(jid)
      if len(self.jids) >= USERS_NUM_IN_TASK:
        flush_jids()
      if len(self.tasks) >= 100:
        flush_tasks()

    def flush_jids():
      if self.jids:
        self.tasks.append(taskqueue.Task(url='/worker', params={'jid': self.jids}))
        self.jids = list()

    def flush_tasks():
      def db_op():
        while db.WRITE_CAPABILITY:
          try:
            self.queues[self.queue_pointer].add(self.tasks)
          except taskqueue.TransientError:
            continue
          break

      if self.tasks:
        db.run_in_transaction(db_op)
        self.tasks = list()
        self.queue_pointer = (self.queue_pointer + 1) % TASK_QUEUE_NUM

    cron_id = int(cron_id)
    self.queues = [taskqueue.Queue('fetch' + str(id)) for id in xrange(TASK_QUEUE_NUM)]
    self.queue_pointer = cron_id % TASK_QUEUE_NUM
    self.tasks = list()
    self.jids = list()
    from db import GoogleUser, Db
    data = GoogleUser.get_all(shard=cron_id)
    try:
      for u in data:
        if u.display_timeline or u.msg_template.strip():
          time_delta = int(time()) - u.last_update
          if time_delta >= u.interval * 60 - 30:
            try:
              flag = xmpp.get_presence(u.jid)
            except xmpp.Error:
              flag = False
            if not flag:
              continue
            Db.set_cache(u)
            add_task_by_jid(u.jid)
      flush_jids()
      flush_tasks()
    except DeadlineExceededError:
      self.response.clear()
      self.response.set_status(500)
      self.response.out.write("This operation could not be completed in time...")
Пример #5
0
 def post(self):
     jid = self.request.get("from").split("/")[0]
     s = Session.get_by_key_name(jid)
     if not s:
         u = GoogleUser.get_by_jid(jid)
         if u and u.enabled_user and u.display_timeline and u.msg_template.strip():
             try:
                 Db.set_datastore(Session(key_name=jid, shard=u.shard))
             except CapabilityDisabledError:
                 try:
                     xmpp.send_presence(jid, presence_type=xmpp.PRESENCE_SHOW_AWAY)
                 except xmpp.Error:
                     pass
Пример #6
0
 def post(self):
     jid = self.request.get('from').split('/')[0]
     s = Session.get_by_key_name(jid)
     if s:
         u = GoogleUser.get_by_jid(jid)
         if u:
             try:
                 flag = xmpp.get_presence(jid)
             except xmpp.Error:
                 flag = False
             if not flag:
                 s.delete()
         else:
             s.delete()
Пример #7
0
 def post(self):
     jid = self.request.get("from").split("/")[0]
     s = Session.get_by_key_name(jid)
     if s:
         u = GoogleUser.get_by_jid(jid)
         if u:
             try:
                 flag = xmpp.get_presence(jid)
             except xmpp.Error:
                 flag = False
             if not flag:
                 s.delete()
         else:
             s.delete()
Пример #8
0
 def get(self, cron_id):
   try:
     cron_id = int(cron_id)
     data = GoogleUser.get_all(shard=cron_id)
     for u in data:
       jid = u.key().name()
       flag = xmpp.get_presence(jid)
       if flag:
         try:
           self.process(u)
         except CapabilityDisabledError:
           pass
   except DeadlineExceededError:
     pass
Пример #9
0
 def post(self):
     jid = self.request.get('from').split('/')[0]
     s = Session.get_by_key_name(jid)
     if not s:
         u = GoogleUser.get_by_jid(jid)
         if u and u.enabled_user and u.display_timeline and u.msg_template.strip(
         ):
             try:
                 Db.set_datastore(Session(key_name=jid, shard=u.shard))
             except CapabilityDisabledError:
                 try:
                     xmpp.send_presence(
                         jid, presence_type=xmpp.PRESENCE_SHOW_AWAY)
                 except xmpp.Error:
                     pass
Пример #10
0
def set_jid(jid):
  global _jid
  global _user
  _jid = jid
  _user = GoogleUser.get_by_jid(jid)
Пример #11
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())
Пример #12
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())
Пример #13
0
 def __init__(self, jid):
   self._jid = jid
   self._user = GoogleUser.get_by_jid(jid)
Пример #14
0
def set_jid(jid):
    global _jid
    global _user
    _jid = jid
    _user = GoogleUser.get_by_jid(jid)
Пример #15
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)
Пример #16
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())
Пример #17
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)