It is now possible to run the API with gunicorn using gunicorn -w 4 -b 0.0.0.0:8001 'bcnsGDSAPI:create_app()'. Also did other small changes.
76 lines
2.7 KiB
Python
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
|
|
|
|
apihost = bcnsGDSSite.modules.init.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(
|
|
apihost + '/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(
|
|
apihost + '/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(
|
|
apihost + '/gamelist/update')
|
|
if response.status_code == 200:
|
|
return redirect(url_for('gamelist.showgamelist', lang_code=lang_code))
|