Compare commits

...

3 Commits

Author SHA1 Message Date
7706b0aa0b Merge pull request '20_gamelist_performance_improv' (#29) from 20_gamelist_performance_improv into master
Reviewed-on: https://git.odecif.net:3000/odecif/bcns-gameDistributionSystem/pulls/29
2023-09-20 15:18:43 +02:00
014fa4c9a2 Cleanup comments 2023-09-20 15:17:37 +02:00
fcf71d6fea Gamelist performance improvements
* Separated /gamelist to 2 calls: /gamelist and /gamelist/thumbnails.
* Refactored nested for-loop with a next() when looping artwork

[fixes #20]
2023-09-20 15:13:42 +02:00
7 changed files with 179 additions and 29 deletions

View File

@ -11,7 +11,11 @@ contentpath = modules.db_connect.contentpath()
# Fetch and present all information from one _index.nfo-file # Fetch and present all information from one _index.nfo-file
@app.route('/game', methods=['POST']) @app.route('/game', methods=['POST'])
def game(predefinednfo=False, return_dict=False, skip_artwork=False): def game(
predefinednfo=False,
return_dict=False,
skip_artwork=False,
skip_displayimage=False):
nfopath = "" nfopath = ""
if predefinednfo is not False: if predefinednfo is not False:
nfopath = predefinednfo nfopath = predefinednfo
@ -19,31 +23,36 @@ def game(predefinednfo=False, return_dict=False, skip_artwork=False):
nfopath = base64.b64decode(request.json).decode() nfopath = base64.b64decode(request.json).decode()
nfo = json.load(open(nfopath, 'r')) nfo = json.load(open(nfopath, 'r'))
nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode() nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode()
nfo['nfo'] = os.path.basename(nfopath)
# Add front cover artwork in medium size # Add front cover artwork in medium size
artpath = os.path.dirname(nfopath)+'/art/' # TODO: Refactor to use next() instead of nested for()
i = 0 if skip_displayimage is True:
if 'artwork' in nfo['game']:
if nfo['game']['artwork']:
for art in nfo['game']['artwork']:
try:
if skip_artwork is False:
nfo['game']['artwork'][i]['data'] = reduceartcv2(
artpath+art['filename'], 'thumbnail')
if art['type'] == 'front':
nfo['game']['displayimage'] = reduceartcv2(
artpath+art['filename'], 'thumbnail')
except Exception as e:
print("Failing cover: " + art['filename'])
print(e)
i += 1
if 'displayimage' not in nfo['game']:
for art in nfo['game']['artwork']:
if art['type'] in ['cd', 'dvd']:
nfo['game']['displayimage'] = reduceartcv2(
artpath+art['filename'], 'thumbnail')
else:
nfo['game']['displayimage'] = "" nfo['game']['displayimage'] = ""
else:
artpath = os.path.dirname(nfopath)+'/art/'
i = 0
if 'artwork' in nfo['game']:
if nfo['game']['artwork']:
for art in nfo['game']['artwork']:
try:
if skip_artwork is False:
nfo['game']['artwork'][i]['data'] = reduceartcv2(
artpath+art['filename'], 'thumbnail')
if art['type'] == 'front':
nfo['game']['displayimage'] = reduceartcv2(
artpath+art['filename'], 'thumbnail')
except Exception as e:
print("Failing cover: " + art['filename'])
print(e)
i += 1
if 'displayimage' not in nfo['game']:
for art in nfo['game']['artwork']:
if art['type'] in ['cd', 'dvd']:
nfo['game']['displayimage'] = reduceartcv2(
artpath+art['filename'], 'thumbnail')
else:
nfo['game']['displayimage'] = ""
if return_dict is False: if return_dict is False:
return jsonify(nfo) return jsonify(nfo)
return nfo return nfo

View File

@ -3,6 +3,7 @@ from flask import jsonify, make_response
import modules.db_connect import modules.db_connect
from modules.functions import get_gamelist, set_gamelist from modules.functions import get_gamelist, set_gamelist
import modules.game import modules.game
from modules.gamelist_functions import get_thumbnails, update_thumbnails
import glob import glob
contentpath = modules.db_connect.contentpath() contentpath = modules.db_connect.contentpath()
@ -28,10 +29,26 @@ def update_gamelist():
glist = [] glist = []
for nfo in nfolist: for nfo in nfolist:
try: try:
game = modules.game.game(nfo, True, True) game = modules.game.game(nfo, True, True, True)
glist.append(game) glist.append(game)
except Exception as e: except Exception as e:
print(nfo, e) print(nfo, e)
set_gamelist(glist) set_gamelist(glist)
return make_response("<h1>Success</h1>", 200) return make_response("<h1>Success</h1>", 200)
# Fetch displayimage for all nfo-files
@app.route('/gamelist/displayimage')
def get_displayimages(update=False):
thumbnails = get_thumbnails()
if (len(thumbnails) == 0) or update:
thumbnails = update_thumbnails(True)
return jsonify(thumbnails)
# Update displayimages
@app.route('/gamelist/displayimage/update')
def update_displayimages():
update_thumbnails()
return make_response("<h1>Success</h1>", 200)

View File

@ -0,0 +1,91 @@
import modules.db_connect
import glob
import os
import json
from modules.functions import reduceartcv2
contentpath = modules.db_connect.contentpath()
nfosuffix = modules.db_connect.nfosuffix()
global nfofiles
nfofiles = []
global thumbnails
thumbnails = []
global thumbnails_update_status
thumbnails_update_status = False
# Set nfo-files list
def set_nfofiles(nfofiles_list):
global nfofiles
nfofiles = nfofiles_list
return True
# Get nfo-files list
def get_nfofiles():
global nfofiles
return nfofiles
# Get list of all .nfo-files
def update_nfofiles(return_list=False):
newlist = list(dict.fromkeys(glob.glob(
str(contentpath)+'/**/**/*'+nfosuffix, recursive=True)))
set_nfofiles(newlist)
if return_list is True:
return newlist
return True
# Set thumbnails list
def set_thumbnails(thumbnails_list):
global thumbnails
thumbnails = thumbnails_list
return True
# Get thumbnails list
def get_thumbnails():
global thumbnails
return thumbnails
# Update thumbnails list
def update_thumbnails(return_list=False):
global thumbnails_update_status
thumbnails_update_status = True
nfos = []
nfolist = get_nfofiles()
if len(nfolist) == 0:
nfolist = update_nfofiles(True)
j = 0
for nfopath in nfolist:
nfo = json.load(open(nfopath, 'r'))
current_nfo = {"nfo": os.path.basename(nfopath)}
artpath = os.path.dirname(nfopath)+'/art/'
if 'artwork' in nfo['game']:
if nfo['game']['artwork']:
art = next((
artw for artw in nfo['game']['artwork'] if artw['type'] ==
"front"), False)
if art is False:
art = next((
artw for artw in nfo['game']['artwork'] if
artw['type'] in ['cd', 'dvd']), False)
if art is not False:
try:
current_nfo['displayimage'] = reduceartcv2(
artpath+art['filename'], 'thumbnail')
except Exception as e:
print("Failing cover:", art['filename'], e)
nfos.append(current_nfo)
j += 1
print("[", j, "/", len(nfofiles), "]", nfopath, "added")
set_thumbnails(nfos)
thumbnails_update_status = False
if return_list is True:
return nfos
return True

View File

@ -8,6 +8,7 @@
"lang_gamelist_title": "Gamelist", "lang_gamelist_title": "Gamelist",
"lang_gamelist_body_header": "Gamelist", "lang_gamelist_body_header": "Gamelist",
"lang_gamelist_body_ingress": "Here there be a bunch of games listed", "lang_gamelist_body_ingress": "Here there be a bunch of games listed",
"lang_gamelist_get_thumbnails_button": "Get thumbnails",
"lang_game_title": "Game", "lang_game_title": "Game",
"lang_game_body_header": "Game", "lang_game_body_header": "Game",
"lang_game_body_ingress": "Here there be info about games n stuff", "lang_game_body_ingress": "Here there be info about games n stuff",

View File

@ -5,5 +5,6 @@
"lang_home_change_language_button": "Byt", "lang_home_change_language_button": "Byt",
"lang_navigation_home": "Hem", "lang_navigation_home": "Hem",
"lang_navigation_gamelist": "Spellista", "lang_navigation_gamelist": "Spellista",
"lang_gamelist_get_thumbnails_button": "Hämta bilder",
"lang_game_download_zip_button": "Ladda ned Zip" "lang_game_download_zip_button": "Ladda ned Zip"
} }

View File

@ -1,8 +1,9 @@
from __main__ import app from __main__ import app
import json import json
import requests import requests
from flask import render_template, redirect, url_for from flask import render_template, redirect, url_for, request
import modules.init import modules.init
import base64
host_endpoint = modules.init.host_endpoint() host_endpoint = modules.init.host_endpoint()
languages = modules.init.get_languages() languages = modules.init.get_languages()
@ -17,10 +18,11 @@ def lang(lang_code):
# Show gamelist # Show gamelist
@app.route("/<lang_code>/gamelist") @app.route("/<lang_code>/gamelist", methods=["GET", "POST"])
# @app.route("/gamelist") # @app.route("/gamelist")
def gamelist(lang_code): def gamelist(lang_code):
lang_code = lang(lang_code) lang_code = lang(lang_code)
thumbnails = request.args.get("thumbnails")
glist = None glist = None
try: try:
glist = json.loads((requests.get( glist = json.loads((requests.get(
@ -41,9 +43,27 @@ def gamelist(lang_code):
except Exception as e: except Exception as e:
print("error type: ", type(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: if glist is not None:
return render_template('gamelist.html', gamelist=glist, return render_template('gamelist.html', gamelist=glist,
**languages[lang_code], lang_code=lang_code) **languages[lang_code], lang_code=lang_code, thumbnails=thumbnailslist)
# Update/Refresh the gamelist by re-scanning the game archive (slow) # Update/Refresh the gamelist by re-scanning the game archive (slow)

View File

@ -9,6 +9,7 @@
{% block content %} {% block content %}
<h1>{{lang_gamelist_body_header}}</h1> <h1>{{lang_gamelist_body_header}}</h1>
<p>{{lang_gamelist_body_ingress}}</p> <p>{{lang_gamelist_body_ingress}}</p>
<a href="{{ url_for('gamelist', thumbnails="get", lang_code=lang_code)}}"><button type="button">{{lang_gamelist_get_thumbnails_button}}</button></a>
<table class="game"> <table class="game">
<tr> <tr>
<th>{{lang_game_artwork}}</th> <th>{{lang_game_artwork}}</th>
@ -20,12 +21,22 @@
</tr> </tr>
{% for item in gamelist %} {% for item in gamelist %}
<tr> <tr>
<td><a href="{{ url_for('game', gamepath=item.path, lang_code=lang_code)}}"><img src="data:image/jpeg;base64,{{item.game.displayimage}}"></img></a></td> {% for thumbnail in thumbnails %}
{% if thumbnail.nfo == item.nfo %}
<td><a href="{{ url_for('game', gamepath=item.path, lang_code=lang_code)}}"><img src="data:image/jpeg;base64,{{thumbnail.displayimage}}"></img></a></td>
{% endif %}
{% else %}
<td></td>
{% endfor %}
<td><a href="{{ url_for('game', gamepath=item.path, lang_code=lang_code)}}">{{item.game.title}}</a></td> <td><a href="{{ url_for('game', gamepath=item.path, lang_code=lang_code)}}">{{item.game.title}}</a></td>
<td>{{item.game.year}}</td> <td>{{item.game.year}}</td>
<td>{{item.game.genre}}</td> <td>{{item.game.genre}}</td>
<td>{{item.game.developer}}</td> <td>{{item.game.developer}}</td>
<td>{{item.game.playermodes}}</td> <td>
{% for mode in item.game.playermodes %}
<p>{{mode}} </p>
{% endfor %}
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>