feat: banatie script
This commit is contained in:
+46
-28
@@ -1,58 +1,76 @@
|
||||
import { writeFileSync, mkdirSync } from 'fs';
|
||||
import { writeFileSync, mkdirSync, readFileSync } from 'fs';
|
||||
import { resolve, dirname } from 'path';
|
||||
|
||||
const API_ENDPOINT = process.env.BANATIE_API_URL || 'https://api.banatie.com/v1/generate';
|
||||
const API_KEY = process.env.BANATIE_API_KEY || '';
|
||||
const envPath = resolve(dirname(new URL(import.meta.url).pathname), '../../.env');
|
||||
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) {
|
||||
console.error('BANATIE_API_KEY environment variable is not set');
|
||||
console.error('BANATIE_KEY environment variable is not set');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const preset = PRESETS[type];
|
||||
if (!preset) {
|
||||
console.error(`Unknown type: ${type}. Use "background" or "icon".`);
|
||||
process.exit(1);
|
||||
}
|
||||
const body = { prompt, aspectRatio };
|
||||
|
||||
const response = await fetch(API_ENDPOINT, {
|
||||
console.log(`Generating: "${prompt}" (${body.aspectRatio})...`);
|
||||
|
||||
const response = await fetch(`${API_BASE}/generations`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${API_KEY}`,
|
||||
'X-API-Key': API_KEY,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
prompt,
|
||||
...preset,
|
||||
}),
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const body = await response.text();
|
||||
console.error(`API error ${response.status}: ${body}`);
|
||||
const text = await response.text();
|
||||
console.error(`API error ${response.status}: ${text}`);
|
||||
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);
|
||||
mkdirSync(dirname(outputPath), { recursive: true });
|
||||
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) {
|
||||
const result = {};
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--type') result.type = args[++i];
|
||||
else if (args[i] === '--prompt') result.prompt = args[++i];
|
||||
if (args[i] === '--prompt') result.prompt = args[++i];
|
||||
else if (args[i] === '--output') result.output = args[++i];
|
||||
else if (args[i] === '--aspect-ratio') result.aspectRatio = args[++i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -61,6 +79,6 @@ const args = parseArgs(process.argv.slice(2));
|
||||
if (args.prompt && args.output) {
|
||||
generateImage(args);
|
||||
} 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user