// Ermittelt amtliche Gemeinden ohne Website in der Liste und sucht sie in Wikidata ohne Typfilter. // Ergebnis: daten/luecken.json import { readFileSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const hier = dirname(fileURLToPath(import.meta.url)); const jsonl = (d) => readFileSync(join(hier, 'daten', d), 'utf8').trim().split('\n').map((z) => JSON.parse(z)); const amtlich = jsonl('destatis-gemeinden.jsonl'); const wiki = new Map(jsonl('gemeinden.jsonl').map((g) => [g.ags, g])); const fehlend = amtlich.filter((g) => !wiki.get(g.ags)?.host); const abfrage = `SELECT ?item ?ags ?website WHERE { VALUES ?ags { ${fehlend.map((g) => `"${g.ags}"`).join(' ')} } ?item wdt:P439 ?ags . OPTIONAL { ?item wdt:P856 ?website } }`; const antwort = await fetch('https://query.wikidata.org/sparql', { method: 'POST', headers: { Accept: 'application/sparql-results+json', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'municipal-dns-study/0.1', }, body: new URLSearchParams({ query: abfrage }), signal: AbortSignal.timeout(60000), }); if (!antwort.ok) throw new Error(`Wikidata ${antwort.status}`); const treffer = (await antwort.json()).results.bindings; const ergebnis = fehlend.map((g) => { const zu = treffer.filter((t) => t.ags.value === g.ags); return { ags: g.ags, land: g.land, name: g.name, einwohner: g.einwohner, qid: zu[0]?.item.value.split('/').pop() ?? null, websites: [...new Set(zu.map((t) => t.website?.value).filter(Boolean))], }; }); writeFileSync(join(hier, 'daten', 'luecken.json'), JSON.stringify(ergebnis, null, 1)); console.log(`Lücken: ${ergebnis.length}, davon mit Website in Wikidata: ${ergebnis.filter((e) => e.websites.length).length}`); for (const e of ergebnis) console.log(e.ags, e.name, e.einwohner, e.qid ?? '-', e.websites.join(' ') || '(keine)');