bcns-gameDistributionSystem/bcnsGDSSite/modules/gamelist.py
odecif 631dc761c6 Major overhaul
* Now using Blueprints instead of importing modules.
* Now using def create_app instead of just app
2023-11-18 02:52:39 +01:00

76 lines
2.7 KiB
Python

import json
import requests
from flask import render_template, redirect, url_for, request, Blueprint
import bcnsGDSSite.modules.init
import base64
host_endpoint = bcnsGDSSite.modules.init.get_apihost()
languages = bcnsGDSSite.modules.init.get_languages()
app_language = bcnsGDSSite.modules.init.app_language
gamelist = Blueprint('gamelist', __name__,
template_folder='templates')
# Check if valid language code is set. If not, return app default
def lang(lang_code):
if lang_code not in languages:
return app_language
return lang_code
# Show gamelist
@gamelist.route("/<lang_code>/gamelist", methods=["GET", "POST"])
def showgamelist(lang_code):
lang_code = lang(lang_code)
thumbnails = request.args.get("thumbnails")
glist = None
try:
glist = json.loads((requests.get(
host_endpoint + '/gamelist').content).decode())
# Sorting list alphabetically
glist = sorted(glist, key=lambda d: d['game']['title'])
# If the server is down
except requests.exceptions.ConnectionError as e:
print("fancy connection error: ", e)
em = "Cannot connect to the API, is the server up?"
et = "Connection Error"
error_object = {"error_type": et, "error_message": em}
return render_template('error.html', **languages[lang_code],
lang_code=lang_code, error_object=error_object)
except Exception as e:
print("error type: ", type(e))
# If thumbnails are to be collected
thumbnailslist = []
if thumbnails == "get":
try:
thumbnailslist = json.loads((requests.get(
host_endpoint + '/gamelist/displayimage').content).decode())
except request.exceptions.ConnectionError as e:
print(e)
em = "Cannot connect to the API, is the server up?"
et = "ConnectionError"
error_object = {"error_type": et, "error_message": em}
return render_template('error.html', **languages[lang_code],
lang_code=lang_code, error_object=error_object)
except Exception as e:
print("error type:", type(e))
if glist is not None:
return render_template('gamelist.html', gamelist=glist,
**languages[lang_code], lang_code=lang_code, thumbnails=thumbnailslist)
# Update/Refresh the gamelist by re-scanning the game archive (slow)
@gamelist.route("/<lang_code>/gamelist/update")
def gamelist_update(lang_code):
lang_code = lang(lang_code)
response = requests.get(
host_endpoint + '/gamelist/update')
if response.status_code == 200:
return redirect(url_for('gamelist', lang_code=lang_code))