From 5488d6ddecc45a2014cf0413ccd7529d252b33f8 Mon Sep 17 00:00:00 2001 From: newtextdoc1111 Date: Fri, 9 May 2025 15:18:46 +0900 Subject: [PATCH] refactor: Update CSV file status API response structure --- modules/api.py | 42 ++++++++++++++++++++++++++++-------------- web/js/data.js | 16 ++++++---------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/modules/api.py b/modules/api.py index 90467b1..c200359 100644 --- a/modules/api.py +++ b/modules/api.py @@ -13,10 +13,13 @@ BASE_URL = '/autocomplete-plus/csv' TAGS_BASE_FILE = 'danbooru_tags.csv' COOCCURRENCE_BASE_FILE = 'danbooru_tags_cooccurrence.csv' -def get_extra_csv_files(): +def get_csv_file_status(): """ - Returns a dictionary with extra CSV files for tags and cooccurrence. + Returns a dictionary of csv file statuses. """ + tags_base_exists = os.path.exists(os.path.join(DATA_DIR, TAGS_BASE_FILE)) + cooccurrence_base_exists = os.path.exists(os.path.join(DATA_DIR, COOCCURRENCE_BASE_FILE)) + tags_extra_files = [] cooccurrence_extra_files = [] @@ -28,14 +31,19 @@ def get_extra_csv_files(): for filename in all_csv_files: if filename in [TAGS_BASE_FILE, COOCCURRENCE_BASE_FILE]: continue # Skip base files - if 'cooccurrence' in filename.lower(): cooccurrence_extra_files.append(filename) elif 'tag' in filename.lower(): tags_extra_files.append(filename) + + # Return the lists of extra files return { - 'tags': tags_extra_files, - 'cooccurrence': cooccurrence_extra_files, + 'danbooru':{ + 'base_tags': tags_base_exists, # exists + 'extra_tags': tags_extra_files, + 'base_cooccurrence': cooccurrence_base_exists, + 'extra_cooccurrence': cooccurrence_extra_files, + } } # --- API Endpoints --- @@ -43,13 +51,19 @@ def get_extra_csv_files(): @server.PromptServer.instance.routes.get('/autocomplete-plus/csv') async def get_csv_list(_request): """ - Returns number of extra CSV files available. + Returns CSV file status. + base files: file exists boolean + extra files: count of extra files """ - extra_csv_files = get_extra_csv_files() + extra_csv_files = get_csv_file_status() response = { - 'tags': len(extra_csv_files['tags']), - 'cooccurrence': len(extra_csv_files['cooccurrence']), + 'danbooru': { + 'base_tags': extra_csv_files['danbooru']['base_tags'], + 'extra_tags': len(extra_csv_files['danbooru']['extra_tags']), + 'base_cooccurrence': extra_csv_files['danbooru']['base_cooccurrence'], + 'extra_cooccurrence': len(extra_csv_files['danbooru']['extra_cooccurrence']), + } } return web.json_response(response) @@ -69,13 +83,13 @@ async def get_extra_tags_file(request): Returns the extra tags CSV file at the specified index. """ try: - extra_csv_files = get_extra_csv_files() + extra_csv_files = get_csv_file_status() index = int(request.match_info['index']) - if index < 0 or index >= len(extra_csv_files['tags']): + if index < 0 or index >= len(extra_csv_files['danbooru']['extra_tags']): return web.json_response({"error": "Invalid index"}, status=404) - file_path = os.path.join(DATA_DIR, extra_csv_files['tags'][index]) + file_path = os.path.join(DATA_DIR, extra_csv_files['danbooru']['extra_tags'][index]) if not os.path.exists(file_path): return web.json_response({"error": "Extra tags file not found"}, status=404) @@ -100,13 +114,13 @@ async def get_extra_cooccurrence_file(request): Returns the extra cooccurrence CSV file at the specified index. """ try: - extra_csv_files = get_extra_csv_files() + extra_csv_files = get_csv_file_status() index = int(request.match_info['index']) if index < 0 or index >= len(extra_csv_files['cooccurrence']): return web.json_response({"error": "Invalid index"}, status=404) - file_path = os.path.join(DATA_DIR, extra_csv_files['cooccurrence'][index]) + file_path = os.path.join(DATA_DIR, extra_csv_files['danbooru']['extra_cooccurrence'][index]) if not os.path.exists(file_path): return web.json_response({"error": "Extra cooccurrence file not found"}, status=404) diff --git a/web/js/data.js b/web/js/data.js index dc30bf0..3683e44 100644 --- a/web/js/data.js +++ b/web/js/data.js @@ -274,26 +274,22 @@ export async function initializeData() { } const csvListData = await response.json(); - const extraTagsCount = csvListData.tags || 0; - const extraCooccurrenceCount = csvListData.cooccurrence || 0; + const extraTagsCount = csvListData.danbooru.extra_tags || 0; + const extraCooccurrenceCount = csvListData.danbooru.extra_cooccurrence || 0; const tagsUrl = '/autocomplete-plus/csv/tags'; - const tagsLoadPromises = [ - loadTags(`${tagsUrl}/base`) - ]; + const tagsLoadPromises = csvListData.danbooru.base_tags ? [loadTags(`${tagsUrl}/base`)] : []; - let currentTagPromise = tagsLoadPromises[0]; + let currentTagPromise = tagsLoadPromises[0] || Promise.resolve(); for (let i = 0; i < extraTagsCount; i++) { currentTagPromise = currentTagPromise.then(loadTags(`${tagsUrl}/extra/${i}`)); tagsLoadPromises.push(currentTagPromise); } const cooccurrenceUrl = '/autocomplete-plus/csv/cooccurrence'; - const cooccurrenceLoadPromises = [ - loadCooccurrence(`${cooccurrenceUrl}/base`) - ]; + const cooccurrenceLoadPromises = csvListData.danbooru.base_cooccurrence ? [loadCooccurrence(`${cooccurrenceUrl}/base`)] : []; - let cooccurrencePromiseChain = cooccurrenceLoadPromises[0]; + let cooccurrencePromiseChain = cooccurrenceLoadPromises[0] || Promise.resolve(); for (let i = 0; i < extraCooccurrenceCount; i++) { cooccurrencePromiseChain = cooccurrencePromiseChain.then(loadCooccurrence(`${cooccurrenceUrl}/extra/${i}`)); cooccurrenceLoadPromises.push(cooccurrencePromiseChain);