bcns-gameDistributionSystem/site/modules/gamelist.py

76 lines
2.6 KiB
Python

from __main__ import app
import json
import requests
from flask import render_template, redirect, url_for, request
import modules.init
import base64
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", methods=["GET", "POST"])
# @app.route("/gamelist")
def gamelist(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)
@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))