kemoverse/web/app.py

92 lines
2.7 KiB
Python

#Kemoverse - a gacha-style bot for the Fediverse.
#Copyright © 2025 Waifu
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as
#published by the Free Software Foundation, either version 3 of the
#License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU Affero General Public License for more details.
#
#You should have received a copy of the GNU Affero General Public License
#along with this program. If not, see https://www.gnu.org/licenses/.
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)