bcns-gameDistributionSystem/site/modules/gamelist.py
odecif ad3fd6a195 JSONEncoder and manual
Focus on remving deprecated flas.JSONEncoder and replacing it with
json.JSONEncoder.

* Exception handling when API is not accessible [fix #25]
* flask.JSONEncoder deprecated [fix #24]
* Show manual (if present) [fix #23]
* Small touch-ups
2023-09-19 00:47:31 +02:00

57 lines
1.8 KiB
Python

from __main__ import app
import json
import requests
from flask import render_template, redirect, url_for
import modules.init
host_endpoint = modules.init.host_endpoint()
languages = modules.init.get_languages()
app_language = modules.init.app_language
# 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
@app.route("/<lang_code>/gamelist")
# @app.route("/gamelist")
def gamelist(lang_code):
lang_code = lang(lang_code)
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 glist is not None:
return render_template('gamelist.html', gamelist=glist,
**languages[lang_code], lang_code=lang_code)
# Update/Refresh the gamelist by re-scanning the game archive (slow)
@app.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))