open-banking-chile 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kai Horwitz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,371 @@
1
+ # Open Banking Chile
2
+
3
+ Scrapers open source para bancos chilenos. Obtén tus movimientos bancarios y saldo como JSON limpio.
4
+
5
+ > **Disclaimer**: Este proyecto no está afiliado con ningún banco. Úsalo bajo tu propia responsabilidad y solo con tus propias credenciales.
6
+
7
+ ## Migración v1 → v2
8
+
9
+ **v2.0.0 introduce un cambio breaking en la interfaz `BankMovement`:**
10
+
11
+ El campo `source` ahora es **obligatorio** e indica el origen del movimiento:
12
+
13
+ | Valor | Descripción |
14
+ | ------------------------ | --------------------------------- |
15
+ | `"account"` | Cuenta corriente o vista |
16
+ | `"credit_card_unbilled"` | Tarjeta de crédito — por facturar |
17
+ | `"credit_card_billed"` | Tarjeta de crédito — facturado |
18
+
19
+ Si construyes objetos `BankMovement` manualmente en tu código, agrega el campo `source`. Si solo **consumes** los resultados del scraper, no hay cambios necesarios — todos los scrapers ya lo incluyen.
20
+
21
+ ```ts
22
+ // v1 (ya no válido en TypeScript)
23
+ const mov: BankMovement = { date, description, amount, balance };
24
+
25
+ // v2
26
+ const mov: BankMovement = {
27
+ date,
28
+ description,
29
+ amount,
30
+ balance,
31
+ source: "account",
32
+ };
33
+ ```
34
+
35
+ También en esta versión: utilidades compartidas (`parseChileanAmount`, `normalizeDate`, `deduplicateMovements`, etc.) disponibles como exports desde `open-banking-chile/utils`.
36
+
37
+ ## Bancos soportados
38
+
39
+ | Banco | ID | Estado |
40
+ | --------------------------------- | ------------ | ------------ |
41
+ | Banco Falabella (cuenta + CMR TC) | `falabella` | ✅ Funcional |
42
+ | Banco BICE | `bice` | ✅ Funcional |
43
+ | Banco Santander | `santander` | ✅ Funcional |
44
+ | Banco Edwards | `edwards` | ✅ Funcional |
45
+ | Scotiabank | `scotiabank` | ✅ Funcional |
46
+ | Banco de Chile | `bchile` | ✅ Funcional |
47
+ | BCI | `bci` | ✅ Funcional |
48
+ | Itaú | `itau` | ✅ Funcional |
49
+ | Banco Estado (CuentaRUT) | `bestado` | ✅ Funcional |
50
+
51
+ **¿Tu banco no está?** → [Contribuir](#contribuir)
52
+
53
+ ## Requisitos
54
+
55
+ - **Node.js** >= 18
56
+ - **Google Chrome** o **Chromium**
57
+
58
+ ```bash
59
+ # Instalar Chrome — Ubuntu/Debian
60
+ sudo apt update && sudo apt install -y google-chrome-stable
61
+
62
+ # macOS
63
+ brew install --cask google-chrome
64
+ ```
65
+
66
+ ## Instalación
67
+
68
+ ```bash
69
+ # Desde GitHub
70
+ npm install github:kaihv/open-banking-chile
71
+
72
+ # O clonar el repo
73
+ git clone https://github.com/kaihv/open-banking-chile.git
74
+ cd open-banking-chile
75
+ npm install
76
+ npm run build
77
+ ```
78
+
79
+ ## Uso
80
+
81
+ ### CLI
82
+
83
+ Configura tu archivo `.env` con tus credenciales:
84
+
85
+ ```bash
86
+ # Banco Falabella
87
+ FALABELLA_RUT=12345678-9
88
+ FALABELLA_PASS=tu_clave
89
+
90
+ # Banco BICE
91
+ BICE_RUT=12345678-9
92
+ BICE_PASS=tu_clave
93
+ ## Opcional:
94
+ BICE_MONTHS=1
95
+
96
+ # Banco Santander
97
+ SANTANDER_RUT=12345678-9
98
+ SANTANDER_PASS=tu_clave
99
+
100
+ # Banco de Chile
101
+ BANCOCHILE_RUT=12345678-9
102
+ BANCOCHILE_PASS=tu_clave
103
+
104
+ # Banco Edwards
105
+ EDWARDS_RUT=12345678-9
106
+ EDWARDS_PASS=tu_clave
107
+
108
+
109
+ # Itaú
110
+ ITAU_RUT=12345678-9
111
+ ITAU_PASS=tu_clave
112
+
113
+ # Banco Estado
114
+ BESTADO_RUT=12345678-9
115
+ BESTADO_PASS=tu_clave
116
+ ```
117
+
118
+ Ejecuta la librería con el comando `npx`, `dotenv` incluirá automáticamente las variables de entorno.
119
+
120
+ ```bash
121
+
122
+ # Consultar banco
123
+ npx open-banking-chile --bank falabella --pretty
124
+ npx open-banking-chile --bank santander --pretty
125
+ npx open-banking-chile --bank bchile --pretty
126
+ npx open-banking-chile --bank edwards --pretty
127
+ npx open-banking-chile --bank itau --pretty
128
+ npx open-banking-chile --bank bestado --pretty
129
+
130
+ # Solo movimientos
131
+ npx open-banking-chile --bank falabella --movements | jq .
132
+
133
+ # Listar bancos disponibles
134
+ npx open-banking-chile --list
135
+
136
+ # Con screenshots para debugging
137
+ npx open-banking-chile --bank falabella --screenshots --pretty
138
+ ```
139
+
140
+ **Opciones CLI:**
141
+
142
+ | Flag | Descripción |
143
+ | ------------------- | --------------------------------------------------------------- |
144
+ | `--bank <id>` | Banco a consultar (requerido) |
145
+ | `--list` | Listar bancos disponibles |
146
+ | `--pretty` | JSON formateado |
147
+ | `--movements` | Solo array de movimientos |
148
+ | `--screenshots` | Guardar screenshots locales en `./screenshots/` |
149
+ | `--headful` | Chrome visible (debugging). **BancoEstado siempre usa headful** |
150
+ | `--owner <T\|A\|B>` | Filtro Titular/Adicional para TC (default: B = todos) |
151
+
152
+ ### Como librería
153
+
154
+ ```typescript
155
+ import { banks, getBank } from "open-banking-chile";
156
+
157
+ // Opción 1: por ID
158
+ const falabella = getBank("falabella");
159
+ const result = await falabella!.scrape({
160
+ rut: "12345678-9",
161
+ password: "mi_clave",
162
+ });
163
+
164
+ // Opción 2: import directo
165
+ import { falabella } from "open-banking-chile";
166
+ const result = await falabella.scrape({
167
+ rut: "12345678-9",
168
+ password: "mi_clave",
169
+ });
170
+
171
+ if (result.success) {
172
+ console.log(`Banco: ${result.bank}`);
173
+ console.log(`Saldo: $${result.balance?.toLocaleString("es-CL")}`);
174
+ console.log(`${result.movements.length} movimientos`);
175
+
176
+ for (const m of result.movements) {
177
+ const sign = m.amount > 0 ? "+" : "";
178
+ console.log(
179
+ `${m.date} | ${m.description.padEnd(40)} | ${sign}$${m.amount.toLocaleString("es-CL")}`,
180
+ );
181
+ }
182
+ }
183
+ ```
184
+
185
+ ### Output
186
+
187
+ ```json
188
+ {
189
+ "success": true,
190
+ "bank": "falabella",
191
+ "movements": [
192
+ {
193
+ "date": "08-03-2026",
194
+ "description": "COMPRA SUPERMERCADO LIDER",
195
+ "amount": -45230,
196
+ "balance": 1250000,
197
+ "source": "account"
198
+ },
199
+ {
200
+ "date": "07-03-2026",
201
+ "description": "COMPRA COMERCIO",
202
+ "amount": -15990,
203
+ "balance": 0,
204
+ "source": "credit_card_unbilled",
205
+ "owner": "titular",
206
+ "installments": "01/03"
207
+ },
208
+ {
209
+ "date": "01-03-2026",
210
+ "description": "PAGO TARJETA DE CRÉDITO",
211
+ "amount": 70000,
212
+ "balance": 0,
213
+ "source": "credit_card_billed"
214
+ }
215
+ ],
216
+ "balance": 1250000
217
+ }
218
+ ```
219
+
220
+ ### Campo `source`
221
+
222
+ Cada movimiento incluye un campo `source` que indica su origen:
223
+
224
+ | Valor | Descripción |
225
+ | ---------------------- | --------------------------------- |
226
+ | `account` | Cuenta corriente o vista |
227
+ | `credit_card_unbilled` | Tarjeta de crédito — por facturar |
228
+ | `credit_card_billed` | Tarjeta de crédito — facturado |
229
+
230
+ Campos opcionales:
231
+
232
+ - `owner`: `"titular"` o `"adicional"` (solo Falabella CMR por ahora)
233
+ - `installments`: cuotas en formato `NN/NN`, ej: `"02/06"` = cuota 2 de 6 (Falabella, BChile, Itaú)
234
+
235
+ ## Seguridad
236
+
237
+ - **Tus credenciales nunca salen de tu máquina**. Todo corre 100% local.
238
+ - No hay analytics, telemetría, ni tracking.
239
+ - Las credenciales se pasan por env vars, nunca se guardan en disco.
240
+ - Los screenshots de debug pueden contener datos sensibles — no los compartas.
241
+ - Lee [SECURITY.md](SECURITY.md) para más detalles.
242
+
243
+ ## Arquitectura
244
+
245
+ El proyecto sigue una **arquitectura limpia en tres capas**, separando responsabilidades para facilitar la reutilización y la adición de nuevos bancos:
246
+
247
+ ```
248
+ src/
249
+ index.ts — Registro de bancos, getBank(), listBanks()
250
+ types.ts — Interfaces: BankScraper, BankMovement, ScrapeResult
251
+ utils.ts — Utilidades compartidas (parsing, fechas, dedup)
252
+ cli.ts — CLI entry point
253
+ infrastructure/
254
+ browser.ts — Gestión centralizada del browser (launch, sesión, cleanup)
255
+ scraper-runner.ts — Pipeline de ejecución (credenciales → browser → scrape → logout → resultado)
256
+ actions/
257
+ login.ts — Login genérico (RUT, password, submit, detección de errores)
258
+ navigation.ts — Navegación DOM (click por texto, sidebars, banners)
259
+ extraction.ts — Extracción de movimientos desde tablas HTML
260
+ pagination.ts — Iteración multi-página (Siguiente, Ver más)
261
+ credit-card.ts — Extracción de movimientos de tarjeta de crédito
262
+ balance.ts — Extracción de saldo
263
+ two-factor.ts — Detección y espera de 2FA
264
+ banks/
265
+ falabella.ts — Banco Falabella + CMR (cuenta + tarjeta de crédito)
266
+ bestado.ts — Banco Estado (CuentaRUT, requiere headful)
267
+ bchile.ts — Banco de Chile
268
+ bci.ts — BCI (iframes + BCI Pass)
269
+ bice.ts — Banco BICE
270
+ edwards.ts — Banco Edwards
271
+ itau.ts — Itaú
272
+ santander.ts — Banco Santander
273
+ scotiabank.ts — Scotiabank Chile
274
+ ```
275
+
276
+ ### Capas
277
+
278
+ **Infrastructure** — Gestión del ciclo de vida del browser. `launchBrowser()` centraliza la configuración de Chrome (anti-detección, user agent, modo headful/headless). `runScraper()` envuelve toda la ejecución: valida credenciales, abre el browser, ejecuta el scraper del banco, hace logout y cierra el browser. Los errores se capturan y retornan como `ScrapeResult`.
279
+
280
+ **Actions** — Operaciones reutilizables e independientes del banco. Cada banco compone estas acciones en vez de reimplementarlas. Por ejemplo, `fillRut()` soporta múltiples formatos de RUT y funciona tanto en `Page` como en iframes; `paginateAndExtract()` navega automáticamente por páginas acumulando movimientos; `detect2FA()` detecta segundo factor por keywords configurables.
281
+
282
+ **Banks** — Orquestación específica de cada banco. Solo contienen la lógica particular: selectores CSS propios, flujo de navegación, y configuración de 2FA. Usan `runScraper()` como wrapper y componen acciones del layer anterior.
283
+
284
+ ### Utilidades compartidas (`utils.ts`)
285
+
286
+ Funciones comunes usadas por scrapers y acciones:
287
+
288
+ | Función | Descripción |
289
+ | ----------------------------------------------- | ---------------------------------------------------------------------- |
290
+ | `parseChileanAmount(text)` | Parsea montos en formato chileno ($1.234.567) a número |
291
+ | `normalizeDate(raw)` | Normaliza fechas a DD-MM-YYYY (soporta dd/mm/yyyy, "9 mar 2026", etc.) |
292
+ | `normalizeOwner(raw)` | Normaliza owner a `"titular"` o `"adicional"` |
293
+ | `normalizeInstallments(raw)` | Normaliza cuotas a formato NN/NN (ej: "1/3" → "01/03") |
294
+ | `deduplicateMovements(movements)` | Elimina movimientos duplicados por fecha+descripción+monto+source |
295
+ | `logout(page, debugLog)` | Cierra sesión buscando botones comunes (cerrar sesión, salir, etc.) |
296
+ | `formatRut(rut)` | Formatea RUT (12345678-9 → 12.345.678-9) |
297
+ | `findChrome()` | Busca Chrome/Chromium en el sistema |
298
+ | `closePopups(page)` | Cierra popups y modales genéricos |
299
+ | `delay(ms)` | Espera N milisegundos |
300
+ | `saveScreenshot(page, name, enabled, debugLog)` | Guarda screenshot si está habilitado |
301
+
302
+ ## Contribuir
303
+
304
+ Queremos cubrir **todos los bancos de Chile**. Si tienes cuenta en un banco que falta:
305
+
306
+ 1. Lee [CONTRIBUTING.md](CONTRIBUTING.md) para la guía paso a paso
307
+ 2. Crea `src/banks/<tu-banco>.ts` implementando `BankScraper`
308
+ 3. Usa `runScraper()` de `infrastructure/` y compone acciones de `actions/` (login, extracción, paginación, etc.)
309
+ 4. Usa las utilidades compartidas de `utils.ts` (parsing, fechas, dedup, logout)
310
+ 5. Regístralo en `src/index.ts`
311
+ 6. Abre un PR
312
+
313
+ ```typescript
314
+ // La interfaz es simple:
315
+ interface BankScraper {
316
+ id: string; // "mi-banco"
317
+ name: string; // "Mi Banco Chile"
318
+ url: string; // "https://www.mibanco.cl"
319
+ scrape(options: ScraperOptions): Promise<ScrapeResult>;
320
+ }
321
+ ```
322
+
323
+ ## Automatización (cron)
324
+
325
+ ```bash
326
+ # Ejemplo: sincronizar Falabella diariamente a las 7 AM
327
+ 0 7 * * * source /home/user/.env && node /path/to/dist/cli.js --bank falabella >> /var/log/bank-sync.log 2>&1
328
+
329
+ # Ejemplo: sincronizar BICE diariamente y con 3 meses históricos
330
+ 0 7 * * * source /home/user/.env && BICE_MONTHS=3 node /path/to/dist/cli.js --bank bice >> /var/log/bank-sync.log 2>&1
331
+ ```
332
+
333
+ ## Troubleshooting
334
+
335
+ | Problema | Solución |
336
+ | --------------------- | ---------------------------------------------------------------------------------------------- |
337
+ | Chrome no encontrado | Instala Chrome o usa `CHROME_PATH=/ruta/chrome` |
338
+ | 2FA / Clave dinámica | Si aparece, apruébalo manualmente en tu banco y vuelve a intentar |
339
+ | 0 movimientos | Usa `--screenshots --pretty` y revisa el debug log |
340
+ | Login falla | Verifica RUT y clave, prueba con `--headful` |
341
+ | BancoEstado bloqueado | BancoEstado bloquea headless (TLS fingerprinting). Siempre abre Chrome visible. Ver nota abajo |
342
+
343
+ ### BancoEstado y modo headless
344
+
345
+ BancoEstado detecta navegadores headless a nivel de red (TLS fingerprinting), no solo por JavaScript. Ni `puppeteer-extra-plugin-stealth` ni `rebrowser-puppeteer-core` logran evadir esta detección. El scraper siempre corre en modo headful (Chrome visible).
346
+
347
+ **En servidores Linux sin GUI**, usa Xvfb (display virtual):
348
+
349
+ ```bash
350
+ # Instalar
351
+ sudo apt install xvfb
352
+
353
+ # Correr con display virtual
354
+ xvfb-run node dist/cli.js --bank bestado --pretty
355
+
356
+ # O como parte de tu app
357
+ xvfb-run node tu-app.js
358
+ ```
359
+
360
+ **En Docker:**
361
+
362
+ ```dockerfile
363
+ RUN apt-get update && apt-get install -y xvfb google-chrome-stable
364
+ CMD ["xvfb-run", "node", "server.js"]
365
+ ```
366
+
367
+ **En Mac/Windows** no necesitas nada extra — Chrome se abre y cierra automáticamente.
368
+
369
+ ## License
370
+
371
+ MIT — Hecho en Chile 🇨🇱