当前位置: 首页>>代码示例>>Python>>正文


Python Db.set_cache方法代码示例

本文整理汇总了Python中db.Db.set_cache方法的典型用法代码示例。如果您正苦于以下问题:Python Db.set_cache方法的具体用法?Python Db.set_cache怎么用?Python Db.set_cache使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在db.Db的用法示例。


在下文中一共展示了Db.set_cache方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: parse_status

# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import set_cache [as 别名]
def parse_status(status):
  if 'retweeted_status' in status and _user.official_retweet:
    status = status['retweeted_status']
  msg_dict = {'content': unescape(status['text']), 'id': str(status['id'])}
  if 'user' in status:
    msg_dict['username'] = status['user']['screen_name']
    Db.set_cache(status)
  elif 'sender' in status:
    msg_dict['username'] = status['sender_screen_name']
  else:
    msg_dict['username'] = ''
  if msg_dict['username'] and _user.bold_username:
    msg_dict['username'] = '*%s*' % msg_dict['username']
  username = _user.enabled_user
  username_at = "@" + username
  short_id = None
  if username_at in msg_dict['content']:
    if _user.bold_username:
      msg_dict['content'] = msg_dict['content'].replace(username_at, '*%s*' % username_at)
  if 'user' in status:
    short_id = generate_short_id(status['id'])
  msg_dict['shortid'] = '#' + str(short_id) if short_id is not None else ''
  utc = pytz.utc
  t = parsedate(status['created_at'])[:6]
  t = datetime(*t)
  utc_dt = utc.localize(t)
  tz = pytz.timezone(_user.timezone)
  t = tz.normalize(utc_dt.astimezone(tz))
  msg_dict['time'] = t.strftime(_user.date_format.encode('UTF-8')).decode('UTF-8')
  if 'source' in status:
    source = re.match(r'<a .*>(.*)</a>', status['source'])
    msg_dict['source'] = source.group(1) if source else status['source']
  else:
    msg_dict['source'] = ''
  return Template(unicode(_user.msg_template)).safe_substitute(msg_dict)
开发者ID:foogle,项目名称:twitalkerplus,代码行数:37,代码来源:utils.py

示例2: post_update

# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import set_cache [as 别名]
 def post_update(self, status, in_reply_to_status_id=None):
   url = '%s/statuses/update.json' % self.base_url
   data = {'status': status}
   if in_reply_to_status_id:
     data['in_reply_to_status_id'] = in_reply_to_status_id
   data = self._fetch_url(url, post_data=data)
   Db.set_cache(data)
   return data
开发者ID:denyany,项目名称:twitalkerplus,代码行数:10,代码来源:twitter.py

示例3: get

# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import set_cache [as 别名]
  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...")
开发者ID:foogle,项目名称:twitalkerplus,代码行数:55,代码来源:cron.py

示例4: parse_status

# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import set_cache [as 别名]
 def parse_status(self, status):
   if status is None:
     return None
   if 'retweeted_status' in status and self._user.official_retweet:
     status = status['retweeted_status']
   short_id, short_id_str = self.generate_short_id(status['id'])
   if not self.allow_duplicate and short_id is None:
     return None
   msg_dict = {'content': Utils.parse_text(status['text']),
               'id': str(status['id'])}
   if 'user' in status:
     msg_dict['username'] = status['user']['screen_name']
     msg_dict['name'] = status['user']['name']
   elif 'sender' in status:
     msg_dict['username'] = status['sender']['screen_name']
     msg_dict['name'] = status['sender']['name']
   else:
     msg_dict['username'] = ''
   Db.set_cache(status)
   if msg_dict['username'] and self._user.bold_username:
     msg_dict['username'] = '*%s*' % msg_dict['username']
   username = self._user.enabled_user
   username_at = "@" + username
   if username_at in msg_dict['content']:
     if self._user.bold_username:
       msg_dict['content'] = msg_dict['content'].replace(username_at, '*%s*' % username_at)
   msg_dict['shortid'] = '#%d=%s' % (short_id, short_id_str) if short_id is not None else ''
   utc = pytz.utc
   t = parsedate(status['created_at'])[:6]
   t = datetime(*t)
   utc_dt = utc.localize(t)
   tz = pytz.timezone(self._user.timezone)
   t = tz.normalize(utc_dt.astimezone(tz))
   msg_dict['time'] = t.strftime(self._user.date_format.encode('UTF-8')).decode('UTF-8')
   if 'source' in status:
     source = re.match(r'<a .*>(.*)</a>', status['source'])
     msg_dict['source'] = source.group(1) if source else status['source']
   else:
     msg_dict['source'] = ''
   return Template(unicode(self._user.msg_template)).safe_substitute(msg_dict)
开发者ID:denyany,项目名称:twitalkerplus,代码行数:42,代码来源:utils.py

示例5: post

# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import set_cache [as 别名]
  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
#.........这里部分代码省略.........
开发者ID:foogle,项目名称:twitalkerplus,代码行数:103,代码来源:worker.py

示例6: process

# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import set_cache [as 别名]
  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:
#.........这里部分代码省略.........
开发者ID:galuano1,项目名称:twitalkerplus,代码行数:103,代码来源:cron.py

示例7: process

# 需要导入模块: from db import Db [as 别名]
# 或者: from db.Db import set_cache [as 别名]
  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())
开发者ID:denyany,项目名称:twitalkerplus,代码行数:81,代码来源:cron.py


注:本文中的db.Db.set_cache方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。