diff --git a/api/modules/game.py b/api/modules/game.py index 5a67c79..b995c8f 100644 --- a/api/modules/game.py +++ b/api/modules/game.py @@ -11,7 +11,11 @@ contentpath = modules.db_connect.contentpath() # Fetch and present all information from one _index.nfo-file @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 = "" if predefinednfo is not False: nfopath = predefinednfo @@ -19,31 +23,36 @@ def game(predefinednfo=False, return_dict=False, skip_artwork=False): nfopath = base64.b64decode(request.json).decode() nfo = json.load(open(nfopath, 'r')) nfo['path'] = base64.b64encode(nfopath.encode('utf-8')).decode() + nfo['nfo'] = os.path.basename(nfopath) # Add front cover artwork in medium size - 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: + # TODO: Refactor to use next() instead of nested for() + if skip_displayimage is True: 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: return jsonify(nfo) return nfo diff --git a/api/modules/gamelist.py b/api/modules/gamelist.py index fdf89ad..8e49eab 100644 --- a/api/modules/gamelist.py +++ b/api/modules/gamelist.py @@ -3,6 +3,7 @@ from flask import jsonify, make_response import modules.db_connect from modules.functions import get_gamelist, set_gamelist import modules.game +from modules.gamelist_functions import get_thumbnails, update_thumbnails import glob contentpath = modules.db_connect.contentpath() @@ -28,10 +29,26 @@ def update_gamelist(): glist = [] for nfo in nfolist: try: - game = modules.game.game(nfo, True, True) + game = modules.game.game(nfo, True, True, True) glist.append(game) except Exception as e: print(nfo, e) set_gamelist(glist) return make_response("

Success

", 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("

Success

", 200) diff --git a/api/modules/gamelist_functions.py b/api/modules/gamelist_functions.py new file mode 100644 index 0000000..d0da7cd --- /dev/null +++ b/api/modules/gamelist_functions.py @@ -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 diff --git a/site/language/en_US.json b/site/language/en_US.json index d38760d..bb12079 100644 --- a/site/language/en_US.json +++ b/site/language/en_US.json @@ -8,6 +8,7 @@ "lang_gamelist_title": "Gamelist", "lang_gamelist_body_header": "Gamelist", "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_body_header": "Game", "lang_game_body_ingress": "Here there be info about games n stuff", diff --git a/site/language/sv_SE.json b/site/language/sv_SE.json index 193c5e4..940a80e 100644 --- a/site/language/sv_SE.json +++ b/site/language/sv_SE.json @@ -5,5 +5,6 @@ "lang_home_change_language_button": "Byt", "lang_navigation_home": "Hem", "lang_navigation_gamelist": "Spellista", + "lang_gamelist_get_thumbnails_button": "Hämta bilder", "lang_game_download_zip_button": "Ladda ned Zip" } diff --git a/site/modules/gamelist.py b/site/modules/gamelist.py index 491a6e1..144a978 100644 --- a/site/modules/gamelist.py +++ b/site/modules/gamelist.py @@ -1,8 +1,9 @@ from __main__ import app import json import requests -from flask import render_template, redirect, url_for +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() @@ -17,10 +18,11 @@ def lang(lang_code): # Show gamelist -@app.route("//gamelist") +@app.route("//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( @@ -41,9 +43,27 @@ def gamelist(lang_code): 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) + **languages[lang_code], lang_code=lang_code, thumbnails=thumbnailslist) # Update/Refresh the gamelist by re-scanning the game archive (slow) diff --git a/site/templates/gamelist.html b/site/templates/gamelist.html index 80f2b16..441288e 100644 --- a/site/templates/gamelist.html +++ b/site/templates/gamelist.html @@ -9,6 +9,7 @@ {% block content %}

{{lang_gamelist_body_header}}

{{lang_gamelist_body_ingress}}

+ @@ -20,12 +21,22 @@ {% for item in gamelist %} - + {% for thumbnail in thumbnails %} + {% if thumbnail.nfo == item.nfo %} + + {% endif %} + {% else %} + + {% endfor %} - + {% endfor %}
{{lang_game_artwork}}
{{item.game.title}} {{item.game.year}} {{item.game.genre}} {{item.game.developer}}{{item.game.playermodes}} + {% for mode in item.game.playermodes %} +

{{mode}}

+ {% endfor %} +