-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
193 lines (175 loc) · 5.47 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict'
const request = require('request')
const cheerio = require('cheerio')
const messorteUrl = 'https://www.lanuv.nrw.de/luqs/messorte/messorte.php'
const messortBildUrl = 'https://www.lanuv.nrw.de/luqs/messorte/bilder/'
const steckbriefUrl = 'https://www.lanuv.nrw.de/luqs/messorte/steckbrief.php?ort='
const messwerteUrl = 'https://www.lanuv.nrw.de/fileadmin/lanuv/luft/immissionen/aktluftqual/eu_luft_akt.htm'
const MIN_KUERZEL_LENGTH = 4
const MAX_KUERZEL_LENGTH = 6
function capitalizeFirstLetter (string) {
if (string.includes('+')) {
const splitted = string.split('+')
const first = capitalizeFirstLetter(splitted[0])
const second = capitalizeFirstLetter(splitted[1])
return `${first}+${second}`
}
return string.charAt(0).toUpperCase() + string.slice(1)
}
/**
* Helper function to query a url and load
* response with cheeriojs
*
* @param {String} url
*/
const query = (url, options = {}) => {
return new Promise((resolve, reject) => {
request({
url,
method: options.method ? options.method : 'GET',
formData: options.formData ? options.formData : undefined
}, function (error, response, body) {
if (error) {
reject(error)
}
// Wrong URL is redirected and error is null
if (response.statusCode >= 400) {
reject(response.statusMessage)
}
resolve(cheerio.load(body))
})
})
}
/**
* Requests LUQS stations website and parse stations table.
*
* @param ort
* @param kurzname
* @param klassifizierung
* @param status
* @param options.allStations return all stations if true
* @returns Promise resolves with an array of all luqs stations
*/
const luqs = (options = {}) => {
return new Promise((resolve, reject) => {
const formData = {}
const { ort, kurzname, klassifizierung, status, allStations } = options
if (ort) formData.suche_ort = ort
if (kurzname) formData.suche_kurzname = kurzname
if (status) formData.auswahl_status = status
if (klassifizierung) formData.auswahl_klassifizierung = capitalizeFirstLetter(klassifizierung)
if (allStations) {
console.info('Option allStations is deprecated and will be removed in a future release!')
formData.auswahl_status = 'alle'
}
query(messorteUrl, {
method: 'POST',
formData: formData
})
.then($ => {
const stations = []
const tableRows = $('#wrapper > table > tbody > tr')
$(tableRows).each(function (_, tableRow) {
const data = $(tableRow).text().trim().split('\n')
const station = {};
[station.messort, station.kuerzel, station.plz, station.standort] = data
stations.push(station)
})
resolve(stations.splice(1, stations.length))
})
.catch(error => {
reject(error)
})
})
}
/**
* Requests details page of a LUQS station and parses
* details of the station.
*
* @param string kuerzel
* @returns Promise resolves with an object
*/
luqs.station = (kuerzel, options = {}) => {
if (typeof kuerzel !== 'string') {
return Promise.reject(new TypeError('Expected a string'))
}
if (kuerzel.length < MIN_KUERZEL_LENGTH) {
return Promise.reject(new Error('Kuerzel is to short'))
}
if (kuerzel.length > MAX_KUERZEL_LENGTH) {
return Promise.reject(new Error('Kuerzel is to long'))
}
return new Promise((resolve, reject) => {
query(steckbriefUrl + kuerzel)
.then($ => {
const tableRows = $('#wrapper > table.table_details > tbody > tr')
const tmpSteckbrief = []
$(tableRows).each(function (_, tableRow) {
$(tableRow).find('td.td_details').each(function (_, tableData) {
const data = $(tableData).text().trim().split('\n')
tmpSteckbrief.push(...data)
})
})
const steckbrief = {};
[
steckbrief.eu_kennung,
steckbrief.kuerzel,
steckbrief.adresse,
steckbrief.altitude,
steckbrief.umgebung,
steckbrief.longitude,
steckbrief.standort,
steckbrief.latitude,
steckbrief.start_messung,
steckbrief.ende_messung
] = tmpSteckbrief
steckbrief.image = `${messortBildUrl}${steckbrief.kuerzel.toUpperCase()}.jpg`
steckbrief.longitude = steckbrief.longitude.replace(',', '.')
steckbrief.latitude = steckbrief.latitude.replace(',', '.')
resolve([steckbrief])
})
.catch(error => {
reject(error)
})
})
}
/**
* Query and parse overview page of current LUQS measurements.
*
* @returns Array with current measured values
*/
luqs.aktuell = (options = {}) => {
return new Promise((resolve, reject) => {
query(messwerteUrl)
.then($ => {
const tableRows = $('table.rahmen')
const results = []
$(tableRows).find('tr').each(function (index, tableRow) {
// Skip first to table rows
if (index < 2) {
return
}
const data = []
$(tableRow).find('td').each(function (index, tableData) {
data.push($(tableData).text().trim().split('\n')[0])
})
const station = {};
[
station.station,
station.kuerzel,
station.ozon,
station.so2,
station.no2,
station.pm10
] = data
results.push(station)
})
resolve(results)
})
.catch(error => {
reject(error)
})
})
}
module.exports = luqs
module.exports.default = luqs