""" @author: luojunhui """ import json import time import uuid import asyncio from quart import Blueprint, jsonify, request from applications.functions.log import logging from applications.schedule import recall_videos, search_videos my_blueprint = Blueprint('LongArticles', __name__) def Routes(mysql_client): """ 路由代码 """ @my_blueprint.route('/healthcheck') def healthcheck(): """ Hello World Test :return: """ logging( code="1001", info="请求接口成功", port="healthcheck" ) return jsonify({'message': 'Hello, World!'}) @my_blueprint.route('/search_videos', methods=['POST']) async def search_videos_from_the_web(): """ 从web 搜索视频并且存储到票圈的视频库中 :return: """ params = await request.get_json() gh_id = params['ghId'] trace_id = "search-{}-{}".format(str(uuid.uuid4()), str(int(time.time()))) params['trace_id'] = trace_id title = params['title'].split("@@")[-1].replace("'", "") contents = params['content'].replace("'", "") account_name = params['accountName'].replace("'", "") logging( code="2000", info="搜索视频内容接口请求成功", port="title_to_search", function="search_videos_from_the_web", trace_id=trace_id ) insert_sql = f""" INSERT INTO long_articles_video_dev (trace_id, gh_id, article_title, article_text, account_name) VALUES ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}');""" await mysql_client.async_insert(insert_sql) try: asyncio.ensure_future( search_videos( params=params, trace_id=trace_id, gh_id=gh_id, mysql_client=mysql_client ) ) res = { "status": "success", "code": 0, "traceId": trace_id } except Exception as e: res = { "status": "fail", "code": 1, "message": str(e) } return jsonify(res) @my_blueprint.route('/recall_videos', methods=['POST']) async def find_videos(): """ 请求接口代码 :return: """ data = await request.get_json() trace_id = data['traceId'] logging( code="1001", info="请求接口成功", port="recall_videos", trace_id=trace_id ) try: result = await recall_videos( trace_id=trace_id, mysql_client=mysql_client ) except Exception as e: result = { "traceId": trace_id, "error": e } return jsonify(result) return my_blueprint