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
This commit is contained in:
odecif 2023-09-19 00:47:31 +02:00
parent e03ab77a67
commit ad3fd6a195
9 changed files with 80 additions and 14 deletions

View File

@ -1,10 +1,10 @@
from flask import request, jsonify, Flask
from flask.json import JSONEncoder
import json
from datetime import date
import modules.db_connect
class MyJSONEncoder(JSONEncoder):
class MyJSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, date):
return o.isoformat()
@ -18,8 +18,8 @@ class MyFlask(Flask):
app = MyFlask(__name__)
modules.db_connect.init()
import modules.gamelist # noqa: E402
import modules.game # noqa: E402
import modules.gamelist # noqa: E402
import modules.game # noqa: E402
# Important initialization stuff
config = modules.db_connect.config

View File

@ -1,5 +1,5 @@
from __main__ import app
from flask import jsonify, request
from flask import jsonify, request, send_from_directory
import base64
import json
import modules.db_connect
@ -75,3 +75,14 @@ def artwork(size='max'):
artlist.append(art)
return jsonify(artlist)
# Server game manual
@app.route('/getmanual', methods=["GET"])
def getmanual():
nfopath = base64.b64decode(request.json).decode()
nfo = json.load(open(nfopath, 'r'))
nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode()
manualname = nfo['game']['manual']
manualpath = os.path.dirname(nfopath)+'/'
return send_from_directory(manualpath, manualname, as_attachment=True)

View File

@ -31,7 +31,7 @@ def update_gamelist():
game = modules.game.game(nfo, True, True)
glist.append(game)
except Exception as e:
print(e)
print(nfo, e)
set_gamelist(glist)
return make_response("<h1>Success</h1>", 200)

View File

@ -1,7 +1,8 @@
{
"lang_error_body_header": "Oops, something went wrong",
"lang_home_body_header": "Hello and welcome",
"lang_home_change_language": "Change language",
"lang_home_change_language_button": "CHange",
"lang_home_change_language_button": "Change",
"lang_navigation_home": "Home",
"lang_navigation_gamelist": "Game list",
"lang_gamelist_title": "Gamelist",
@ -11,6 +12,9 @@
"lang_game_body_header": "Game",
"lang_game_body_ingress": "Here there be info about games n stuff",
"lang_game_plot": "Plot",
"lang_game_manual": "Manual",
"lang_game_get_manual": "Link to manual",
"lang_game_manual_not_found": "Manual not available",
"lang_game_download_header": "Download game",
"lang_game_download_ingress": "Here you can download the game using either torrent file or a magnet-link!",
"lang_game_download_torrent_button": "Download",

View File

@ -1,4 +1,5 @@
{
"lang_error_body_header": "Ojdå, någonting gick fel",
"lang_home_body_header": "Hej och välkommen",
"lang_home_change_language": "Byt språk",
"lang_home_change_language_button": "Byt",

View File

@ -1,7 +1,7 @@
from __main__ import app
import json
import requests
from flask import render_template, request
from flask import render_template, request, Response
import modules.init
host_endpoint = modules.init.host_endpoint()
@ -47,3 +47,15 @@ def artwork(lang_code):
@app.route("/<lang_code>/game/download")
def download(lang_code):
pass
# Download manual
@app.route("/<lang_code>/game/getmanual")
def getmanual(lang_code):
gamepath = request.args.get("gamepath")
manualname = request.args.get("manual")
manual = requests.get(host_endpoint + '/getmanual', json=gamepath)
return Response(manual, mimetype="application/pdf",
headers={
"Content-Disposition":
"attachment;filename=" + str(manualname)})

View File

@ -21,14 +21,29 @@ def lang(lang_code):
# @app.route("/gamelist")
def gamelist(lang_code):
lang_code = lang(lang_code)
glist = json.loads((requests.get(
host_endpoint + '/gamelist').content).decode())
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'])
# Sorting list alphabetically
glist = sorted(glist, key=lambda d: d['game']['title'])
return render_template('gamelist.html', gamelist=glist,
**languages[lang_code], lang_code=lang_code)
# 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)

17
site/templates/error.html Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>BCNS</title>
</head>
<body>
{% extends "navigation.html" %}
{% block content %}
<h1>{{lang_error_body_header}}</h1>
{% if error_object %}
<p class="error">Something went wrong</p>
<p>{{error_object.error_type}}</p>
<p>{{error_object.error_message}}</p>
{% endif %}
{% endblock %}
</body>
</html>

View File

@ -14,6 +14,12 @@
{% for part in game.game.plot %}
<p>{{part}}</p>
{% endfor %}
<h3>{{lang_game_manual}}</h3>
{% if 'manual' in game.game %}
<a href="{{ url_for('getmanual', gamepath=game.path, manual=game.game.manual, lang_code=lang_code)}}">{{lang_game_get_manual}}</a>
{% else %}
<p>{{lang_game_manual_not_found}}</p>
{% endif%}
<h3>{{lang_game_download_header}}</h3>
<p>{{lang_game_download_ingress}}</p>
<a href="{{ url_for('download', gamepath=game.path, lang_code=lang_code)}}"><button type="button">{{lang_game_download_torrent_button}}</button></a>