Skip to content

Commit

Permalink
fix(source): change source of data
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaticaq committed Sep 18, 2018
1 parent ea1e168 commit cb4f80d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
26 changes: 17 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const cheerio = require('cheerio')
const http = require('http')
const https = require('https')
const querystring = require('querystring')

const getZip = data => {
Expand All @@ -12,12 +12,12 @@ const getZip = data => {
comuna: data.commune
})
const options = {
hostname: 'www.correos.cl',
port: 80,
path: `/SitePages/codigo_postal/codigo_postal.aspx?${qs}`,
hostname: 'codigopostal.correos.cl',
port: 443,
path: `/?${qs}`,
method: 'GET'
}
const req = http.request(options, res => {
const req = https.request(options, res => {
if (res.statusCode !== 200) {
reject(new Error(`Request Failed. Status Code: ${res.statusCode}`))
} else {
Expand All @@ -29,10 +29,18 @@ const getZip = data => {
res.on('end', () => {
try {
const $ = cheerio.load(rawData, { decodeEntities: false })
const zip = $('span[id$="CodigoPostal"]').text()
const address = $('span[id$="Calle"]').text()
const number = $('span[id$="Numero"]').text()
const commune = $('span[id$="Comuna"]').text()
const zip = $('.tu_codigo')
.text()
.trim()
const address = $('.dato-info:nth-child(1)')
.text()
.trim()
const number = $('.dato-info:nth-child(3)')
.text()
.trim()
const commune = $('.dato-info:nth-child(5)')
.text()
.trim()
if (!zip) throw new Error('Not found')
resolve({
zip: parseInt(zip, 10),
Expand Down
33 changes: 27 additions & 6 deletions test/valid.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
<h1>Tu <span class="span-rojo">Código Postal</span> es: <span class="tu_codigo"><span id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_lblCodigoPostal">1111111</span></span></h1>
<div id="datos">
Calle : <span id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_lblCalle" class="dato-info">AVENIDA SIEMPREVIVA</span><br>
Número : <span id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_lblNumero" class="dato-info">742</span><br>
Comuna : <span id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_lblComuna" class="dato-info">SPRINGFIELD</span>
<div id="codigo_postal_mapa">
<div id="consulta">
<span>AVENIDA SIEMPREVIVA 742, SPRINGFIELD</span>
</div>
<!--fin consulta-->
<h1>Tu <span class="span-rojo">Código Postal</span> es: <span class="tu_codigo">
<span>8360593</span>
</span></h1>
<div id="datos">
Calle :
<span class="dato-info">
AVENIDA SIEMPREVIVA</span>
<br> Número :
<span class="dato-info">
742</span>
<br> Comuna :
<span class="dato-info">
SPRINGFIELD</span>
</div>
<!--fin datos-->
<input type="hidden" name="lat" id="lat">
<input type="hidden" name="lon" id="lon">
<div id="content-mapa" style="display:none;">
<h2>Ubicación mapa</h2>
<div id="map"></div>
</div>
<!--fin content-mapa-->
</div>
<input type="hidden" name="ctl00$PlaceHolderMain$g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f$ctl00$HiddenField1" id="ctl00_PlaceHolderMain_g_78a5f193_c1ad_4599_b57b_b321c2ff5f9f_ctl00_HiddenField1" value="avenida siempreviva 742;springfield">

0 comments on commit cb4f80d

Please sign in to comment.