-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
110 lines (102 loc) · 2.99 KB
/
Copy pathindex.js
File metadata and controls
110 lines (102 loc) · 2.99 KB
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
const express = require('express')
const fs = require('fs')
const axios = require('axios')
const cors = require('cors')
const cheerio = require('cheerio');
const allData = require('./data/extract.js')
const provinces = require('./data/provinces.js')
const app = express()
app.use(cors())
app.get('/', (req, res) => {
fs.readFile(__dirname + '/index.html', (err, html) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(html);
return res.end();
})
})
app.get('/provinces', async (req, res) => {
res.json(provinces)
})
app.get('/districts', async (req, res) => {
const province = provinces.find(prov => prov.id == req.query.provinceId)
if (province) {
try {
const { data: xmlData } = (await axios.get(`https://data.bmkg.go.id/DataMKG/MEWS/DigitalForecast/DigitalForecast-${province.name.replace(/ /g, '')}.xml`))
const $ = cheerio.load(xmlData, { xmlMode: true })
const districts = []
$('area').each((i, d) => {
districts.push({
id: d.attribs.id,
name: $(d).find('[xml\\:lang="id_ID"]').text(),
area: d.attribs.description,
lat: d.attribs.latitude,
lng: d.attribs.longitude
})
})
const data = {
districts: districts
}
return res.json(data)
} catch (error) {
return res.json({
status: 200,
error
})
}
}
return res.json({
status: 200,
districts: [],
error: 'Province id not found'
})
})
app.get('/cuaca', async (req, res) => {
const province = provinces.find(prov => prov.id == req.query.provinceId)
if (province) {
try {
const xmlData = (await axios.get(`https://data.bmkg.go.id/DataMKG/MEWS/DigitalForecast/DigitalForecast-${province.name.replace(/ /g, '')}.xml`)).data
const $ = cheerio.load(xmlData, { xmlMode: true })
const district = $(`area[id="${req.query.districtId}"]`)
if (district.length) {
const times = {}
const data = {}
$('issue').children().toArray().forEach(el => times[el.tagName] = el.firstChild.data)
if (req.query.receive) {
if (req.query.receive == 'timestamp') {
return res.json({ times })
} else {
if (allData[req.query.receive]) {
data[req.query.receive] = allData[req.query.receive]($, district)
}
}
} else {
for (const dataKey in allData) {
data[dataKey] = allData[dataKey]($, district)
}
}
return res.json({ times, data })
} else {
return res.json({
status: 200,
data: {},
error: 'District id not found'
})
}
} catch (error) {
console.log(error)
return res.json({
status: 200,
error
})
}
}
return res.json({
status: 200,
data: {},
error: 'Province id not found'
})
})
const server = app.listen(3000, async () => {
console.log('Express is listening on port 3000..')
})
module.exports = app