kemoverse/web/app.py
2025-06-14 05:08:59 +09:00

76 lines
2 KiB
Python

import sqlite3
import sys
from pathlib import Path
# Add parent directory to Python path so we can import from bot/
sys.path.append(str(Path(__file__).parent.parent))
from bot.config import WEB_PORT, BIND_ADDRESS, DB_PATH
from flask import Flask, render_template, abort
from werkzeug.exceptions import HTTPException
app = Flask(__name__)
def get_db_connection():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
@app.errorhandler(HTTPException)
def handle_exception(error):
return render_template("_error.html", error=error), error.code
@app.route("/i404")
def i404():
return abort(404)
@app.route('/')
def index():
conn = get_db_connection()
users = conn.execute('SELECT id, username FROM users').fetchall()
top_users = conn.execute('''
SELECT users.id, users.username, COUNT(pulls.id) AS pull_count
FROM users
LEFT JOIN pulls ON users.id = pulls.user_id
GROUP BY users.id
ORDER BY pull_count DESC
LIMIT 5
''').fetchall()
conn.close()
return render_template('index.html', users=users, top_users=top_users)
@app.route('/user/<int:user_id>')
def user_profile(user_id):
conn = get_db_connection()
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('SELECT * FROM users WHERE id = ?', (user_id,))
user = cursor.fetchone()
if user is None:
abort(404)
cursor.execute('''
SELECT pulls.timestamp, characters.name as character_name, characters.rarity
FROM pulls
JOIN characters ON pulls.character_id = characters.id
WHERE pulls.user_id = ?
ORDER BY pulls.timestamp DESC
''', (user_id,))
pulls = cursor.fetchall()
conn.close()
return render_template('user.html', user=user, pulls=pulls)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/submit')
def submit_character():
return render_template('submit.html')
if __name__ == '__main__':
app.run(host=BIND_ADDRESS, port=WEB_PORT, debug=True)