| | |
| | | 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( |
| | |
| | | "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) |