Esempio n. 1
0
def update_article_status(article_id, valid, backup_path=None):
    # 做点准备工作
    db = DbOperator()
    success, item = db.find_article(article_id)
    if not success:
        # log it
        print("can't find article by article_id: " + str(article_id))
        logger.error("can't find article by article_id: " + str(article_id))
        return False
    URL = item['URL']
    open_id = item['open_id']
    success, result = db.find_user(open_id)
    if not success:
        logger.error("article & user no match: " +
                     " ".join(map(str, [article_id, open_id])))
        return False
    email = result['email']

    if not valid:  # 当文章已不可访问
        if item['backup_addr'] == FAKE_PATH_PLACE_HOLDER:
            backup_addr = None
        else:
            backup_addr = item['backup_addr']
        notify_user(email, URL, backup_addr)
        db.archive_article(item)
        logger.info("article expired, move to archive: " +
                    " ".join(map(str, [article_id, URL])))
    else:
        prev_status = item['status']
        if prev_status == 1:  # 正常观察状态,无需额外操作
            return True
        elif prev_status == 0:  # 初次完成观察,制作备份
            if backup_path == None:
                print("backup addr not valid")
                return False
            else:
                new_path = _backup_article(article_id, backup_path)
                if new_path == None:
                    return False
                article_info = (article_id, new_path, 1)  # 制作备份完成,状态更新成1
                return db.update_article(article_info)
        else:
            print("unknown status!")
            logger.error(
                "article status Error: " +
                " ".join(map(str, [article_id, open_id, prev_status])))

    return True
Esempio n. 2
0
def update_article_status(article_id, valid, backup_path=None, optionals={}):
    """Do jobs when article status changed.

    Args:
        article_id : int
        valid : bool
        backup_path : str
        optionals : dict 
            maybe contains: "reason" shows why status changed
                            "title" shows article title detected
    Returns:
        success: bool
    """
    # 做点准备工作
    db = DbOperator()
    success, item = db.find_article(article_id)
    if not success:
        # log it
        print("can't find article by article_id: " + str(article_id))
        logger.error("can't find article by article_id: " + str(article_id))
        return False
    URL = item['URL']
    open_id = item['open_id']
    success, result = db.find_user(open_id)
    if not success:
        logger.error("article & user no match: "
                     + " ".join(map(str, [article_id, open_id])))
        return False
    email = result['email']
    # 开始更新数据库
    if not valid:  # 当文章已不可访问
        if item['backup_addr'] == FAKE_PATH_PLACE_HOLDER:
            backup_addr = None
        else:
            backup_addr = item['backup_addr']
        try:
            notify_user(email, URL, backup_addr)
            item['status'] = reason2status[optionals["reason"]]
            db.archive_article(item)
        except:
            logger.error("update_article_status ERROR")
            return False
        logger.info("article expired, move to archive: "
                    + " ".join(map(str, [article_id, URL])))
    else:
        prev_status = item['status']
        if prev_status == STATUS_NORMAL_OB:  # 正常观察状态,无需额外操作
            return True
        elif prev_status == STATUS_NEW_UNKNOWN:  # 初次完成观察,制作备份
            if backup_path == None:
                print("backup addr not valid")
                return False
            else:
                new_path = _backup_article(article_id, backup_path)
                if new_path == None:
                    return False
                article_info = (article_id, new_path, STATUS_NORMAL_OB) # 制作备份完成,状态更新
                return db.update_article(article_info)
        else:
            print("unknown status!")
            logger.error("article status Error: "
                         + " ".join(map(str, [article_id, open_id, prev_status])))

    return True