Bot version

This commit is contained in:
Mike Lynch 2025-03-08 17:15:28 +11:00
parent 10f4f0b2c6
commit 4decaaf156

View File

@ -19,65 +19,59 @@ const MAG = 2;
const WIDTH = 20;
const HEIGHT = WIDTH;
function poptimal_svg() {
function randomise_params() {
const palette_name = random.choice(Array.from(PALETTES.keys()));
const palette_fn = PALETTES.get(palette_name);
const palette = palette_fn();
const patterns = [1,2].map((n) => { return {
i: n,
colour: palette[n],
m: random.choice([1, 2, 3, 4, 5]),
n: random.choice([1, 2, 3, 4, 5]),
f: random.choice(RADIUS_OPTS),
r: random.float(0, 0.4),
}});
return {
background: palette[0],
patterns: patterns
}
}
function poptimal_svg(width, params) {
const window = new JSDOM().window;
const document = window.document;
const container = d3.select(document.body).append("div");
const dm = new DotMaker(WIDTH);
const m1 = random.choice([1, 2, 3, 4, 5]);
const n1 = random.choice([1, 2, 3, 4, 5]);
const m2 = random.choice([1, 2, 3, 4, 5]);
const n2 = random.choice([1, 2, 3, 4, 5]);
const palette_fn = random.choice(Array.from(PALETTES.values()));
const palette = palette_fn();
const bg = palette[0];
const fg1 = palette[1];
const fg2 = palette[2];
const f1 = random.choice(RADIUS_OPTS);
const f2 = random.choice(RADIUS_OPTS);
const r1 = random.float(0, 0.4);
const r2 = random.float(0, 0.4);
const dots1 = dm.dots(1 / m1, n1);
const dots2 = dm.dots(1 / m2, n2);
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("fill", bg);
.attr("width", width)
.attr("height", width)
.attr("fill", params.background);
const dots_g1 = svg.append("g")
.attr("id", "dots1");
const p1 = params.patterns[0];
const p2 = params.patterns[1];
dots_g1.selectAll("circle")
.data(dots1)
const dots = dm.dots(1 / p1.m, p1.n);
const dots_g = svg.append("g")
.attr("id", `$dots{p1.i}`);
dots_g.selectAll("circle")
.data(dots)
.join("circle")
.attr("r", (d) => dm.radius(d, f1, r1))
.attr("fill", fg1)
.attr("r", (d) => dm.radius(d, p1.f, p1.r))
.attr("fill", p1.colour)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y);
const dots_g2 = svg.append("g")
.attr("id", "dots2");
dots_g2.selectAll("circle")
.data(dots2)
.join("circle")
.attr("r", (d) => dm.radius(d, f2, r2))
.attr("fill", fg2)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y);
const node = svg.node();
node.setAttributeNS(xmlns, "xmlns", svgns);
@ -87,13 +81,51 @@ function poptimal_svg() {
}
async function post_image(image, alt_text, cf) {
const status_url = `${cf.base_url}/api/v1/statuses`;
const media_url = `${cf.base_url}/api/v1/media`;
const headers = {
'Authorization': `Bearer ${cf.access_token}`,
};
const rawData = await promises.readFile(image);
const blob = new Blob([rawData]);
const fd = new FormData();
fd.set('description', alt_text);
fd.set('file', blob, image, 'text/png');
const resp = await fetch(media_url, {
method: 'POST',
headers: headers,
body: fd
});
const bodyjson = await resp.text();
const response = JSON.parse(bodyjson);
const media_id = response["id"];
headers['Accept'] = 'application/json';
headers['Content-Type'] = 'application/json';
const resp2 = await fetch(status_url, {
method: 'POST',
headers: headers,
body: JSON.stringify({ media_ids: [ media_id ] })
});
const bodyjson2 = await resp2.text();
console.log(bodyjson2);
}
async function main() {
const argv = yargs(hideBin(process.argv))
.usage("Usage: -w WIDTH -o OUTPUT_PNG")
.usage("Usage: -w WIDTH -o OUTPUT_PNG -g GOTOSOCIAL_CONFIG")
.default('w', 1200)
.default('o', 'poptimal.png').argv;
.default('o', 'poptimal.png')
.default('g', 'config.json').argv;
const svg = poptimal_svg(argv.w);
const params = randomise_params();
console.log(JSON.stringify(params));
const svg = poptimal_svg(argv.w, params);
const opts = {
background: 'rgba(255, 255, 255, 1.0)',
fitTo: {
@ -101,11 +133,16 @@ async function main() {
value: argv.w,
},
};
const resvg = new Resvg(svg, opts)
const pngData = resvg.render()
const pngBuffer = pngData.asPng()
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);
// }
}