request.js
1.81 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
var request = require('request');
const convert = require("xml-js");
const { routeIdToBusNum } = require("./routeidmap");
var url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList';
var queryParams = '?' + encodeURIComponent('serviceKey') + process.env.SERVICE_KEY; /* Service Key*/
queryParams += '&' + encodeURIComponent('stationId') + '=' + encodeURIComponent('228000710'); /* */
function callRequest() {
return new Promise((resolve, reject) => {
request({
url: url + queryParams,
method: 'GET'
}, function (error, response, body) {
const data = convert.xml2js(body, { compact: true }).response.msgBody.busArrivalList
if(!data) resolve("운행 중인 버스가 없습니다.");
let result = "";
for (let i in data) {
result += `${routeIdToBusNum[data[i].routeId._text]}번 버스 도착 정보입니다\n`;
result += `첫 번째 도착: ${data[i].predictTime1._text}\n두 번째 도착: ${data[i].predictTime2._text}\n\n`;
}
resolve(result.substring(0, result.length - 2));
// const data1 = convert.xml2js(body, { compact: true }).response.msgBody.busArrivalList[0].predictTime1._text
// const data2 = convert.xml2js(body, { compact: true }).response.msgBody.busArrivalList[0].predictTime2._text
// if (data1)
// {
// //resolve(JSON.stringify(data.busArrivalList[0]));
// console.log("첫 번째: " + data1)
// resolve("첫 번째: " + data1 + "\n두 번째: "+data2);
// //resolve(JSON.stringify(data));
// }
// else
// resolve("운행 중인 버스가 없습니다.");
});
})
}
module.exports = callRequest;