// Holt alle deutschen Gemeinden mit amtlichem Gemeindeschlüssel und Website aus Wikidata (CC0). // Nur lesend. Ergebnis: daten/wikidata-roh.json import { mkdirSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const hier = dirname(fileURLToPath(import.meta.url)); const ziel = join(hier, 'daten', 'wikidata-roh.json'); const abfrage = ` SELECT ?item ?name ?ags ?website WHERE { ?item p:P439 ?agsAussage . ?agsAussage ps:P439 ?ags . FILTER NOT EXISTS { ?agsAussage pq:P582 ?agsEnde } FILTER NOT EXISTS { ?agsAussage wikibase:rank wikibase:DeprecatedRank } ?item p:P31 ?typAussage . ?typAussage ps:P31/wdt:P279* wd:Q262166 . FILTER NOT EXISTS { ?typAussage pq:P582 ?typEnde } FILTER NOT EXISTS { ?typAussage wikibase:rank wikibase:DeprecatedRank } FILTER NOT EXISTS { ?item wdt:P576 ?aufgeloest } FILTER NOT EXISTS { ?item wdt:P1366 ?nachfolger } OPTIONAL { ?item rdfs:label ?name . FILTER(LANG(?name) = "de") } 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(120000), }); if (!antwort.ok) { console.error('Wikidata antwortet', antwort.status, (await antwort.text()).slice(0, 300)); process.exit(1); } const json = await antwort.json(); const zeilen = json.results.bindings.map((b) => ({ qid: b.item.value.split('/').pop(), name: b.name?.value ?? null, ags: b.ags.value, website: b.website?.value ?? null, })); mkdirSync(dirname(ziel), { recursive: true }); writeFileSync(ziel, JSON.stringify(zeilen)); const gueltig = zeilen.filter((z) => /^\d{8}$/.test(z.ags)); const proAgs = new Map(); for (const z of gueltig) { if (!proAgs.has(z.ags)) proAgs.set(z.ags, []); proAgs.get(z.ags).push(z); } const mitSite = [...proAgs.values()].filter((l) => l.some((z) => z.website)).length; const mehrereSites = [...proAgs.values()].filter((l) => new Set(l.map((z) => z.website).filter(Boolean)).size > 1).length; const mehrereItems = [...proAgs.values()].filter((l) => new Set(l.map((z) => z.qid)).size > 1).length; const land = {}; for (const [ags, l] of proAgs) { const k = ags.slice(0, 2); land[k] ??= { gemeinden: 0, mitWebsite: 0 }; land[k].gemeinden++; if (l.some((z) => z.website)) land[k].mitWebsite++; } console.log(JSON.stringify({ zeilen: zeilen.length, ungueltigeAgs: zeilen.length - gueltig.length, gemeinden: proAgs.size, mitWebsite: mitSite, mehrereWebsites: mehrereSites, mehrereWikidataEintraege: mehrereItems, land, }, null, 1));