m3u8_download/m3u8_ui.py
2025-03-16 19:03:36 +08:00

86 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from flask import Flask, render_template, request, jsonify, send_file, current_app
from flask_cors import CORS # 导入 Flask-CORS 扩展
from utils.Log import Log
from utils.Db_Execute import get_movie_list,movie_options,movie_add
from werkzeug.exceptions import BadRequest
app = Flask(__name__)
log = Log()
# 允许所有源访问接口,可以根据需要进行更细粒度的控制
CORS(app)
@app.route('/')
def index():
return send_file('templates/index.html')
@app.route('/movie_list', methods=['GET'])
def convert_sql():
try:
# 调用 get_movie_list() 获取电影列表
movie_list = get_movie_list()
# 如果列表为空,返回空数组
if not movie_list:
return jsonify([]), 200
# 正常返回电影列表
return jsonify(movie_list), 200
except Exception as e:
# 捕获异常并返回错误信息
log.error(f"Error in fetching movie list: {str(e)}", exc_info=True) # 添加 exc_info=True 以便记录完整的堆栈跟踪
return jsonify({"error": "Failed to fetch movie list"}), 500
@app.route('/movie/<int:movie_id>', methods=['GET', 'PUT'])
def movie_markers(movie_id):
try:
if request.method == 'GET':
# 获取当前状态逻辑(示例)
current_status = movie_options(movie_id) # 替换为实际数据库查询
return jsonify({"status": current_status}), 200
elif request.method == 'PUT':
# 更新状态逻辑(示例)
movie_options(movie_id) # 替换为实际数据库操作
return jsonify(success=True), 200
else:
return jsonify({"message": "无效的请求方法"}), 400
except Exception as e:
return jsonify({"message": "操作失败", "error": str(e)}), 500
@app.route('/add_movie', methods=['POST'])
def add_movie():
try:
# 修改点1获取JSON格式数据
data = request.get_json()
if not data:
raise BadRequest("请求体必须为JSON格式")
# 修改点2使用正确的字段名
movie_name = data.get('name')
movie_path = data.get('path')
# 输入验证(字段名同步修改)
if not movie_name or not movie_path:
raise BadRequest("缺少必要的参数: name 或 path")
if len(movie_name) > 255 or len(movie_path) > 255:
raise BadRequest("参数过长: name 或 path 超过255字符")
# 数据库操作(保持原逻辑)
movie_add(movie_name, movie_path)
return jsonify(success=True), 200
except BadRequest as e:
return jsonify({"message": e.description}), 400
except Exception as e:
return jsonify({"message": "操作失败", "error": str(e)}), 500
if __name__ == '__main__':
# 指定host和port这里使用0.0.0.0可以让服务器被外部访问
app.run(host='0.0.0.0', port=8778, debug=True)