""" @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, return_info_v2 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() try: 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 ) except Exception as e: result = { "status": "fail", "code": 1, "message": str(e), "info": "params check error" } return jsonify(result) if "video_id=" in title: video_id = title.split("video_id=")[-1] insert_sql = f""" INSERT INTO long_articles_video (trace_id, gh_id, article_title, article_text, account_name, recall_video_id1) VALUES ('{trace_id}', '{gh_id}', '{title}', '{contents}', '{account_name}', '{video_id}');""" await mysql_client.async_insert(insert_sql) res = { "status": "success", "code": 0, "traceId": trace_id } return jsonify(res) else: insert_sql = f""" INSERT INTO long_articles_video (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 ) print(result) except Exception as e: result = { "traceId": trace_id, "error": str(e) } print(result) return jsonify(result) @my_blueprint.route('/re_search_videos', methods=['POST']) async def re_search_videos(): """ 重新搜索 :return: """ params = await request.get_json() gh_id = params['ghId'] trace_id = params['trace_id'] logging( code="2000", info="重新搜索视频内容接口请求成功", port="title_to_search", function="re_search_videos_from_the_web", trace_id=trace_id ) 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) return my_blueprint