rpiwebapp-public/games/games.py

92 lines
2.6 KiB
Python
Raw Normal View History

from flask import Blueprint, render_template, request, send_file, current_app, jsonify, abort
from flask_login import login_required
from pathvalidate import sanitize_filename
import os
import inspect
import re
from scripts import database, func
Games = Blueprint("games", __name__, template_folder="templates")
@Games.route("/games")
@login_required
def index():
try:
page = request.args.get("page", 1, type=int)
max_items = request.args.get("max_items", 30, type=int)
games = database.get_all_games()
start = (max_items*(page-1))
end = len(games) if len(games) < max_items*page else max_items*page
return render_template("games/index.html", title="Games", games=games)
except Exception as e:
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
return str(type(e)) + " " + str(e)
@Games.route('/games/get_games')
@login_required
def get_games():
try:
games = database.get_all_games()
games_json = {}
for game in games:
games_json[game.game_id] = {
"id": game.game_id,
"title": game.title,
"windows": game.windows,
"mac": game.mac,
"linux": game.linux,
"description": game.description,
"poster_path": game.poster_path
}
return jsonify(games_json)
# return jsonify({game["id"]: game for game in games})
except Exception as e:
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
return str(type(e)) + " " + str(e)
@Games.route('/games/get_game/<int:game_id>')
@login_required
def get_game(game_id):
try:
game = database.get_game(game_id)
if game:
game_json = {
"title": game.title,
"game_id": game.game_id,
"description": game.description,
"poster_path": game.poster_path,
"windows": game.windows,
"mac": game.mac,
"linux": game.linux
}
return jsonify(game_json)
abort(404)
except Exception as e:
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
return str(type(e)) + " " + str(e)
@Games.route("/games/download/<int:game_id>")
@login_required
def download_game(game_id):
try:
game = database.get_game(game_id)
if game:
files = game.windows["files"]
filename = sanitize_filename(files[0])
folder = re.match(r"(.+)_setup_win.(exe|msi)", filename).group(1)
if len(files) > 1:
filename = sanitize_filename(game.title+".zip")
path = os.path.join(func.GAMES_DIRECTORY, folder, filename)
return send_file(path, as_attachment=True, attachment_filename=filename)
else:
abort(404)
except Exception as e:
current_app.logger.info(inspect.stack()[0][3] + " " + str(type(e)) + " " + str(e))
return str(type(e)) + " " + str(e)