Command-line script is now a gotosocial bot

This commit is contained in:
Mike Lynch 2025-03-08 17:34:37 +11:00
parent 4decaaf156
commit 270bcc9cfa

View File

@ -33,44 +33,45 @@ function randomise_params() {
}});
return {
background: palette[0],
palette: palette_name,
patterns: patterns
}
}
function poptimal_svg(width, params) {
function poptimal_svg(params) {
const window = new JSDOM().window;
const document = window.document;
const container = d3.select(document.body).append("div");
const dm = new DotMaker(width);
const dm = new DotMaker(WIDTH);
const svg = container.append("svg")
.attr("width", width * CELL * MAG)
.attr("width", WIDTH * CELL * MAG)
.attr("height", HEIGHT * CELL * MAG)
.attr("viewBox", [ 0, 0, width, HEIGHT ]);
.attr("viewBox", [ 0, 0, WIDTH, HEIGHT ]);
const background = svg.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", width)
.attr("height", width)
.attr("width", WIDTH)
.attr("height", WIDTH)
.attr("fill", params.background);
const p1 = params.patterns[0];
const p2 = params.patterns[1];
const dots = dm.dots(1 / p1.m, p1.n);
const dots_g = svg.append("g")
.attr("id", `$dots{p1.i}`);
params.patterns.map((p) => {
const dots = dm.dots(1 / p.m, p.n);
const dots_g = svg.append("g")
.attr("id", `dots${p.i}`);
dots_g.selectAll("circle")
.data(dots)
.join("circle")
.attr("r", (d) => dm.radius(d, p1.f, p1.r))
.attr("fill", p1.colour)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y);
dots_g.selectAll("circle")
.data(dots)
.join("circle")
.attr("r", (d) => dm.radius(d, p.f, p.r))
.attr("fill", p.colour)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y);
});
const node = svg.node();
@ -117,32 +118,37 @@ async function post_image(image, alt_text, cf) {
async function main() {
const argv = yargs(hideBin(process.argv))
.usage("Usage: -w WIDTH -o OUTPUT_PNG -g GOTOSOCIAL_CONFIG")
.default('w', 1200)
.default('o', 'poptimal.png')
.usage("Usage: -s SIZE -o output.png -c config.json")
.default('s', 1200)
.default('g', 'config.json').argv;
const params = randomise_params();
console.log(JSON.stringify(params));
const cfjson = await promises.readFile(argv.g);
const cf = JSON.parse(cfjson);
const svg = poptimal_svg(argv.w, params);
const fn = argv.o || String(Date.now()) + '.png';
const imgfile = cf['working_dir'] + '/' + fn;
const params = randomise_params();
const alt_text = JSON.stringify(params);
const svg = poptimal_svg(params);
const opts = {
background: 'rgba(255, 255, 255, 1.0)',
fitTo: {
mode: 'width',
value: argv.w,
value: argv.s,
},
};
const resvg = new Resvg(svg, opts);
const pngData = resvg.render();
const pngBuffer = pngData.asPng();
await promises.writeFile(argv.o, pngBuffer);
// if( argv.g ) {
// const cfjson = await promises.readFile(argv.g);
// const cf = JSON.parse(cfjson);
// await post_image(argv.o, "An abstract pattern", cf);
// }
await promises.writeFile(imgfile, pngBuffer);
if( cf['base_url'] ) {
await post_image(imgfile, alt_text, cf);
}
}