-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a53e28f
Showing
7 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"env": { | ||
"node": true, | ||
"commonjs": true, | ||
"es2021": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": "latest" | ||
}, | ||
"rules": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
.env | ||
|
||
# vercel | ||
.vercel | ||
|
||
screenshot*.* | ||
previousData*.* | ||
*.xlsx | ||
*lock* | ||
|
||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const express = require('express'); | ||
const {tabletojson} = require ('tabletojson'); | ||
const json2xls = require ('json2xls'); | ||
const app = express(); | ||
|
||
async function readSecoexpo (back,queryURL) { | ||
try { | ||
const newData = await tabletojson.convertUrl(queryURL,(tables)=>{ | ||
return tables[0].map( | ||
(element) => { | ||
const primerObjeto = element; | ||
const keys = Object.keys(primerObjeto) | ||
const primeraPropiedad = keys[0]; | ||
primerObjeto['Destinacion'] = primerObjeto[primeraPropiedad]; | ||
delete primerObjeto[primeraPropiedad]; | ||
return primerObjeto; | ||
} | ||
); | ||
}); | ||
if(!newData){ | ||
return null; | ||
} | ||
|
||
//Convert data to xls | ||
let xls = json2xls(newData); | ||
|
||
switch (back.toLowerCase().trim()){ | ||
case 'json': | ||
return newData; | ||
case 'xls': | ||
return xls; | ||
default: | ||
return null; | ||
} | ||
} | ||
catch(e) { | ||
console.log (e); | ||
} | ||
} | ||
|
||
async function mainController(req,res){ | ||
const back = req.query.back; | ||
|
||
// Inicializo los paramentros para la URL | ||
let cuit = req.query.cuit; | ||
let fechaInicio = decodeURIComponent(req.query.fechai); | ||
let fechaFin = decodeURIComponent(req.query.fechaf); | ||
// el cuit | ||
if (!(cuit && cuit.match(/\d{11}/g))){ | ||
res.status(500).json('[{"error":"Specify cuit ?cuit=00000000000}]'); | ||
return; | ||
} | ||
// la fecha de inicio | ||
if (!(fechaInicio && fechaInicio.match(/\d\d[-]\d\d[-]\d{4}/))) { | ||
res.status(500).json('[{"error":"Specify fechai ?fechai=01-01-2023}]'); | ||
return; | ||
} | ||
fechaInicio = encodeURIComponent(fechaInicio); | ||
// la fecha de finalizacion | ||
if (!(fechaFin && fechaFin.match(/\d\d[-]\d\d[-]\d{4}/))) { | ||
res.status(500).json('[{"error":"Specify fechaf ?fechaf=01-01-2023}]'); | ||
return; | ||
} | ||
fechaFin = encodeURIComponent(fechaFin); | ||
const queryURL = `https://www.bcra.gob.ar/BCRAyVos/exportaciones-bcra-certificados-cumplidos-secoexpo.asp?cuit=${cuit}&desde=${fechaInicio}&hasta=${fechaFin}&Tipo_Respuesta=1&B1=Enviar`; | ||
// ---- fin confeccion URL ----- | ||
|
||
if (!back || (back != 'json' && back != 'xls') ){ | ||
res.status(500).json('[{"error":"Specify return type as ?back=json or ?back=xls"}]'); | ||
return; | ||
} | ||
|
||
const data = await readSecoexpo(back,queryURL); | ||
if (data){ | ||
switch (back){ | ||
case 'json': | ||
res.status(200).json(data); | ||
break; | ||
case 'xls': | ||
// Establece las cabeceras adecuadas para indicar que se trata de una descarga de archivo | ||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); | ||
res.setHeader('Content-Disposition', 'attachment; filename=BCRA_Secoexpo.xlsx'); | ||
// Envía los datos binarios al navegador | ||
// Envía los datos XLS al navegador | ||
res.write(data, 'binary'); | ||
res.end(); | ||
break; | ||
default: | ||
res.status(500).json([{"error":"No arguments received."}]); | ||
} | ||
} else { | ||
res.status(500).json([{"error":"No data returned."}]); | ||
} | ||
|
||
} | ||
|
||
console.log("START") | ||
console.time("measure"); | ||
|
||
app.get("/", mainController); | ||
app.listen(process.env.PORT || 3000, () => console.log(`Server running at http://localhost:3000`)); | ||
|
||
console.log("END"); | ||
console.timeEnd("measure"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const fs = require('fs'); | ||
const {tabletojson} = require ('tabletojson'); | ||
const json2xls = require ('json2xls'); | ||
require('dotenv').config(); | ||
const cuit = process.env.cuit; | ||
const fechaInicio = encodeURIComponent(process.env.fechai); | ||
const fechaFin = encodeURIComponent(process.env.fechaf); | ||
const URL = `https://www.bcra.gob.ar/BCRAyVos/exportaciones-bcra-certificados-cumplidos-secoexpo.asp?cuit=${cuit}&desde=${fechaInicio}&hasta=${fechaFin}&Tipo_Respuesta=1&B1=Enviar`; | ||
|
||
(async () => { | ||
try { | ||
console.log("START") | ||
console.time("measure"); | ||
|
||
const newData = await tabletojson.convertUrl(URL,(tables)=>{ | ||
return tables[0].map( | ||
(element) => { | ||
const primerObjeto = element; | ||
const keys = Object.keys(primerObjeto) | ||
const primeraPropiedad = keys[0]; | ||
primerObjeto['Destinacion'] = primerObjeto[primeraPropiedad]; | ||
delete primerObjeto[primeraPropiedad]; | ||
return primerObjeto; | ||
} | ||
); | ||
}); | ||
if(!newData){ | ||
console.log("Datos BCRA vacios.") | ||
console.timeEnd("measure") | ||
return false; | ||
} | ||
// Read the file synchronously | ||
const filePreviousData = await readFileOrCreateFile('previousData.json'); | ||
|
||
if (filePreviousData){ | ||
// Parse the JSON data | ||
const previousData = JSON.parse(filePreviousData); | ||
//Compare previous and new data. | ||
if (newData.length > previousData.length){ | ||
let countNewElements = newData.length - previousData.length; | ||
for (let j=0;j<countNewElements;j++){ | ||
console.log("NEW: " + JSON.stringify(newData[j])); | ||
previousData.unshift(newData[j]); | ||
} | ||
console.log("Saving new data."); | ||
await fs.promises.writeFile("previousData.json", JSON.stringify(previousData), (e)=>console.log("Error writing new data.\n" + e)) | ||
} else { | ||
console.log("Los datos se encuentran actualizados.") | ||
} | ||
|
||
let xls = json2xls(previousData); | ||
fs.promises.writeFile('BCRA_Secoexpo.xlsx', xls, 'binary'); | ||
|
||
} else if (newData) { | ||
console.log("No previous data. Saving new data."); | ||
await fs.promises.writeFile("previousData.json", JSON.stringify(newData), (e)=>console.log("Error writing new data.\n" + e)) | ||
} else { | ||
console.log("No new nor previous data loaded. End."); | ||
} | ||
|
||
console.log("END"); | ||
console.timeEnd("measure"); | ||
} | ||
catch(e) { | ||
console.log (e); | ||
} | ||
})(); | ||
|
||
async function readFileOrCreateFile(filePath) { | ||
try { | ||
// Check if the file exists | ||
const content = await fs.promises.readFile(filePath, 'utf-8', (e) => console.log("Error reading previousData.json.\n" + e)); | ||
return content; | ||
} catch (error) { | ||
// If the file doesn't exist, create it | ||
fs.writeFile(filePath, ''); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "scraping-bot", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"dotenv": "^16.4.5", | ||
"export-from-json": "^1.7.4", | ||
"express": "^4.18.3", | ||
"json2xls": "^0.1.2", | ||
"tabletojson": "^4.1.3" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^8.57.0", | ||
"eslint-config-standard": "^17.1.0", | ||
"eslint-plugin-import": "^2.29.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^6.1.1", | ||
"standard": "^17.1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
BACKEND | ||
BCRA - SEGUIMIENTO DE CUMPLIDOS DE EMBARQUE (SECOEXPO) | ||
Los exportadores pueden consultar los permisos de embarques cumplidos | ||
A partir del 18/06/2020, los exportadores pueden consultar en la página web del Banco Central de la República Argentina (BCRA), los cumplidos de embarque informados por las entidades financieras. | ||
Para el exportador es de suma importancia esta práctica; el control le permite verificar el cumplimiento de su obligación de orden cambiario para todas sus operaciones de exportación. | ||
Esta APP que sirve para hacer una consulta a BCRA ( bcra.gob.ar ) SECOEXPO sobre el estado | ||
de presentacion de las destinaciones. | ||
Con dicha informacion se crea un archivo JSON y un xls con los datos obtenidos. | ||
Utilizando app de forma local, utilizo los datos por diferencia por si en el futuro quiero realizar mas acciones sobre los mismos. | ||
|
||
Puede hacer consultas a la API con el siguiente link modificando los datos de la quey: | ||
https://faw-secoexpo-bcra.vercel.app/?back=json&cuit=00000000000&fechai=01-01-2019&fechaf=31-12-2024 | ||
|
||
1) Para utilizar la API localmente: | ||
npm i | ||
node index.js | ||
acceder en tu browser a: | ||
http://localhost:3000/?back=json&cuit=00000000000&fechai=01-01-2019&fechaf=31-12-2024 | ||
|
||
2)Para utilizar esta app: | ||
ejecutar: npm i | ||
|
||
crear un archivo .env con los siguientes datos: | ||
cuit = "00000000000" | ||
fechai = "dd/mm/aaaa" | ||
fechaf = "dd/mm/aaaa" | ||
|
||
ejecutar con: node index.js | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"version": 2, | ||
"builds": [ | ||
{ "src": "index.js", "use": "@vercel/node" } | ||
], | ||
"routes": [ | ||
{ "src": "/(.*)", "dest": "index.js" } | ||
] | ||
} |