bcns-gameDistributionSystem/api/modules/gamelist_functions.py
odecif 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

92 lines
2.4 KiB
Python

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