prodia-scripts/src/index.ts

60 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-10-25 13:48:42 +08:00
#!node
2024-10-25 13:34:26 +08:00
import { program } from 'commander';
import * as fs from 'fs/promises';
2024-10-25 14:23:29 +08:00
import { awaitProdiaJob, defaultProdiaParams, fetchProdia } from './prodia';
2024-10-25 13:34:26 +08:00
program
.name('prodia-scripts')
.description('cli for prodia usage')
.version('0.1.0');
program
.command('sdxl')
.description('generate an image with sdxl')
2024-10-25 13:48:42 +08:00
.argument('[config]', 'path to the config json', './sdxl.gen.json')
.option('--init', 'create a default sdxl.gen.json')
2024-10-25 16:04:17 +08:00
.option('--append [where]', 'saves generated url to a text file', './generated.txt')
2024-10-25 14:23:29 +08:00
.option('--queue [amount]', 'queues jobs without waiting for them')
2024-10-25 13:48:42 +08:00
.action(async (config, flags) => {
if (flags.init) {
console.log('Creating sdxl.gen.json...');
await fs.writeFile(
'./sdxl.gen.json',
JSON.stringify(defaultProdiaParams, null, 4)
);
return;
} else {
console.log(`Reading config=${config}...`);
const txt = await fs.readFile(config, { encoding: 'utf8' });
const json = JSON.parse(txt);
console.log(`Fetching prodia api...`);
2024-10-25 14:23:29 +08:00
if (flags.queue) {
const queue = parseInt(flags.queue);
if (isNaN(queue) || queue < 1)
throw new Error(`invalid queue number=${flags.queue}`);
for (let i = 0; i < queue; i++) {
console.log(`Fetching ${i + 1}/${queue}...`);
const job = await fetchProdia(json);
2024-10-25 16:04:17 +08:00
const url = `https://images.prodia.xyz/${job.job}.png`;
2024-10-25 14:23:29 +08:00
if (flags.append)
2024-10-25 16:04:17 +08:00
await fs.appendFile(flags.append, url + '\n');
console.log(url);
2024-10-25 14:23:29 +08:00
}
console.log('Done!');
} else {
const job = await fetchProdia(json);
const status = await awaitProdiaJob(job);
console.log('Done: ', status);
if (status.imageUrl && flags.append) {
await fs.appendFile(flags.append, `${status.imageUrl}\n`);
}
}
2024-10-25 13:48:42 +08:00
}
2024-10-25 13:34:26 +08:00
});
2024-10-25 13:48:42 +08:00
program.parse();