Files
novafarma/scripts/sort_detective.py
2026-01-20 01:05:17 +01:00

105 lines
4.2 KiB
Python

import os
import shutil
ROOT = "assets/slike"
CATEGORIES = {
"teren": ["grass", "dirt", "sand", "ground", "path", "tile", "water", "farmland", "floor", "stone"],
"okolje": ["tree", "bush", "flower", "rock", "plant", "vegetation", "nature", "weeds", "stump", "log"],
"liki": ["kai", "ana", "gronk", "player", "character"],
"sovrazniki": ["zombie", "enemy", "monster", "mutant", "golem", "boss", "creature", "beast"],
"predmeti": ["item", "tool", "food", "inventory", "icon", "pickaxe", "axe", "hoe", "shovel", "watering", "weapon", "drop", "seed", "crop", "vape"],
"zgodba": ["intro", "family", "virus", "memory", "portrait", "cutscene", "flash", "story", "dream", "wakeup"],
"zivali": ["cow", "sheep", "chicken", "pig", "animal", "fauna", "rabbit", "dog", "cat", "bird", "owl"]
}
# Create destination folders
DESTINATIONS = {}
for cat in CATEGORIES.keys():
path = os.path.join(ROOT, cat)
if not os.path.exists(path):
os.makedirs(path)
DESTINATIONS[cat] = path
ZA_PREGLED = os.path.join(ROOT, "ZA_PREGLED")
if not os.path.exists(ZA_PREGLED):
os.makedirs(ZA_PREGLED)
def sort_detective():
count_moved = 0
# Walk through all files in ROOT recursively
for root_dir, dirs, files in os.walk(ROOT):
# Skip categorization folders themselves to avoid loops/re-sorting sorted stuff?
# Actually, user wants to re-sort EVERYTHING.
# But if I move file from Teren to Teren, it's fine.
for file in files:
if not file.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
continue
src_path = os.path.join(root_dir, file)
fname = file.lower()
# Decide category
target_cat = "ZA_PREGLED"
# Priority Check
found = False
# Check Zgodba (Story) - PRIORITY 1
if any(k in fname for k in CATEGORIES["zgodba"]):
target_cat = "zgodba"
found = True
elif any(k in fname for k in CATEGORIES["liki"]):
target_cat = "liki"
found = True
elif any(k in fname for k in CATEGORIES["sovrazniki"]):
target_cat = "sovrazniki"
found = True
elif any(k in fname for k in CATEGORIES["predmeti"]):
target_cat = "predmeti"
found = True
elif any(k in fname for k in CATEGORIES["zivali"]):
target_cat = "zivali" # I'll treat this as valid, user didn't forbid it
found = True
elif any(k in fname for k in CATEGORIES["okolje"]):
target_cat = "okolje"
found = True
elif any(k in fname for k in CATEGORIES["teren"]):
target_cat = "teren"
found = True
# Special Rule: Terrain Strict
# If it's currently in 'teren' but matches 'tree', move to 'okolje'.
# If it's in 'biomi' and matches 'grass', move to 'teren'.
# Logic: Just move to target_cat.
dest_folder = DESTINATIONS.get(target_cat, ZA_PREGLED)
if target_cat == "zivali": # User didn't ask for zivali explicitly, use ZA_PREGLED or create it?
# I added it to DESTINATIONS, so it goes to assets/slike/zivali.
pass
# If src folder is same as dest folder, skip
if os.path.abspath(root_dir) == os.path.abspath(dest_folder):
continue
dest_path = os.path.join(dest_folder, file)
# Conflict handling
if os.path.exists(dest_path):
base, ext = os.path.splitext(file)
import uuid
dest_path = os.path.join(dest_folder, f"{base}_{uuid.uuid4().hex[:4]}{ext}")
try:
shutil.move(src_path, dest_path)
count_moved += 1
# print(f"Moved {file} -> {target_cat}")
except Exception as e:
print(f"Error moving {file}: {e}")
print(f"✅ SORT DETECTIVE FINISHED. Moved {count_moved} files.")
if __name__ == "__main__":
sort_detective()