feat: banatie script

This commit is contained in:
Oleg Proskurin 2026-02-24 00:21:54 +07:00
parent 7a6bd81d9e
commit 5a854479f9
2 changed files with 59 additions and 32 deletions

View File

@ -150,12 +150,21 @@ When generating HTML worksheets:
## Banatie API ## Banatie API
REST API for generating images. REST API at `https://api.banatie.app` for generating images. Auth via `X-API-Key` header (reads `BANATIE_KEY` from `.env`).
- **Backgrounds:** ~1200×1700px, themed illustrations (forest, space, ocean, etc.) **POST `/api/v1/generations`** — create generation:
- **Icons:** 128×128px, transparent PNG, simple collectible items (stars, gems, animals) - `prompt` (string, required) — image description
- `aspectRatio``1:1`, `16:9`, `9:16`, `3:2`, `21:9` (default: `1:1`)
- `referenceImages` (string[]) — image IDs as references
- `autoEnhance` (boolean) — prompt enhancement (default: true)
Configuration is in `src/scripts/banatie.mjs`. Set the `BANATIE_API_KEY` environment variable for authentication. Response returns JSON with `data.outputImage.storageUrl` (CDN URL to download).
Script `src/scripts/banatie.mjs` wraps the API with presets:
- `background``aspectRatio: "9:16"` (vertical, for page backgrounds)
- `icon``aspectRatio: "1:1"` (square, for problem card icons)
Rate limit: 100 requests/hour per API key.
## Background Removal ## Background Removal

View File

@ -1,58 +1,76 @@
import { writeFileSync, mkdirSync } from 'fs'; import { writeFileSync, mkdirSync, readFileSync } from 'fs';
import { resolve, dirname } from 'path'; import { resolve, dirname } from 'path';
const API_ENDPOINT = process.env.BANATIE_API_URL || 'https://api.banatie.com/v1/generate'; const envPath = resolve(dirname(new URL(import.meta.url).pathname), '../../.env');
const API_KEY = process.env.BANATIE_API_KEY || ''; try {
const envContent = readFileSync(envPath, 'utf-8');
for (const line of envContent.split('\n')) {
const match = line.match(/^([^#=]+)=(.*)$/);
if (match && !process.env[match[1].trim()]) {
process.env[match[1].trim()] = match[2].trim();
}
}
} catch {}
const PRESETS = {
background: { width: 1200, height: 1700, format: 'png' },
icon: { width: 128, height: 128, format: 'png', transparent: true },
};
export async function generateImage({ type = 'icon', prompt, output }) { const API_BASE = 'https://api.banatie.app/api/v1';
const API_KEY = process.env.BANATIE_KEY || '';
export async function generateImage({ prompt, output, aspectRatio = '1:1' }) {
if (!API_KEY) { if (!API_KEY) {
console.error('BANATIE_API_KEY environment variable is not set'); console.error('BANATIE_KEY environment variable is not set');
process.exit(1); process.exit(1);
} }
const preset = PRESETS[type]; const body = { prompt, aspectRatio };
if (!preset) {
console.error(`Unknown type: ${type}. Use "background" or "icon".`);
process.exit(1);
}
const response = await fetch(API_ENDPOINT, { console.log(`Generating: "${prompt}" (${body.aspectRatio})...`);
const response = await fetch(`${API_BASE}/generations`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`, 'X-API-Key': API_KEY,
}, },
body: JSON.stringify({ body: JSON.stringify(body),
prompt,
...preset,
}),
}); });
if (!response.ok) { if (!response.ok) {
const body = await response.text(); const text = await response.text();
console.error(`API error ${response.status}: ${body}`); console.error(`API error ${response.status}: ${text}`);
process.exit(1); process.exit(1);
} }
const buffer = Buffer.from(await response.arrayBuffer()); const result = await response.json();
if (!result.success) {
console.error(`Generation failed:`, result.error);
process.exit(1);
}
const imageUrl = result.data.outputImage.storageUrl;
console.log(`Downloading from ${imageUrl}...`);
const imageResponse = await fetch(imageUrl);
if (!imageResponse.ok) {
console.error(`Failed to download image: ${imageResponse.status}`);
process.exit(1);
}
const buffer = Buffer.from(await imageResponse.arrayBuffer());
const outputPath = resolve(output); const outputPath = resolve(output);
mkdirSync(dirname(outputPath), { recursive: true }); mkdirSync(dirname(outputPath), { recursive: true });
writeFileSync(outputPath, buffer); writeFileSync(outputPath, buffer);
console.log(`Image saved: ${outputPath}`);
return outputPath; console.log(`Image saved: ${outputPath} (${result.data.outputImage.width}x${result.data.outputImage.height})`);
return { path: outputPath, generation: result.data };
} }
function parseArgs(args) { function parseArgs(args) {
const result = {}; const result = {};
for (let i = 0; i < args.length; i++) { for (let i = 0; i < args.length; i++) {
if (args[i] === '--type') result.type = args[++i]; if (args[i] === '--prompt') result.prompt = args[++i];
else if (args[i] === '--prompt') result.prompt = args[++i];
else if (args[i] === '--output') result.output = args[++i]; else if (args[i] === '--output') result.output = args[++i];
else if (args[i] === '--aspect-ratio') result.aspectRatio = args[++i];
} }
return result; return result;
} }
@ -61,6 +79,6 @@ const args = parseArgs(process.argv.slice(2));
if (args.prompt && args.output) { if (args.prompt && args.output) {
generateImage(args); generateImage(args);
} else if (process.argv.length > 2) { } else if (process.argv.length > 2) {
console.error('Usage: node banatie.mjs --type <background|icon> --prompt "<description>" --output <path>'); console.error('Usage: node banatie.mjs --prompt "<description>" --output <path> [--aspect-ratio <ratio>]');
process.exit(1); process.exit(1);
} }