scoring.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:naziscore 作者: rbanffy 项目源码 文件源码
def get_score_by_twitter_id(twitter_id, depth):
    try:
        score = yield Score.query(Score.twitter_id == twitter_id).get_async()
    except OverQuotaError:
        logging.critical(
            'Over quota fetching {}'.format(twitter_id))
        raise ndb.Return(None)
    if score is None or (
            score.last_updated < datetime.datetime.now()
            - datetime.timedelta(days=MAX_AGE_DAYS)):
            # If we don't have one, or if we have one that's too old, we need
            # to calculate one.
        task_name = '{}_{}'.format(
                twitter_id,
                os.environ['CURRENT_VERSION_ID'].split('.')[0])
        queue_name = 'scoring-direct' if depth == 0 else 'scoring-indirect'
        try:
            _ = yield taskqueue.Task(
                name=task_name,
                params={
                    'twitter_id': twitter_id,
                    'depth': depth
                }).add_async(queue_name)

            # If this is a direct query, schedule an analysis of the profile
            # picture.
            if depth == 0:
                _ = yield taskqueue.Task(
                    name=task_name,
                    params={
                        'twitter_id': twitter_id,
                    }).add_async('profile-pic')

            # If we add it to the scoring-direct queue, we should remove
            # the corresponding task from the scoring-indirect queue at this
            # point.
            if queue_name == 'scoring-direct':
                delete_from_scoring_indirect(task_name)

        except taskqueue.TaskAlreadyExistsError:
            # We already are going to check this person. There is nothing
            # to do here.
            logging.warning(
                'Fetch for {} already scheduled on queue {}'.format(
                    task_name, queue_name))

        except taskqueue.TombstonedTaskError:
            # This task is too recent. We shouldn't try again so
            # soon. Thombstoning won't happen across different deploys, as the
            # task name has the deploy timestamp on it.
            logging.warning('Fetch for {} tombstoned'.format(task_name))
        raise ndb.Return(score)
    else:
        raise ndb.Return(score)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号