""" @author: luojunhui """ import os import time import uuid import asyncio from quart import Blueprint, jsonify, request from applications.log import logging from applications.process import ProcessParams from applications.search import search_videos from applications.functions.common import find_videos_in_mysql, ask_kimi_and_save_to_local my_blueprint = Blueprint('kimi', __name__) @my_blueprint.route('/healthcheck') def hello(): """ Hello World Test :return: """ logging( code="1001", info="请求接口成功", port="healthcheck" ) return jsonify({'message': 'Hello, World!'}) @my_blueprint.route('/title_to_search', methods=['POST']) async def search_videos_from_the_web(): """ 从web 搜索视频并且存储到票圈的视频库中 :return: """ params = await request.get_json() title = params['title'] gh_id = params['ghId'] trace_id = "search-{}-{}".format(str(uuid.uuid4()), str(int(time.time()))) params['trace_id'] = trace_id logging( code="2000", info="搜索视频内容接口请求成功", port="title_to_search", function="search_videos_from_the_web", trace_id=trace_id ) try: title_p = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title)) if os.path.exists(title_p): logging( code="2001", info="该标题已经被 kimi 处理过,跳过请求 kimi 操作--- {}".format(title), function="search_videos_from_the_web", trace_id=trace_id ) else: ask_kimi_and_save_to_local((title, trace_id, title_p)) await asyncio.sleep(2) search_videos( title=title, video_path=title_p, trace_id=trace_id, gh_id=gh_id, ) res = { "trace_id": trace_id, "code": 0 } except Exception as e: res = { "trace_id": trace_id, "code": 1, "message": str(e) } return jsonify(res) @my_blueprint.route('/out_videos', methods=['POST']) async def find_in_mysql(): """ 搜索是否存在外站视频 video_list, 如果存在,则返回成功 :return: """ data = await request.get_json() trace_id = data['traceId'] logging( code="2000", info="请求接口成功", port="title_to_video", trace_id=trace_id, function="find_in_mysql" ) res = find_videos_in_mysql(trace_id=trace_id) return jsonify(res) @my_blueprint.route('/title_to_video', methods=['POST']) async def post_data(): """ 请求接口代码 :return: """ data = await request.get_json() trace_id = data['traceId'] logging( code="1001", info="请求接口成功", port="title_to_video", trace_id=trace_id ) p = ProcessParams(t_id=trace_id) processed_data = await p.deal(data) return jsonify(processed_data)