Example #1
0
def token_valid():
    """验证jwt是否有效
    eg:
    {
        "username": "******"
    }
    Returns:
        Response: 响应类
    """
    return response_handle(request=request, dict_value=UniResponse.SUCCESS)
Example #2
0
def source_list():
    """获取所有文档源统计信息
    eg:
    {
        "username": "******"
    }
    Returns:
        Response: 响应类
    """
    mongodb_base: MongodbBase = current_app.config["mongodb_base"]
    app_logger: LOGGER = current_app.config["app_logger"]
    coll = mongodb_base.get_collection(coll_name="liuli_articles")
    try:
        doc_source_list = coll.distinct("doc_source")
        doc_source_dict = {}
        for doc_source in doc_source_list:
            pipeline = [
                {
                    "$match": {
                        "doc_source": doc_source
                    }
                },
                {
                    "$group": {
                        "_id": "$doc_source_name",
                        "count": {
                            "$sum": 1
                        }
                    }
                },
            ]
            doc_source_dict[doc_source] = {
                "doc_count": 0,
                "doc_source_list": [],
                "doc_source_details": [],
            }
            for item in coll.aggregate(pipeline):
                doc_source_list: list = doc_source_dict[doc_source][
                    "doc_source_list"]
                doc_source_list.append(item["_id"])
                doc_source_details: list = doc_source_dict[doc_source][
                    "doc_source_details"]
                doc_source_details.append(item)
                doc_source_dict[doc_source]["doc_count"] += item["count"]
        result = {
            ResponseField.DATA: doc_source_dict,
            ResponseField.MESSAGE: ResponseReply.SUCCESS,
            ResponseField.STATUS: ResponseCode.SUCCESS,
        }
    except Exception as e:
        result = UniResponse.DB_ERR
        err_info = f"query doc source failed! DB response info -> {e}"
        app_logger.error(err_info)
    return response_handle(request=request, dict_value=result)
Example #3
0
def login():
    """用户登录接口
    eg:
    {
        "username": "******",
        "password": "******"
    }
    Token Demo:
    "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTYyNzc1MDQ1OCwianRpIjoiNzJjZjZkYzYtZDE5NS00NGRhLTg2NWUtNmNhZmY3MTdkMjMwIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6MTU3Njc5NTY4OTAsIm5iZiI6MTYyNzc1MDQ1OH0.xwUuyTYoXFIymE6RqnEuuteyFbYiMmY72YYtIUMfqNY"
    Returns:
        Response: Flask响应类
    """
    # 获取基本配置
    mongodb_base: MongodbBase = current_app.config["mongodb_base"]
    app_logger: LOGGER = current_app.config["app_logger"]
    coll = mongodb_base.get_collection(coll_name="liuli_user")
    # 获取基础数据
    post_data: dict = request.json
    username = post_data.get("username", "")
    password = post_data.get("password", "")
    user_db_res = mongodb_find(
        coll_conn=coll,
        filter_dict={
            "username": username,
            "password": md5_encryption(password)
        },
        return_dict={"_id": 0},
    )
    user_info_list = user_db_res["info"]
    if username and password and user_db_res["status"] and len(
            user_info_list) == 1:
        # 半年过期一次 259200
        expires_delta = datetime.timedelta(minutes=259200)
        access_token = create_access_token(identity=username,
                                           expires_delta=expires_delta)
        result = {
            ResponseField.DATA: {
                "token": access_token,
                "username": username
            },
            ResponseField.MESSAGE: ResponseReply.SUCCESS,
            ResponseField.STATUS: ResponseCode.SUCCESS,
        }
    else:
        result = {
            ResponseField.DATA: {},
            ResponseField.MESSAGE: ResponseReply.USER_LOGIN_ERROR,
            ResponseField.STATUS: ResponseCode.USER_LOGIN_ERROR,
        }
        err_info = f"login failed! DB response info -> {user_db_res}"
        app_logger.error(err_info)

    return response_handle(request=request, dict_value=result)
Example #4
0
def rss_list():
    """获取用户下所有rss链接地址
    eg:
    {
        "username": "******",
        "doc_source": "",
    }
    Returns:
        Response: 响应类
    """
    mongodb_base: MongodbBase = current_app.config["mongodb_base"]
    app_logger: LOGGER = current_app.config["app_logger"]
    app_config: Config = current_app.config["app_config"]
    coll = mongodb_base.get_collection(coll_name="liuli_rss")
    # 获取基础数据
    post_data: dict = request.json
    doc_source = post_data.get("doc_source", "")
    filter_dict = {"doc_source": doc_source} if doc_source else {}
    return_dict = {
        "_id": 0,
        "doc_source": 1,
        "doc_source_name": 1,
        "updated_at": 1
    }
    domain: str = app_config.DOMAIN or f"http://{get_ip()}:{Config.HTTP_PORT}"

    try:
        cursor = coll.find(filter_dict, return_dict).sort("updated_at", 1)
        rss_dict = []
        for document in cursor:
            updated_at = document["updated_at"]
            doc_source = document["doc_source"]
            doc_source_name = document["doc_source_name"]
            rss_dict.append({
                **document,
                **{
                    "updated_at":
                    ts_to_str_date(updated_at),
                    "rss_url":
                    urljoin(domain, f"rss/{doc_source}/{doc_source_name}"),
                },
            })
        result = {
            ResponseField.DATA: rss_dict,
            ResponseField.MESSAGE: ResponseReply.SUCCESS,
            ResponseField.STATUS: ResponseCode.SUCCESS,
        }
    except Exception as e:
        result = UniResponse.DB_ERR
        err_info = f"query doc RSS failed! DB response info -> {e}"
        app_logger.error(err_info)

    return response_handle(request=request, dict_value=result)
Example #5
0
def book_chapter():
    """
    返回书籍目录json,依赖参数:
        - url: 书籍目录链接
    eg: http://0.0.0.0:8765/v1/utils/book_chapter?url=https://www.yruan.com/article/38563.html
    """
    args = request.args.to_dict()
    url = args.get("url", "")
    chapter_list = []
    result = UniResponse.SUCCESS
    if url:
        # 目录链接必须存在
        resp_text = get_html_by_requests(
            url, headers={"User-Agent": Config.SPIDER_UA})
        chapter_list = extract_chapters(url, resp_text)
    else:
        result = UniResponse.PARAM_ERR

    result[ResponseField.DATA] = {
        "url": url,
        "chapter_list": chapter_list,
    }
    return response_handle(request=request, dict_value=result)
Example #6
0
def book_content():
    """
    基于readability算法提取文章核心内容,并转化为MD格式输出
    返回书籍目录json,依赖参数:
        - url: 书籍章节页链接
    eg: http://0.0.0.0:8765/v1/utils/book_content?url=https://www.yruan.com/article/38563/4082438.html
    """
    args = request.args.to_dict()
    url = args.get("url", "")
    result = UniResponse.SUCCESS
    core_html = ""
    if url:
        # 章节链接必须存在
        resp_text = get_html_by_requests(
            url, headers={"User-Agent": Config.SPIDER_UA})
        _, core_html = extract_core_html(resp_text)
    else:
        result = UniResponse.PARAM_ERR

    result[ResponseField.DATA] = {
        "url": url,
        "core_html": core_html,
    }
    return response_handle(request=request, dict_value=result)
Example #7
0
def change_pwd():
    """修改密码
    eg:
    {
        "username": "******",
        "o_password": "******",
        "n_password": "******"
    }
    Returns:
        Response: 响应类
    """
    # 获取基本配置
    mongodb_base: MongodbBase = current_app.config["mongodb_base"]
    app_logger: LOGGER = current_app.config["app_logger"]
    coll = mongodb_base.get_collection(coll_name="liuli_user")
    # 获取基础数据
    post_data: dict = request.json
    username = post_data.get("username") or ""
    o_password = post_data.get("o_password") or ""
    n_password = post_data.get("n_password") or ""
    user_db_res = mongodb_find(
        coll_conn=coll,
        filter_dict={
            "username": username,
            "password": md5_encryption(o_password)
        },
        return_dict={"_id": 0},
    )
    user_info_list = user_db_res["info"]
    if username and n_password and user_db_res["status"] and len(
            user_info_list) == 1:
        # 历史用户存在
        db_res = mongodb_update_data(
            coll_conn=coll,
            filter_dict={"username": username},
            update_data={
                "$set": {
                    "password": md5_encryption(n_password),
                    "updated_at": int(time.time()),
                }
            },
        )
        if db_res["status"]:
            result = {
                ResponseField.DATA: {
                    "username": username
                },
                ResponseField.MESSAGE: ResponseReply.SUCCESS,
                ResponseField.STATUS: ResponseCode.SUCCESS,
            }

        else:
            result = UniResponse.CHANGE_PWD_ERROR
            err_info = f"change user pwd failed! DB response info -> {db_res['info']}"
            app_logger.error(err_info)

    else:
        result = UniResponse.CHANGE_PWD_ERROR
        err_info = f"change user pwd failed! DB response info -> {user_db_res}"
        app_logger.error(err_info)

    return response_handle(request=request, dict_value=result)
Example #8
0
def articles():
    """查询历史文章
    {
        "username": "******",
        "doc_source": "liuli_wechat",
        "doc_source_name": "",
        "size": 10,
        "page": 1,
        "sorted_order": 1,

    }

    Returns:
        Response: 响应类
    """
    mongodb_base: MongodbBase = current_app.config["mongodb_base"]
    app_logger: LOGGER = current_app.config["app_logger"]
    app_config: Config = current_app.config["app_config"]
    coll = mongodb_base.get_collection(coll_name="liuli_articles")
    # 获取基础数据
    post_data: dict = request.json
    doc_source = post_data.get("doc_source", "")
    doc_source_name = post_data.get("doc_source_name", "")
    size = post_data.get("size", 10)
    page = post_data.get("page", 1)
    # 默认从小到大
    sorted_order = post_data.get("sorted_order", 1)
    filter_dict = {"doc_source": doc_source} if doc_source else {}
    if doc_source_name:
        filter_dict.update({"doc_source_name": doc_source_name})
    db_res = mongodb_find_by_page(
        coll_conn=coll,
        filter_dict=filter_dict,
        size=size,
        page=page,
        return_dict={
            "_id": 1,
            "doc_source": 1,
            "doc_source_name": 1,
            "doc_ts": 1,
            "doc_name": 1,
        },
        sorted_key="doc_ts",
        sorted_order=sorted_order,
    )
    db_info = db_res["info"]
    if db_res["status"]:
        # 对于 _id 做强制 str 处理
        return json.dumps(
            {
                ResponseField.DATA: {
                    **db_info,
                    **{
                        "size": size,
                        "page": page
                    }
                },
                ResponseField.MESSAGE: ResponseReply.SUCCESS,
                ResponseField.STATUS: ResponseCode.SUCCESS,
            },
            default=str,
        )
    else:
        result = UniResponse.DB_ERR
        err_info = f"query doc articles failed! DB response info -> {db_info}"
        app_logger.error(err_info)

        return response_handle(request=request, dict_value=result)