discord_intro_quiz_bot/web/handlers.py

162 lines
5.6 KiB
Python
Raw Normal View History

2024-08-03 10:29:46 +00:00
import os
import urllib.parse
from flask import request, jsonify, session, render_template, current_app
import requests
from db import get_db
from models import ChannelId, QuizMaster
def handle_file_upload():
if 'files' not in request.files:
return jsonify({'error': 'ファイルが見つかりません'})
files = request.files.getlist('files')
if not files or files[0].filename == '':
return jsonify({'error': '選択されたファイルがありません'})
for file in files:
filename = file.filename
file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
return jsonify({'message': 'ファイルがアップロードされました'})
def delete_file(filename):
filename = urllib.parse.unquote(filename)
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
if os.path.exists(file_path):
os.remove(file_path)
return jsonify({'message': 'ファイルが削除されました'})
return jsonify({'error': 'ファイルが見つかりません'})
def play_file(filename, volume):
filename = urllib.parse.unquote(filename)
response = requests.post(f"{current_app.config['BOT_API_URL']}/play", json={'filename': filename, 'volume': volume})
return jsonify(response.json())
def join_channel():
channel_id = request.json.get('channel_id')
db = get_db()
new_channel = ChannelId(channel_id=channel_id)
db.add(new_channel)
db.commit()
response = requests.post(f"{current_app.config['BOT_API_URL']}/join", json={'channel_id': channel_id})
return jsonify(response.json())
def leave_channel():
response = requests.post(f"{current_app.config['BOT_API_URL']}/leave")
return jsonify(response.json())
def index():
files = os.listdir(current_app.config['UPLOAD_FOLDER'])
db = get_db()
last_channel_id = db.query(ChannelId).order_by(ChannelId.id.desc()).first()
last_channel_id = last_channel_id.channel_id if last_channel_id else ""
return render_template('index.html', files=files, last_channel_id=last_channel_id)
def get_files():
files = os.listdir(current_app.config['UPLOAD_FOLDER'])
return jsonify(files)
def get_quiz_master():
db = get_db()
quiz_master_data = db.query(QuizMaster).all()
result = []
for qm in quiz_master_data:
result.append({
'id': qm.id,
'title': qm.title,
'full_audio_name': qm.full_audio_name,
'intro_audio_name': qm.intro_audio_name,
'keyword1': qm.keyword1,
'keyword2': qm.keyword2,
'keyword3': qm.keyword3
})
return jsonify(result)
def get_unique_keywords():
db = get_db()
keywords = set()
quiz_master_data = db.query(QuizMaster).all()
for qm in quiz_master_data:
if qm.keyword1:
keywords.add(qm.keyword1)
if qm.keyword2:
keywords.add(qm.keyword2)
if qm.keyword3:
keywords.add(qm.keyword3)
sorted_keywords = sorted(keywords)
return jsonify(sorted_keywords)
def update_quiz_master():
data = request.json
db = get_db()
quiz_master = db.query(QuizMaster).filter_by(id=data['id']).first()
if quiz_master:
old_full_audio_name = quiz_master.full_audio_name
old_intro_audio_name = quiz_master.intro_audio_name
# フル音源名とイントロ音源名を更新
2024-08-03 10:29:46 +00:00
quiz_master.title = data['title']
quiz_master.full_audio_name = data['full_audio_name']
quiz_master.intro_audio_name = data['intro_audio_name']
quiz_master.keyword1 = data['keyword1']
quiz_master.keyword2 = data['keyword2']
quiz_master.keyword3 = data['keyword3']
# ファイル名を変更
upload_folder = current_app.config['UPLOAD_FOLDER']
if old_full_audio_name != data['full_audio_name']:
os.rename(
os.path.join(upload_folder, old_full_audio_name),
os.path.join(upload_folder, data['full_audio_name'])
)
if old_intro_audio_name != data['intro_audio_name']:
os.rename(
os.path.join(upload_folder, old_intro_audio_name),
os.path.join(upload_folder, data['intro_audio_name'])
)
2024-08-03 10:29:46 +00:00
db.commit()
return jsonify({'message': '更新が完了しました'})
return jsonify({'error': 'クイズマスタが見つかりません'})
2024-08-03 10:29:46 +00:00
def add_quiz_master():
data = request.json
db = get_db()
new_quiz_master = QuizMaster(
title=data['title'],
full_audio_name=data['full_audio_name'],
intro_audio_name=data['intro_audio_name'],
keyword1=data['keyword1'],
keyword2=data['keyword2'],
keyword3=data['keyword3']
)
db.add(new_quiz_master)
db.commit()
return jsonify({'message': '追加が完了しました'})
def delete_quiz_master():
data = request.json
db = get_db()
db.query(QuizMaster).filter_by(id=data['id']).delete()
db.commit()
return jsonify({'message': '削除が完了しました'})
def draw_random_quiz():
data = request.json
keyword = data.get('keyword')
selected_quiz = random_quiz(keyword)
if not selected_quiz:
return jsonify({'error': '利用可能なクイズがありません'})
result = {
'id': selected_quiz.id,
'title': selected_quiz.title,
'full_audio_name': selected_quiz.full_audio_name,
'intro_audio_name': selected_quiz.intro_audio_name,
'keyword1': selected_quiz.keyword1,
'keyword2': selected_quiz.keyword2,
'keyword3': selected_quiz.keyword3
}
return jsonify(result)