-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
115 lines (103 loc) · 3.63 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict'
const xlsx = require('xlsx')
const senadoresBase = require('senadores-base')
const fs = require('fs')
function addGasto (gasto, rut, results) {
let senatorIndex = -1
results.forEach((res, i) => {
if (res.senador.rut.toUpperCase() === rut.toUpperCase()) senatorIndex = i
})
// For those candidates who lose elections
if (senatorIndex === -1) return
let gastos = results[senatorIndex].elecciones.gastos
? results[senatorIndex].elecciones.gastos
: { detalle: [], total: 0 }
gastos.detalle.push(gasto)
gastos.total += gasto.monto
results[senatorIndex].elecciones = Object.assign(results[senatorIndex].elecciones, { gastos })
}
function addIngreso (ingreso, rut, results) {
let senatorIndex = -1
results.forEach((res, i) => {
if (res.senador.rut.toUpperCase() === rut.toUpperCase()) senatorIndex = i
})
// For those candidates who lose elections
if (senatorIndex === -1) return
let ingresos = results[senatorIndex].elecciones.ingresos
? results[senatorIndex].elecciones.ingresos
: { detalle: [], total: 0 }
ingresos.detalle.push(ingreso)
ingresos.total += ingreso.monto
results[senatorIndex].elecciones = Object.assign(results[senatorIndex].elecciones, { ingresos })
}
function getIngresos (results) {
let workbook = xlsx.readFile('data/DETALLE_INGRESOS_CANDIDATOS_ELECCION2013.xls')
let sheetName = workbook.SheetNames[0]
let sheet = workbook.Sheets[sheetName]
for (let cell in sheet) {
if (cell[0] !== 'A') continue
if (sheet[cell].v !== 'SENADOR') continue
let i = cell.slice(1)
let rut = sheet[`G${i}`].v
let ingresos = {
estado: sheet[`F${i}`].v,
proveedor: {
rut: sheet[`N${i}`].v,
nombre: sheet[`O${i}`].v
},
fecha: sheet[`P${i}`] ? sheet[`P${i}`].v : '',
documento: {
tipo: sheet[`Q${i}`] ? sheet[`Q${i}`].v : '',
descripcion: sheet[`R${i}`] ? sheet[`R${i}`].v : '',
numero: sheet[`S${i}`] ? sheet[`S${i}`].v : ''
},
descripcion: sheet[`U${i}`].v,
glosa: sheet[`V${i}`] ? sheet[`V${i}`].v : '',
monto: sheet[`W${i}`].v
}
// Now, find that senator, and create a new 'ingresos' property, and/or add
// the result to that property
addIngreso(ingresos, rut, results)
}
}
function getGastos (results) {
let workbook = xlsx.readFile('data/DETALLE_GASTOS_CANDIDATOS_ELECCION2013.xls')
let sheetName = workbook.SheetNames[0]
let sheet = workbook.Sheets[sheetName]
for (let cell in sheet) {
if (cell[0] !== 'A') continue
if (sheet[cell].v !== 'SENADOR') continue
let i = cell.slice(1)
let rut = sheet[`G${i}`].v
let gastos = {
estado: sheet[`F${i}`].v,
proveedor: {
rut: sheet[`N${i}`].v,
nombre: sheet[`O${i}`].v
},
fecha: sheet[`P${i}`] ? sheet[`P${i}`].v : '',
documento: {
tipo: sheet[`Q${i}`] ? sheet[`Q${i}`].v : '',
descripcion: sheet[`R${i}`] ? sheet[`R${i}`].v : '',
numero: sheet[`S${i}`] ? sheet[`S${i}`].v : ''
},
descripcion: sheet[`U${i}`].v,
glosa: sheet[`V${i}`] ? sheet[`V${i}`].v : '',
monto: sheet[`W${i}`].v
}
// Now, find that senator, and create a new 'ingresos' property, and/or add
// the result to that property
addGasto(gastos, rut, results)
}
}
;(function () {
let results = senadoresBase().map(senador => {
return { senador, elecciones: {}, representacion: {} }
})
getIngresos(results)
getGastos(results)
fs.writeFile('senadores-elecciones.json', JSON.stringify(results, null, 2), 'utf8', err => {
if (err) throw err
console.log('It\'s saved!')
})
})()