From ad3fd6a195610064ead14eb82d5a599616513848 Mon Sep 17 00:00:00 2001 From: odecif Date: Tue, 19 Sep 2023 00:47:31 +0200 Subject: [PATCH] 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 --- api/api.py | 8 ++++---- api/modules/game.py | 13 ++++++++++++- api/modules/gamelist.py | 2 +- site/language/en_US.json | 6 +++++- site/language/sv_SE.json | 1 + site/modules/game.py | 14 +++++++++++++- site/modules/gamelist.py | 27 +++++++++++++++++++++------ site/templates/error.html | 17 +++++++++++++++++ site/templates/game.html | 6 ++++++ 9 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 site/templates/error.html diff --git a/api/api.py b/api/api.py index 2dc0a8f..725231c 100644 --- a/api/api.py +++ b/api/api.py @@ -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 diff --git a/api/modules/game.py b/api/modules/game.py index cb44137..d485ac1 100644 --- a/api/modules/game.py +++ b/api/modules/game.py @@ -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) diff --git a/api/modules/gamelist.py b/api/modules/gamelist.py index bceb87f..fdf89ad 100644 --- a/api/modules/gamelist.py +++ b/api/modules/gamelist.py @@ -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("

Success

", 200) diff --git a/site/language/en_US.json b/site/language/en_US.json index c6a79e2..79e08a8 100644 --- a/site/language/en_US.json +++ b/site/language/en_US.json @@ -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", diff --git a/site/language/sv_SE.json b/site/language/sv_SE.json index 03f7f32..172ddf6 100644 --- a/site/language/sv_SE.json +++ b/site/language/sv_SE.json @@ -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", diff --git a/site/modules/game.py b/site/modules/game.py index 8e824af..2c1c88a 100644 --- a/site/modules/game.py +++ b/site/modules/game.py @@ -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("//game/download") def download(lang_code): pass + + +# Download manual +@app.route("//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)}) diff --git a/site/modules/gamelist.py b/site/modules/gamelist.py index 1c08a1f..491a6e1 100644 --- a/site/modules/gamelist.py +++ b/site/modules/gamelist.py @@ -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) diff --git a/site/templates/error.html b/site/templates/error.html new file mode 100644 index 0000000..a64b7a2 --- /dev/null +++ b/site/templates/error.html @@ -0,0 +1,17 @@ + + + + BCNS + + + {% extends "navigation.html" %} + {% block content %} +

{{lang_error_body_header}}

+ {% if error_object %} +

Something went wrong

+

{{error_object.error_type}}

+

{{error_object.error_message}}

+ {% endif %} + {% endblock %} + + diff --git a/site/templates/game.html b/site/templates/game.html index a104749..39e2f94 100644 --- a/site/templates/game.html +++ b/site/templates/game.html @@ -14,6 +14,12 @@ {% for part in game.game.plot %}

{{part}}

{% endfor %} +

{{lang_game_manual}}

+ {% if 'manual' in game.game %} + {{lang_game_get_manual}} + {% else %} +

{{lang_game_manual_not_found}}

+ {% endif%}

{{lang_game_download_header}}

{{lang_game_download_ingress}}