|
1 | | -import { parse } from "https://cdn.skypack.dev/[email protected]?dts"; |
2 | | - |
3 | | -const r = await fetch("https://estruturaorganizacional.dados.gov.br/doc/estrutura-organizacional/completa.xml?codigoPoder=1&codigoEsfera=1&codigoUnidade=86144&retornarOrgaoEntidadeVinculados=SIM"); |
4 | | -if (!r.ok) throw r.status; |
5 | | -const xml = await r.text(); |
6 | | - |
7 | | -const obj = parse(xml, { ignoreAttributes: false, attributeNamePrefix: "", parseNodeValue: true }); |
8 | | -const unidades = obj.unidades.unidades ?? []; |
9 | | - |
10 | | -const u = unidades.map(x=>({ |
11 | | - s: x.sigla || "", |
12 | | - n: x.nome || "", |
13 | | - c: (x.competencia||"").replace(/\s+/g," "), |
14 | | - f: x.endereco?.uf||"", |
15 | | - m: x.endereco?.municipio||"", |
16 | | - z: x.endereco?.cep||"", |
17 | | - i: x.codigoUnidade.split("/").pop()||"", |
18 | | - p: x.codigoUnidadePai.split("/").pop()||"" |
19 | | -})); |
20 | | - |
21 | | -u.forEach(x=>{ |
22 | | - let S=[], y=x; |
23 | | - while(y){ S.unshift(y.s||y.n); y=u.find(z=>z.i===y.p); } |
24 | | - x.siglaCompleta = S.join("/"); |
25 | | - if(x.s.startsWith("NURAC")){ |
26 | | - const a=x.n.split(" "); |
27 | | - x.s=`NURAC ${a[6]||""} ${a[7]||""}`; |
| 1 | +const inputFile = Deno.args[0]; |
| 2 | +if (!inputFile) { |
| 3 | + console.error("❌ Arquivo de entrada não informado. Passe o arquivo JSON como argumento."); |
| 4 | + Deno.exit(1); |
| 5 | +} |
| 6 | + |
| 7 | +const outputFile = inputFile; // sobrescreve o mesmo arquivo |
| 8 | + |
| 9 | +interface Unidade { |
| 10 | + sigla?: string; |
| 11 | + nome?: string; |
| 12 | + competencia?: string; |
| 13 | + municipio?: string; |
| 14 | + cep?: string; |
| 15 | + uf?: string; |
| 16 | + codigoUnidade?: string; |
| 17 | + codigoUnidadePai?: string; |
| 18 | + [key: string]: any; |
| 19 | + siglaCompleta?: string; |
| 20 | +} |
| 21 | + |
| 22 | +function buildSiglaCompleta(u: Unidade, byCode: Record<string, Unidade>): string { |
| 23 | + const path: string[] = []; |
| 24 | + const seen = new Set<string>(); |
| 25 | + let cur: Unidade | undefined = u; |
| 26 | + while (cur && cur.codigoUnidade && !seen.has(cur.codigoUnidade)) { |
| 27 | + seen.add(cur.codigoUnidade); |
| 28 | + path.unshift(cur.sigla || cur.nome || ""); |
| 29 | + cur = byCode[cur.codigoUnidadePai ?? ""]; |
28 | 30 | } |
29 | | -}); |
| 31 | + return path.filter(Boolean).join("/"); |
| 32 | +} |
| 33 | + |
| 34 | +async function processar() { |
| 35 | + const raw = await Deno.readTextFile(inputFile); |
| 36 | + const unidades: Unidade[] = JSON.parse(raw); |
30 | 37 |
|
31 | | -const header = ["Sigla","Sigla Completa","Nome","Competencia","UF","Municipio","CEP","CodigoUnidade","CodigoUnidadePai"].join("\t"); |
32 | | -const rows = u.map(x=>[x.s,x.siglaCompleta,x.n,x.c,x.f,x.m,x.z,x.i,x.p].join("\t")).join("\n"); |
| 38 | + const byCode: Record<string, Unidade> = {}; |
| 39 | + unidades.forEach(u => { |
| 40 | + if (u.codigoUnidade) byCode[u.codigoUnidade] = u; |
| 41 | + }); |
33 | 42 |
|
34 | | -await Deno.writeTextFile("anac-estrutura.tsv", `${header}\n${rows}`); |
| 43 | + const result = unidades.map(u => { |
| 44 | + let sigla = u.sigla ?? ""; |
| 45 | + let siglaCompleta = buildSiglaCompleta(u, byCode); |
| 46 | + |
| 47 | + // Ajuste NURAC |
| 48 | + if (sigla.toUpperCase().startsWith("NURAC")) { |
| 49 | + const partes = (u.nome ?? "").split(" "); |
| 50 | + const cidade = partes[6] ?? ""; |
| 51 | + const estado = partes[7] ?? ""; |
| 52 | + sigla = `NURAC ${cidade} ${estado}`; |
| 53 | + const pcs = siglaCompleta.split("/"); |
| 54 | + if (pcs.length >= 3) pcs[pcs.length - 1] = sigla; |
| 55 | + siglaCompleta = pcs.join("/"); |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + ...u, |
| 60 | + sigla, |
| 61 | + siglaCompleta, |
| 62 | + }; |
| 63 | + }); |
| 64 | + |
| 65 | + await Deno.writeTextFile(outputFile, JSON.stringify(result, null, 2)); |
| 66 | + console.log(`💾 JSON pós-processado salvo: ${outputFile} (${result.length} registros)`); |
| 67 | +} |
| 68 | + |
| 69 | +processar().catch(err => { |
| 70 | + console.error("❌ Erro no pós-processamento:", err); |
| 71 | + Deno.exit(1); |
| 72 | +}); |
0 commit comments