refactor: Update CSV file status API response structure

This commit is contained in:
newtextdoc1111
2025-05-09 15:18:46 +09:00
parent 4827b11168
commit 5488d6ddec
2 changed files with 34 additions and 24 deletions
+28 -14
View File
@@ -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)