From 4992631aef19d7f644cbcd6cd5e37dde37c6e149 Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期一, 14 七月 2025 10:46:10 +0800 Subject: [PATCH] add:增加对外接口 --- app.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 55 insertions(+), 0 deletions(-) diff --git a/app.py b/app.py index 872cff9..cb75612 100644 --- a/app.py +++ b/app.py @@ -10,6 +10,11 @@ from werkzeug.exceptions import BadRequest from ner_config import NERConfig, RepaymentNERConfig, IncomeNERConfig, FlightNERConfig, TrainNERConfig import re +import threading +import subprocess + +from test2 import process_films + # 配置日志 logging.basicConfig( @@ -1681,5 +1686,55 @@ "message": "服务器内部错误" }), 500 + +def run_douban_spider(type_, tag): + subprocess.run( + ["python", "douban.py", type_, tag], + capture_output=True, + text=True + ) + +def run_process_films(): + try: + process_films() + except Exception as e: + logger.error(f"处理电影内容失败: {str(e)}") + +@app.route("/crawl-douban", methods=["POST"]) +def crawl_douban(): + try: + default_params = {"type": "tv", "tag": "热门"} + + # 尝试解析 JSON,失败则使用默认值 + # 先检查 Content-Type 和数据结构 + if request.is_json: + params = request.get_json() + if not isinstance(params, dict): # 确保是字典类型 + params = default_params + else: + params = default_params + + # 获取参数(带默认值) + type_ = params.get("type", default_params["type"]) + tag = params.get("tag", default_params["tag"]) + # 打印接收到的参数(调试用) + print(f"Received params - type: {type_}, tag: {tag}") + + # 启动线程异步执行爬虫 + t = threading.Thread(target=run_douban_spider, args=(type_, tag)) + t.start() + + return jsonify({"status": "success", "message": "爬虫任务已启动", "type": type_, "tag": tag}) + except Exception as e: + return jsonify({"status": "error", "message": str(e)}), 500 + +@app.route('/generate-film-content', methods=['POST']) +def generate_film_content_api(): + """生成电影内容接口""" + try: + threading.Thread(target=run_process_films).start() + return jsonify({"status": "accepted", "message": "任务已异步启动"}) + except Exception as e: + return jsonify({"status": "error", "message": str(e)}), 500 if __name__ == "__main__": app.run(host="0.0.0.0", port=5000) \ No newline at end of file -- Gitblit v1.9.3