import bcnsGDSAPI.modules.db_connect import glob import os import json from bcnsGDSAPI.modules.functions import reduceartcv2 import threading from queue import Queue import time contentpath = bcnsGDSAPI.modules.db_connect.contentpath() nfosuffix = bcnsGDSAPI.modules.db_connect.nfosuffix() global nfofiles nfofiles = [] global thumbnails_update_status thumbnails_update_status = False global threaded_thumbnails threaded_thumbnails = [] # 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 threaded_thumbnails list def set_threaded_thumbnails(thumbnails_list): global threaded_thumbnails threaded_thumbnails = thumbnails_list return True # Get threaded_thumbnails list def get_threaded_thumbnails(): global threaded_thumbnails return threaded_thumbnails # Threaded printer def printer(queue): while True: message = queue.get() print(message) # Create thumbnails def create_thumbnails(nfolist, lock, queue): global threaded_thumbnails nfos = [] 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: queue.put(( "Failing cover:" + str(art['filename']) + str(e))) nfos.append(current_nfo) with lock: j += 1 queue.put(( "[" + str(j) + "/" + str(len(nfolist)) + "] " + str(nfopath) + " added")) with lock: set_threaded_thumbnails(get_threaded_thumbnails() + nfos) return True # Update thumbnails list, threaded def update_threaded_thumbnails(return_list=False): global thumbnails_update_status nfolist = get_nfofiles() if len(nfolist) == 0: nfolist = update_nfofiles(True) nl1 = nfolist[:len(nfolist)//2] nl2 = nfolist[len(nfolist)//2:] lock = threading.Lock() queue = Queue() printer_thread = threading.Thread( target=printer, args=(queue,), daemon=True, name="Printer") printer_thread.start() t1 = threading.Thread(target=create_thumbnails, args=(nl1, lock, queue)) t2 = threading.Thread(target=create_thumbnails, args=(nl2, lock, queue)) start_time = time.time() t1.start() t2.start() t1.join() t2.join() print("Threading done! duration:", (time.time() - start_time)) if return_list: return get_threaded_thumbnails() return True