Closed s2ward closed 4 months ago
import json import os from pathlib import Path def sanitize_path(path): return path.replace(' ', '_') def create_book_files(data): for book in data: name = sanitize_path(book.get('name', 'Unknown')) text = book.get('text', '') locations = book.get('locations-wip', []) for location in locations: mainarea = sanitize_path(location.get('mainarea', 'Unknown')) area = sanitize_path(location.get('area', 'Unknown')) subarea = sanitize_path(location.get('subarea', '')) # Create folder structure folder_path = Path('data/books/text') / mainarea / area / subarea folder_path.mkdir(parents=True, exist_ok=True) # Create file name file_name = f"{name}.txt" file_path = folder_path / file_name # Write text to file with open(file_path, 'w', encoding='utf-8') as f: f.write(text) print(f"Created file: {file_path}") # Load JSON data with open('data/books/book_database.json', 'r') as f: data = json.load(f) # Create book files create_book_files(data)