Exemplo n.º 1
0
 def func_fav(self, args):
     length = len(args)
     if not length or (length == 1 and (args[0][0].lower() == 'p'
                                        and args[0][1:].isdigit())):
         page = args[0][1:] if length else 1
         statuses = self._api.get_favorites(page=int(page))
         return _('FAVOURITES') + _('PAGE') % str(
             page) + '\n\n' + utils.parse_statuses(statuses)
     elif length == 1 and (args[0].isdigit() or
                           (args[0] and args[0][0] == '#'
                            and args[0][1:].isdigit())):
         if args[0][0] == '#':
             short_id = int(args[0][1:])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
             short_id = int(args[0])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         else:
             short_id = None
             id = long(args[0])
         id_str = '#' + str(short_id) if short_id else str(id)
         try:
             self._api.create_favorite(id)
         except twitter.TwitterError, e:
             if 'No status found' in e.message:
                 return _('STATUS_NOT_FOUND') % id_str
             else:
                 return ''
         return _('FAVOURITED') % id_str
Exemplo n.º 2
0
 def func_reply(self, args):
   length = len(args)
   if not length or (length == 1 and (args[0][0].lower() == 'p' and args[0][1:].isdigit())):
     page = args[0][1:] if length else 1
     statuses = self._api.get_mentions(page=int(page))
     return _('MENTIONS') + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)
   elif length > 1 and (args[0].isdigit() or (args[0] and args[0][0] == '#' and args[0][1:].isdigit())\
   and ' '.join(args[1:])):
     if args[0][0] == '#':
       short_id = int(args[0][1:])
       id = utils.restore_short_id(short_id, self._google_user.jid)
     elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
       short_id = int(args[0])
       id = utils.restore_short_id(short_id, self._google_user.jid)
     else:
       short_id = None
       id = int(args[0])
     id_str = '#' + str(short_id) if short_id else str(id)
     try:
       status = self._api.get_status(id)
     except twitter.TwitterError, e:
       if 'No status found' in e.message:
         return _('STATUS_NOT_FOUND') % id_str
     message = u'@%s %s' % (status['user']['screen_name'], ' '.join(args[1:]))
     if len(message) > twitter.CHARACTER_LIMIT:
       return _('WORDS_COUNT_EXCEED') % (len(message), twitter.CHARACTER_LIMIT)
     try:
       self._api.post_update(message, id)
     except twitter.TwitterError, e:
       if 'Status is a duplicate' in e.message:
         return _('STATUS_DUPLICATE')
Exemplo n.º 3
0
 def func_msg(self, args):
     length = len(args)
     if length == 1 and (args[0].isdigit() or (args[0] and args[0][0] == '#'
                                               and args[0][1:].isdigit())):
         if args[0][0] == '#':
             short_id = int(args[0][1:])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
             short_id = int(args[0])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         else:
             short_id = None
             id = long(args[0])
         id_str = '#' + str(short_id) if short_id else str(id)
         statuses = list()
         for __ in xrange(config.MAX_REPLY_STATUS):
             try:
                 status = self._api.get_status(id)
             except twitter.TwitterError, e:
                 if 'No status found' in e.message:
                     if statuses:
                         break
                     else:
                         return _('STATUS_NOT_FOUND') % id_str
                 else:
                     return ''
             if status is None:
                 break
             statuses.append(status)
             if 'in_reply_to_status_id' in status:
                 id = status['in_reply_to_status_id']
             else:
                 break
         return _('STATUSES_CONVERSATION') + '\n\n' + utils.parse_statuses(
             statuses, reverse=False)
Exemplo n.º 4
0
 def func_fav(self, args):
     length = len(args)
     if not length or (length == 1 and (args[0][0].lower() == "p" and args[0][1:].isdigit())):
         page = args[0][1:] if length else 1
         statuses = self._api.get_favorites(page=int(page))
         return _("FAVOURITES") + _("PAGE") % str(page) + "\n\n" + utils.parse_statuses(statuses)
     elif length == 1 and (args[0].isdigit() or (args[0] and args[0][0] == "#" and args[0][1:].isdigit())):
         if args[0][0] == "#":
             short_id = int(args[0][1:])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
             short_id = int(args[0])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         else:
             short_id = None
             id = long(args[0])
         id_str = "#" + str(short_id) if short_id else str(id)
         try:
             self._api.create_favorite(id)
         except twitter.TwitterError, e:
             if "No status found" in e.message:
                 return _("STATUS_NOT_FOUND") % id_str
             else:
                 return ""
         return _("FAVOURITED") % id_str
Exemplo n.º 5
0
 def func_msg(self, args):
     length = len(args)
     if length == 1 and (args[0].isdigit() or (args[0] and args[0][0] == "#" and args[0][1:].isdigit())):
         if args[0][0] == "#":
             short_id = int(args[0][1:])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
             short_id = int(args[0])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         else:
             short_id = None
             id = long(args[0])
         id_str = "#" + str(short_id) if short_id else str(id)
         statuses = list()
         for __ in xrange(config.MAX_REPLY_STATUS):
             try:
                 status = self._api.get_status(id)
             except twitter.TwitterError, e:
                 if "No status found" in e.message:
                     if statuses:
                         break
                     else:
                         return _("STATUS_NOT_FOUND") % id_str
                 else:
                     return ""
             if status is None:
                 break
             statuses.append(status)
             if "in_reply_to_status_id" in status:
                 id = status["in_reply_to_status_id"]
             else:
                 break
         return _("STATUSES_CONVERSATION") + "\n\n" + utils.parse_statuses(statuses, reverse=False)
Exemplo n.º 6
0
 def func_home(self, args):
   length = len(args)
   if not length:
     page = 1
   elif length == 1 and args[0][0].lower() == 'p' and args[0][1:].isdigit():
     page = args[0][1:]
   else:
     raise NotImplementedError
   statuses = self._api.get_home_timeline(page=int(page))
   return _('TIMELINE') + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)
Exemplo n.º 7
0
 def func_home(self, args):
     length = len(args)
     if not length:
         page = 1
     elif length == 1 and args[0][0].lower() == "p" and args[0][1:].isdigit():
         page = args[0][1:]
     else:
         raise NotImplementedError
     try:
         statuses = self._api.get_home_timeline(page=int(page))
     except twitter.TwitterError:
         return ""
     if statuses is None:
         return None
     return _("TIMELINE") + _("PAGE") % str(page) + "\n\n" + utils.parse_statuses(statuses)
Exemplo n.º 8
0
 def func_home(self, args):
     length = len(args)
     if not length:
         page = 1
     elif length == 1 and args[0][0].lower() == 'p' and args[0][1:].isdigit(
     ):
         page = args[0][1:]
     else:
         raise NotImplementedError
     try:
         statuses = self._api.get_home_timeline(page=int(page))
     except twitter.TwitterError:
         return ''
     if statuses is None:
         return None
     return _('TIMELINE') + _('PAGE') % str(
         page) + '\n\n' + utils.parse_statuses(statuses)
Exemplo n.º 9
0
 def func_dm(self, args):
   length = len(args)
   if not length or (length == 1 and args[0][0].lower() == 'p' and args[0][1:].isdigit()):
     page = 1 if not length else args[0][1:]
     statuses = self._api.get_direct_messages(page=int(page))
     return _('DIRECT_MESSAGES') + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)
   elif length > 1 and ' '.join(args[1:]):
     message = ' '.join(args[1:])
     if len(message) > twitter.CHARACTER_LIMIT:
       return _('WORDS_COUNT_EXCEED') % (len(message), twitter.CHARACTER_LIMIT)
     try:
       self._api.post_direct_message(args[0], message)
     except twitter.TwitterError, e:
       if 'Not found' in e.message:
         return _('USER_NOT_FOUND') % args[0]
       elif 'You cannot send messages to users who are not following you' in e.message:
         return _('MESSAGE_TO_NOT_FOLLOWED')
     return _('SUCCESSFULLY_MESSAGE') % args[0]
Exemplo n.º 10
0
 def func_dm(self, args):
     length = len(args)
     if not length or (length == 1 and args[0][0].lower() == "p" and args[0][1:].isdigit()):
         page = 1 if not length else args[0][1:]
         statuses = self._api.get_direct_messages(page=int(page))
         return _("DIRECT_MESSAGES") + _("PAGE") % str(page) + "\n\n" + utils.parse_statuses(statuses)
     elif length > 1 and " ".join(args[1:]):
         message = " ".join(args[1:])
         if len(message) > twitter.CHARACTER_LIMIT:
             return _("WORDS_COUNT_EXCEED") % (len(message), twitter.CHARACTER_LIMIT)
         try:
             self._api.post_direct_message(args[0], message)
         except twitter.TwitterError, e:
             if "Not found" in e.message:
                 return _("USER_NOT_FOUND") % args[0]
             elif "You cannot send messages to users who are not following you" in e.message:
                 return _("MESSAGE_TO_NOT_FOLLOWED")
             else:
                 return ""
         return _("SUCCESSFULLY_MESSAGE") % args[0]
Exemplo n.º 11
0
 def func_reply(self, args):
     length = len(args)
     if not length or (length == 1 and (args[0][0].lower() == 'p'
                                        and args[0][1:].isdigit())):
         page = args[0][1:] if length else 1
         statuses = self._api.get_mentions(page=int(page))
         return _('MENTIONS') + _('PAGE') % str(
             page) + '\n\n' + utils.parse_statuses(statuses)
     elif length > 1 and (args[0].isdigit() or (args[0] and args[0][0] == '#' and args[0][1:].isdigit())\
     and ' '.join(args[1:])):
         if args[0][0] == '#':
             short_id = int(args[0][1:])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
             short_id = int(args[0])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         else:
             short_id = None
             id = int(args[0])
         id_str = '#' + str(short_id) if short_id else str(id)
         try:
             status = self._api.get_status(id)
         except twitter.TwitterError, e:
             if 'No status found' in e.message:
                 return _('STATUS_NOT_FOUND') % id_str
             else:
                 return ''
         if status is None:
             return ''
         message = u'@%s %s' % (status['user']['screen_name'], ' '.join(
             args[1:]))
         if len(message) > twitter.CHARACTER_LIMIT:
             return _('WORDS_COUNT_EXCEED') % (len(message),
                                               twitter.CHARACTER_LIMIT)
         try:
             self._api.post_update(message, id)
         except twitter.TwitterError, e:
             if 'Status is a duplicate' in e.message:
                 return _('STATUS_DUPLICATE')
             else:
                 return ''
Exemplo n.º 12
0
 def func_reply(self, args):
     length = len(args)
     if not length or (length == 1 and (args[0][0].lower() == "p" and args[0][1:].isdigit())):
         page = args[0][1:] if length else 1
         statuses = self._api.get_mentions(page=int(page))
         return _("MENTIONS") + _("PAGE") % str(page) + "\n\n" + utils.parse_statuses(statuses)
     elif length > 1 and (
         args[0].isdigit() or (args[0] and args[0][0] == "#" and args[0][1:].isdigit()) and " ".join(args[1:])
     ):
         if args[0][0] == "#":
             short_id = int(args[0][1:])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         elif int(args[0]) < config.MAX_SHORT_ID_LIST_NUM:
             short_id = int(args[0])
             id = utils.restore_short_id(short_id, self._google_user.jid)
         else:
             short_id = None
             id = int(args[0])
         id_str = "#" + str(short_id) if short_id else str(id)
         try:
             status = self._api.get_status(id)
         except twitter.TwitterError, e:
             if "No status found" in e.message:
                 return _("STATUS_NOT_FOUND") % id_str
             else:
                 return ""
         if status is None:
             return ""
         message = u"@%s %s" % (status["user"]["screen_name"], " ".join(args[1:]))
         if len(message) > twitter.CHARACTER_LIMIT:
             return _("WORDS_COUNT_EXCEED") % (len(message), twitter.CHARACTER_LIMIT)
         try:
             self._api.post_update(message, id)
         except twitter.TwitterError, e:
             if "Status is a duplicate" in e.message:
                 return _("STATUS_DUPLICATE")
             else:
                 return ""
Exemplo n.º 13
0
 def func_dm(self, args):
     length = len(args)
     if not length or (length == 1 and args[0][0].lower() == 'p'
                       and args[0][1:].isdigit()):
         page = 1 if not length else args[0][1:]
         statuses = self._api.get_direct_messages(page=int(page))
         return _('DIRECT_MESSAGES') + _('PAGE') % str(
             page) + '\n\n' + utils.parse_statuses(statuses)
     elif length > 1 and ' '.join(args[1:]):
         message = ' '.join(args[1:])
         if len(message) > twitter.CHARACTER_LIMIT:
             return _('WORDS_COUNT_EXCEED') % (len(message),
                                               twitter.CHARACTER_LIMIT)
         try:
             self._api.post_direct_message(args[0], message)
         except twitter.TwitterError, e:
             if 'Not found' in e.message:
                 return _('USER_NOT_FOUND') % args[0]
             elif 'You cannot send messages to users who are not following you' in e.message:
                 return _('MESSAGE_TO_NOT_FOLLOWED')
             else:
                 return ''
         return _('SUCCESSFULLY_MESSAGE') % args[0]
Exemplo n.º 14
0
        else:
            raise NotImplementedError
        try:
            statuses = self._api.get_user_timeline(screen_name=screen_name,
                                                   page=int(page))
        except twitter.TwitterError, e:
            if 'Not found' in e.message:
                return _('USER_NOT_FOUND') % screen_name
            elif 'Not authorized' in e.message:
                return _('PROTECTED_USER')
            else:
                return ''
        if statuses is None:
            return ''
        return _('USER_TIMELINE') % screen_name + _('PAGE') % str(
            page) + '\n\n' + utils.parse_statuses(statuses)

    def func_list(self, args):
        length = len(args)
        if length == 1 or (length == 2 and (args[1][0].lower() == 'p'
                                            and args[1][1:].isdigit())):
            path = args[0].split('/')
            if len(path) == 1:
                user = self._google_user.enabled_user
                list_id = path[0]
            elif len(path) == 2:
                user = path[0]
                list_id = path[1]
            else:
                raise NotImplementedError
            page = args[1][1:] if length == 2 else 1
Exemplo n.º 15
0
            screen_name = args[0]
            page = args[1][1:] if length == 2 else 1
        else:
            raise NotImplementedError
        try:
            statuses = self._api.get_user_timeline(screen_name=screen_name, page=int(page))
        except twitter.TwitterError, e:
            if "Not found" in e.message:
                return _("USER_NOT_FOUND") % screen_name
            elif "Not authorized" in e.message:
                return _("PROTECTED_USER")
            else:
                return ""
        if statuses is None:
            return ""
        return _("USER_TIMELINE") % screen_name + _("PAGE") % str(page) + "\n\n" + utils.parse_statuses(statuses)

    def func_list(self, args):
        length = len(args)
        if length == 1 or (length == 2 and (args[1][0].lower() == "p" and args[1][1:].isdigit())):
            path = args[0].split("/")
            if len(path) == 1:
                user = self._google_user.enabled_user
                list_id = path[0]
            elif len(path) == 2:
                user = path[0]
                list_id = path[1]
            else:
                raise NotImplementedError
            page = args[1][1:] if length == 2 else 1
        elif length == 2 or (length == 3 and (args[2][0].lower() == "p" and args[2][1:].isdigit())):
Exemplo n.º 16
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)
Exemplo n.º 17
0
class cron_handler(webapp.RequestHandler):
    def get(self, cron_id):
        cron_id = int(cron_id)
        data = Session.get_all(shard=cron_id)
        for u in data:
            jid = u.key().name()
            try:
                self.process(u)
            except CapabilityDisabledError:
                try:
                    xmpp.send_presence(jid,
                                       presence_show=xmpp.PRESENCE_SHOW_AWAY)
                except xmpp.Error:
                    pass
            else:
                try:
                    xmpp.send_presence(jid)
                except xmpp.Error:
                    pass

    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())
        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)
                while CapabilitySet('xmpp').is_enabled():
                    try:
                        xmpp.send_message(google_user.jid, content)
                    except xmpp.Error:
                        pass
                    else:
                        break
        if google_user.display_timeline & MODE_DM:
            try:
                statuses = api._process_result(dm_rpc)
                content = utils.parse_statuses(statuses)
                if content.strip():
                    while CapabilitySet('xmpp').is_enabled():
                        try:
                            xmpp.send_message(
                                google_user.jid,
                                _('DIRECT_MESSAGES') + '\n\n' + content)
                        except xmpp.Error:
                            pass
                        else:
                            break
                    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)
Exemplo n.º 18
0
    if not length or (length == 1 and (args[0][0].lower() == 'p' and args[0][1:].isdigit())):
      screen_name = self._user['screen_name']
      page = args[0][1:] if length else 1
    elif length == 1 or (length == 2 and (args[1][0].lower() == 'p' and args[1][1:].isdigit())):
      screen_name = args[0]
      page = args[1][1:] if length == 2 else 1
    else:
      raise NotImplementedError
    try:
      statuses = self._api.get_user_timeline(screen_name=screen_name, page=int(page))
    except twitter.TwitterError, e:
      if 'Not found' in e.message:
        return _('USER_NOT_FOUND') % screen_name
      elif 'Not authorized' in e.message:
        return _('PROTECTED_USER')
    return _('USER_TIMELINE') % screen_name + _('PAGE') % str(page) + '\n\n' + utils.parse_statuses(statuses)

  def func_list(self, args):
    length = len(args)
    if length == 1 or (length == 2 and (args[1][0].lower() == 'p' and args[1][1:].isdigit())):
      path = args[0].split('/')
      if len(path) == 1:
        user = self._google_user.enabled_user
        list_id = path[0]
      elif len(path) == 2:
        user = path[0]
        list_id = path[1]
      else:
        raise NotImplementedError
      page = args[1][1:] if length == 2 else 1
    elif length == 2 or (length == 3 and (args[2][0].lower() == 'p' and args[2][1:].isdigit())):