-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
99 lines (90 loc) · 2.44 KB
/
index.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
'use strict'
const fs = require('fs')
const got = require('got')
const cheerio = require('cheerio')
const bluebird = require('bluebird')
const NodeGeocoder = require('node-geocoder')
const options = { provider: 'google' }
const geocoder = NodeGeocoder(options)
const getData = link => {
return got(link).then(response => {
const $ = cheerio.load(response.body)
const name = $('#nombre').val()
const rawAddress = $('#direccion').val()
const [address, _] = rawAddress.split(', ') // eslint-disable-line
const phone = $('h2:contains("Teléfono:")').next().text() || ''
const lat = parseFloat($('#glat').val())
const lng = parseFloat($('#glng').val())
const data = {
name: name,
address: address,
phone: phone,
latitude: lat,
longitud: lng
}
return geocoder
.reverse({ lat: lat, lon: lng })
.then(results => {
if (results.length === 0) return data
data.commune =
results[0].city || results[0].administrativeLevels.level3short
data.country = results[0].country
data.region = results[0].administrativeLevels.level1short
return data
})
.catch(() => {
return data
})
})
}
const getAllData = links => bluebird.map(links, getData)
const toCsv = results => {
const ws = fs.createWriteStream('bomberos.csv')
const fields = [
'address',
'commune',
'country',
'latitude',
'longitude',
'name',
'phone',
'region'
]
const fieldsString = fields.map(field => `"${field}"`).join(',')
ws.write(`${fieldsString}\n`)
results.forEach(result => {
const fields = [
result.address,
result.commune,
result.country,
result.latitude,
result.longitude,
result.name,
result.phone,
result.region
]
const fieldsString = fields.map(field => `"${field}"`).join(',')
ws.write(`${fieldsString}\n`)
})
ws.end()
}
const toJson = results => {
const ws = fs.createWriteStream('bomberos2.json')
ws.write(JSON.stringify(results))
ws.end()
}
const getLinks = () => {
const link = 'http://www.bomberos.cl/php/Cuerpos_de_Bomberos.php'
return got(link).then(response => {
const $ = cheerio.load(response.body)
return $('.li-esc2 > a')
.map(function () {
return `http://www.bomberos.cl/php/${$(this).attr('href').trim()}`
})
.get()
})
}
getLinks().then(getAllData).then(results => {
toCsv(results)
toJson(results)
})