From 909e01c3736bb19e868d2acf999601f776f7b657 Mon Sep 17 00:00:00 2001 From: odecif Date: Fri, 28 Oct 2022 11:04:27 +0200 Subject: [PATCH] Gamelist in memory Instead of always scanning the game folder for nfo's when presenting the gamelist it is now loaded once into memory and then accessed from there. --- api/modules/functions.py | 16 ++++++++++++++++ api/modules/gamelist.py | 29 ++++++++++++++++++++--------- site/modules/gamelist.py | 12 +++++++++++- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/api/modules/functions.py b/api/modules/functions.py index c57a9ca..a66e192 100644 --- a/api/modules/functions.py +++ b/api/modules/functions.py @@ -1,6 +1,22 @@ import cv2 import base64 +global gamelist +gamelist = [] + + +# Set the gamelist +def set_gamelist(gamelist_dict): + global gamelist + gamelist = gamelist_dict + return True + + +# Get the gamelist +def get_gamelist(): + global gamelist + return gamelist + # Reducing the size of an artwork/image (PNG to JPEG) and base64-encode it. # imagepath = full file image path diff --git a/api/modules/gamelist.py b/api/modules/gamelist.py index b69fd13..e0a52c6 100644 --- a/api/modules/gamelist.py +++ b/api/modules/gamelist.py @@ -1,25 +1,36 @@ from __main__ import app -from flask import jsonify, request +from flask import jsonify, make_response import modules.db_connect +from modules.functions import get_gamelist, set_gamelist import modules.game import glob -import json -import base64 contentpath = modules.db_connect.contentpath() -# Fetch all _index.nfo-files present in given path and corresponding data + +# Collects all _index.nfo-files present and crunches them into a list of +# games. @app.route('/gamelist', methods=['GET']) -def gamelist(): +def show_gamelist(): + if get_gamelist(): + return jsonify(get_gamelist()) + update_gamelist() + return jsonify(get_gamelist()) - nfolist = list(dict.fromkeys(glob.glob(str(contentpath)+'/**/**/*_index.nfo',recursive=True))) - gamelist = [] +# Updates the gamelist by searching for new nfo's +@app.route('/gamelist/update', methods=['GET']) +def update_gamelist(): + nfolist = list(dict.fromkeys(glob.glob( + str(contentpath)+'/**/**/*_index.nfo', recursive=True))) + + glist = [] for nfo in nfolist: try: game = modules.game.game(nfo, True, True) - gamelist.append(game) + glist.append(game) except Exception as e: print(e) - return jsonify(gamelist) + set_gamelist(glist) + return make_response("

Success

", 200) diff --git a/site/modules/gamelist.py b/site/modules/gamelist.py index 8ffde00..4070f56 100644 --- a/site/modules/gamelist.py +++ b/site/modules/gamelist.py @@ -1,7 +1,7 @@ from __main__ import app import json import requests -from flask import render_template +from flask import render_template, redirect, url_for import modules.init host_endpoint = modules.init.host_endpoint() @@ -26,3 +26,13 @@ def gamelist(lang_code): 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("//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))