diff --git a/scripts/fake-listings.mjs b/scripts/fake-listings.mjs new file mode 100644 index 0000000..5844e6d --- /dev/null +++ b/scripts/fake-listings.mjs @@ -0,0 +1,108 @@ +// Publishes invented listings to a node you own, for testing the search, the +// map of cells and the owner pages with a realistic amount of data. +// +// node scripts/fake-listings.mjs --url http://127.0.0.1:18200 --token $(cat DIR/admin.token) +// +// Every description says it is invented, so nobody mistakes them for offers. +// The same --seed gives the same listings. Options: +// --count 100 --lat 50.59 --lon 6.65 (Mechernich) --spread-km 12 --seed 1 +import { parseArgs } from "node:util"; +import { encodeGeohash } from "../web/src/geohash.ts"; + +const { values: o } = parseArgs({ + options: { + url: { type: "string" }, + token: { type: "string" }, + count: { type: "string", default: "100" }, + lat: { type: "string", default: "50.59" }, + lon: { type: "string", default: "6.65" }, + "spread-km": { type: "string", default: "12" }, + seed: { type: "string", default: "1" }, + }, +}); +if (!o.url || !o.token) { + console.error("usage: node scripts/fake-listings.mjs --url http://127.0.0.1:18200 --token [--count 100] [--lat 50.59 --lon 6.65] [--spread-km 12] [--seed 1]"); + process.exit(2); +} +const count = Number(o.count), lat0 = Number(o.lat), lon0 = Number(o.lon), spread = Number(o["spread-km"]); + +// A small seeded generator (mulberry32): the same seed, the same data. +let state = Number(o.seed) >>> 0; +const rnd = () => { + state = (state + 0x6d2b79f5) >>> 0; + let t = state; + t = Math.imul(t ^ (t >>> 15), t | 1); + t ^= t + Math.imul(t ^ (t >>> 7), t | 61); + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; +}; +const between = (a, b) => a + Math.floor(rnd() * (b - a + 1)); +const pick = (xs) => xs[Math.floor(rnd() * xs.length)]; +const weighted = (pairs) => { + let r = rnd() * pairs.reduce((s, [, w]) => s + w, 0); + for (const [v, w] of pairs) if ((r -= w) < 0) return v; + return pairs[0][0]; +}; + +const PLACES = ["Mechernich", "Kommern", "Satzvey", "Vussem", "Strempt", "Lessenich", "Antweiler", "Bleibuir", "Firmenich", "Holzheim", "Kallmuth", "Eiserfey", "Weyer", "Harzheim", "Roggendorf", "Floisdorf", "Obergartzem", "Katzvey", "Berg", "Breitenbenden"]; +const FEATURES = ["Balkon", "Terrasse", "Garten", "Garage", "Stellplatz", "Keller", "Aufzug", "Einbauküche", "Fußbodenheizung", "Neubau", "Altbau", "Barrierefrei", "Haustiere erlaubt", "Photovoltaik", "Kamin", "Dachboden", "Gäste-WC"]; +const NOUN = { apartment: "Wohnung", house: "Haus", room: "Zimmer", commercial: "Gewerbefläche" }; +const MOOD = ["Helle", "Ruhige", "Gepflegte", "Moderne", "Gemütliche", "Sanierte", "Geräumige", "Zentrale", "Sonnige", "Familienfreundliche"]; + +// One point within the spread, as a uniform spot on a disc, in degrees. +function place() { + const r = spread * Math.sqrt(rnd()), a = rnd() * 2 * Math.PI; + return { lat: lat0 + (r * Math.cos(a)) / 111, lon: lon0 + (r * Math.sin(a)) / (111 * Math.cos((lat0 * Math.PI) / 180)) }; +} + +function draft() { + const category = weighted([["apartment", 55], ["house", 20], ["room", 15], ["commercial", 10]]); + const kind = category === "room" ? "rent" : weighted([["rent", 70], ["sale", 30]]); + const [area, rooms] = { + apartment: [between(30, 120), between(2, 8)], + house: [between(90, 220), between(6, 14)], + room: [between(10, 30), 2], + commercial: [between(30, 300), between(2, 12)], + }[category]; + // Round prices to what people actually ask: 10 EUR a month, 1000 EUR for a sale. + const euros = kind === "rent" + ? Math.round(area * { apartment: 9.5, house: 8, room: 18, commercial: 11 }[category] * (0.8 + rnd() * 0.5) / 10) * 10 + : Math.round(area * { apartment: 2600, house: 2900, room: 0, commercial: 2100 }[category] * (0.7 + rnd() * 0.6) / 1000) * 1000; + const where = pick(PLACES); + const features = [...new Set(Array.from({ length: between(0, 5) }, () => pick(FEATURES)))]; + const roomsText = category === "room" ? "" : `${rooms % 2 ? (rooms / 2).toString().replace(".", ",") : rooms / 2}-Zimmer-`; + const p = place(); + return { + kind, category, + geohash: encodeGeohash(p.lat, p.lon, 6), + price: { amount_cents: euros * 100, currency: "EUR", period: kind === "rent" ? "month" : "once" }, + area_m2: area, + rooms_x2: rooms, + features, + title: `${pick(MOOD)} ${roomsText}${NOUN[category]} in ${where}${kind === "sale" ? " zu verkaufen" : ""}`, + description: `${area} m² in ${where}. ${features.length ? "Ausstattung: " + features.join(", ") + "." : "Schlichte Ausstattung."}\n\nTestanzeige: Diese Anzeige ist erfunden und kein echtes Angebot.`, + expires_in_days: 30, + }; +} + +let done = 0; +const failed = []; +for (let i = 1; i <= count; i++) { + const d = draft(); + try { + const res = await fetch(`${o.url}/api/v1/listings`, { + method: "POST", + headers: { Authorization: `Bearer ${o.token}`, "Content-Type": "application/json" }, + body: JSON.stringify(d), + }); + if (!res.ok) throw new Error(`${res.status} ${await res.text()}`); + done++; + } catch (e) { + failed.push(`#${i} ${d.title}: ${e.message}`); + } + if (i % 25 === 0 || i === count) console.log(`${i}/${count} sent, ${failed.length} refused`); +} +console.log(`published ${done} of ${count} listings`); +if (failed.length) { + console.error(failed.slice(0, 10).join("\n")); + process.exit(1); +}