Created
March 8, 2019 12:52
-
-
Save Mikodes/819e71c58eb7eefb881d2304a113ad60 to your computer and use it in GitHub Desktop.
last pasrsers
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
var parseString = require('xml2js').parseString; | |
var http = require('http'); | |
const createCsvWriter = require('csv-writer').createObjectCsvWriter; | |
var _ = require('lodash'); | |
var moment = require('moment'); | |
function xmlToJson(url, callback) { | |
var req = http.get(url, function (res) { | |
var xml = ''; | |
res.on('data', function (chunk) { | |
xml += chunk; | |
}); | |
res.on('error', function (e) { | |
callback(e, null); | |
}); | |
res.on('timeout', function (e) { | |
callback(e, null); | |
}); | |
res.on('end', function () { | |
parseString(xml, function (err, result) { | |
callback(null, result); | |
}); | |
}); | |
}); | |
} | |
var url = 'http://portal.apinmo.com/xml/c04st/3699-web.xml'; | |
xmlToJson(url, function (err, data) { | |
if (err) { | |
// Handle this however you like | |
return console.err(err); | |
} | |
var languages = ['es', 'en', 'cat', 'fr', 'de', 'ru']; | |
_.forEach(languages, function (lang) { | |
const csvWriter = createCsvWriter({ | |
path: 'inmuebles_' + lang + '.csv', | |
fieldDelimiter: ';', | |
header: [ | |
{ | |
id: 'id', | |
title: 'id' | |
}, | |
{ | |
id: 'email', | |
title: 'email' | |
}, { | |
id: 'destacado', | |
title: 'destacado' | |
}, { | |
id: 'antiguedad', | |
title: 'antiguedad' | |
}, { | |
id: 'tipo_ofer', | |
title: 'tipo_ofer' | |
}, { | |
id: 'fechaact', | |
title: 'fechaact' | |
}, { | |
id: 'keypromo', | |
title: 'keypromo' | |
}, { | |
id: 'opcioncompra', | |
title: 'opcioncompra' | |
}, { | |
id: 'latitud', | |
title: 'latitud' | |
}, { | |
id: 'altitud', | |
title: 'altitud' | |
}, { | |
id: 'aseos', | |
title: 'aseos' | |
}, { | |
id: 'banyos', | |
title: 'banyos' | |
}, { | |
id: 'carpint', | |
title: 'carpint' | |
}, { | |
id: 'carpext', | |
title: 'carpext' | |
}, { | |
id: 'comunidadincluida', | |
title: 'comunidadincluida' | |
}, { | |
id: 'tfachada', | |
title: 'tfachada' | |
}, { | |
id: 'tcocina', | |
title: 'tcocina' | |
}, { | |
id: 'tpostigo', | |
title: 'tpostigo' | |
}, { | |
id: 'tbano', | |
title: 'tbano' | |
}, { | |
id: 'tinterior', | |
title: 'tinterior' | |
}, { | |
id: 'distmar', | |
title: 'distmar' | |
}, { | |
id: 'planta', | |
title: 'planta' | |
}, { | |
id: 'conservacion', | |
title: 'conservacion' | |
}, { | |
id: 'gastos_com', | |
title: 'gastos_com' | |
}, { | |
id: 'habdobles', | |
title: 'habdobles' | |
}, { | |
id: 'habitaciones', | |
title: 'habitaciones' | |
}, { | |
id: 'm_cons', | |
title: 'm_cons' | |
}, { | |
id: 'm_parcela', | |
title: 'm_parcela' | |
}, { | |
id: 'm_uties', | |
title: 'm_uties' | |
}, { | |
id: 'm_cocina', | |
title: 'm_cocina' | |
}, { | |
id: 'm_comedor', | |
title: 'm_comedor' | |
}, { | |
id: 'm_terraza', | |
title: 'm_terraza' | |
}, { | |
id: 'numplanta', | |
title: 'numplanta' | |
}, { | |
id: 'orientacion', | |
title: 'orientacion' | |
}, { | |
id: 'salon', | |
title: 'salon' | |
}, { | |
id: 'suelo', | |
title: 'suelo' | |
}, { | |
id: 'precioinmo', | |
title: 'precioinmo' | |
}, { | |
id: 'precioalq', | |
title: 'precioalq' | |
}, { | |
id: 'ref', | |
title: 'ref' | |
}, { | |
id: 'accion', | |
title: 'accion' | |
}, { | |
id: 'ciudad', | |
title: 'ciudad' | |
}, { | |
id: 'cp', | |
title: 'cp' | |
}, { | |
id: 'zona', | |
title: 'zona' | |
}, { | |
id: 'energialetra', | |
title: 'energialetra' | |
}, { | |
id: 'energiarecibido', | |
title: 'energiarecibido' | |
}, { | |
id: 'descrip1', | |
title: 'descrip1' | |
}, { | |
id: 'titulo1', | |
title: 'titulo1' | |
}, { | |
id: 'estadoficha', | |
title: 'estadoficha' | |
}, { | |
id: 'tour', | |
title: 'tour' | |
}, { | |
id: 'amenities', | |
title: 'amenities' | |
}, { | |
id: 'numfotos', | |
title: 'numfotos' | |
}, { | |
id: 'fotos', | |
title: 'fotos' | |
},] | |
}); | |
// Do whatever you want with the data here | |
// Following just pretty-prints the object | |
var propiedades = []; | |
var _tipo_cocina = { | |
1: 'Cocina Independiente', | |
2: 'Cocina Exterior', | |
3: 'Cocina Americana', | |
4: 'Salon Cocina', | |
5: 'Cocina Francesa', | |
6: 'Cocina-Office', | |
} | |
var _amenities = { | |
adaptadominus: 'Adaptado minus', | |
airecentral: 'Aire Acond. Central', | |
aire_con: 'Aire Acondicionado', | |
alarma: 'Alarma', | |
alarmaincendio: 'Alarma Incendio', | |
alarmarobo: 'Alarma Robo', | |
arma_empo: 'Armarios empotrados', | |
ascensor: 'Ascensor', | |
balcon: 'Balcón', | |
bar: 'Bar', | |
barbacoa: 'Barbacoa', | |
cajafuerte: 'Caja fuerte', | |
calefacentral: 'Calefacción central', | |
calefaccion: 'Calefacción', | |
chimenea: 'Chimenea', | |
depoagua: 'Deposito Agua', | |
descalcificador: 'Descalcificador', | |
despensa: 'Despensa', | |
diafano: 'Diáfano', | |
// electro: p.electro[0], | |
esquina: 'Esquina', | |
galeria: 'Galería', | |
plaza_gara: 'Garaje', | |
garajedoble: 'Garaje Doble', | |
gasciudad: 'Gas Ciudad', | |
gimnasio: 'Gimnasio', | |
habjuegos: 'Hab. Juegos', | |
hidromasaje: 'Hidromasaje', | |
jacuzzi: 'Jacuzzi', | |
lavanderia: 'Lavandería', | |
linea_tlf: 'Línea telefónica', | |
luminoso: 'Luminoso', | |
luz: 'Luz', | |
muebles: 'Muebles', | |
ojobuey: 'Ojos de Buey', | |
parking: 'Parking', | |
patio: 'Patio', | |
piscina_com: 'Piscina Comunitaria', | |
piscina_prop: 'Piscina Propia', | |
preinstaacc: 'Preinst. AACC', | |
primera_line: 'Preinst. Hilo Musical', | |
puerta_blin: 'Puerta Blindada', | |
satelite: 'Satélite', | |
sauna: 'Sauna', | |
solarium: 'Solárium', | |
sotano: 'Sótano', | |
tv: 'televisión', | |
terraza: 'Terraza', | |
// terrazaacris: p.terrazaacris[0], | |
// todoext: p.todoext[0], | |
trastero: 'Trastero', | |
// urbanizacion: p.urbanizacion[0], | |
// vestuarios: p.vestuarios[0], | |
video_port: 'Videoportero', | |
vistasalmar: 'Vistas al mar', | |
} | |
_.forEach(data.propiedades.propiedad, function (p) { | |
var titulo = null; | |
var descripcion = null; | |
// Castellano -> 1 | |
// Catalan -> 12 | |
// Ingles -> 2 | |
// Frances -> 4 | |
// Aleman -> 3 | |
// Ruso -> 7 | |
if (lang == 'en') { | |
titulo = p.titulo2[0]; | |
descripcion = p.descrip2[0]; | |
} else if (lang == 'es') { | |
titulo = p.titulo1[0]; | |
descripcion = p.descrip1[0]; | |
} else if (lang == 'cat') { | |
titulo = p.titulo12[0]; | |
descripcion = p.descrip12[0]; | |
} else if (lang == 'fr') { | |
titulo = p.titulo4[0]; | |
descripcion = p.descrip4[0]; | |
} else if (lang == 'de') { | |
titulo = p.titulo3[0]; | |
descripcion = p.descrip3[0]; | |
} else if (lang == 'ru') { | |
titulo = p.titulo7[0]; | |
descripcion = p.descrip7[0]; | |
} | |
var _prop = { | |
id: p.id[0], | |
email: p.email[0], | |
ref: p.ref[0], | |
precioinmo: p.precioinmo[0], | |
precioalq: p.precioalq[0], | |
fechaact: moment(p.fechaact[0], 'DD/MM/YYYY', true).format('YYYY-MM-DD'), | |
estadoficha: p.estadoficha[0], | |
titulo1: titulo, | |
descrip1: descripcion, | |
tipo_ofer: p.tipo_ofer[0], | |
accion: p.accion[0], | |
ciudad: p.ciudad[0], | |
zona: p.zona[0], | |
cp: p.cp[0], | |
m_uties: p.m_uties[0], | |
m_parcela: p.m_parcela[0], | |
m_cons: p.m_cons[0], | |
aseos: p.aseos[0], | |
banyos: p.banyos[0], | |
energialetra: p.energialetra[0], | |
energiarecibido: p.energiarecibido[0], | |
habdobles: p.habdobles[0], | |
habitaciones: p.habitaciones[0], | |
destacado: p.destacado[0], | |
antiguedad: p.antiguedad[0], | |
latitud: p.latitud[0], | |
altitud: p.altitud[0], | |
carpint: p.carpint[0], | |
carpext: p.carpext[0], | |
tfachada: p.tfachada[0], | |
tcocina: p.tcocina[0], | |
tpostigo: p.tpostigo[0], | |
tbano: p.tbano[0], | |
tinterior: p.tinterior[0], | |
distmar: p.distmar[0], | |
numplanta: p.numplanta[0], | |
planta: p.planta[0], | |
conservacion: p.conservacion[0], | |
gastos_com: p.gastos_com[0], | |
m_cocina: p.m_cocina[0], | |
m_comedor: p.m_comedor[0], | |
m_terraza: p.m_terraza[0], | |
suelo: p.suelo[0], | |
orientacion: p.orientacion[0], | |
salon: p.salon[0], | |
tour: p.tour[0], | |
numfotos: p.numfotos[0], | |
fotos: '', | |
amenities: '', | |
opcioncompra: p.opcioncompra[0], | |
comunidadincluida: p.comunidadincluida[0], | |
} | |
if (_.isEqual('Vender', _prop.accion)) { | |
_prop.accion = 'Comprar'; | |
} | |
for (var i = 1; i < parseInt(_prop.numfotos); i++) { | |
var pick = p['foto' + i][0]['_']; | |
if (_.isNil(pick)) { | |
_prop.fotos += p['foto' + i][0] + ','; | |
} else { | |
_prop.fotos += p['foto' + i][0]['_'] + ','; | |
} | |
} | |
_.forEach(_amenities, function (val, key) { | |
// console.log(key,val); | |
// console.log(p) | |
var _a = parseInt(p[key][0]); | |
if (_.isEqual(_a, 1)) { | |
_prop.amenities += val + ','; | |
} | |
}); | |
propiedades.push(_prop) | |
}); | |
csvWriter.writeRecords(propiedades) // returns a promise | |
.then(function () { | |
console.log('finished') | |
}); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment