Showing
3 changed files
with
34 additions
and
10 deletions
1 | var express = require('express'); | 1 | var express = require('express'); |
2 | require("dotenv").config(); | 2 | require("dotenv").config(); |
3 | -var url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList'; | ||
4 | -var queryParams = '?' + encodeURIComponent('serviceKey') + 'Y6FZX1ZGniy+sKjl3AYf+bnh719O0mTDXOdJEODTJ4x1hgFVU60TCHS8Cj//pUtXTkEBp6k2RQ7GGuTqNeAiyA=='; /* Service Key*/ | ||
5 | -queryParams += '&' + encodeURIComponent('stationId') + '=' + encodeURIComponent('200000078'); /* */ | ||
6 | 3 | ||
7 | const fs = require('fs'); | 4 | const fs = require('fs'); |
8 | const path = require('path'); | 5 | const path = require('path'); | ... | ... |
1 | var request = require('request'); | 1 | var request = require('request'); |
2 | -const convert = require("xml-js") | 2 | +const convert = require("xml-js"); |
3 | +const { routeIdToBusNum } = require("./routeidmap"); | ||
3 | 4 | ||
4 | var url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList'; | 5 | var url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList'; |
5 | var queryParams = '?' + encodeURIComponent('serviceKey') + process.env.SERVICE_KEY; /* Service Key*/ | 6 | var queryParams = '?' + encodeURIComponent('serviceKey') + process.env.SERVICE_KEY; /* Service Key*/ |
... | @@ -11,11 +12,27 @@ function callRequest() { | ... | @@ -11,11 +12,27 @@ function callRequest() { |
11 | url: url + queryParams, | 12 | url: url + queryParams, |
12 | method: 'GET' | 13 | method: 'GET' |
13 | }, function (error, response, body) { | 14 | }, function (error, response, body) { |
14 | - const data = convert.xml2js(body, { compact: true }).response.msgBody | 15 | + const data = convert.xml2js(body, { compact: true }).response.msgBody.busArrivalList |
15 | - if (data) | 16 | + if(!data) resolve("운행 중인 버스가 없습니다."); |
16 | - resolve(JSON.stringify(data.busArrivalList[1])); | 17 | + |
17 | - else | 18 | + let result = ""; |
18 | - resolve("운행 중인 버스가 없습니다."); | 19 | + for (let i in data) { |
20 | + result += `${routeIdToBusNum[data[i].routeId._text]}번 버스 도착 정보입니다\n`; | ||
21 | + result += `첫 번째 도착: ${data[i].predictTime1._text}\n두 번째 도착: ${data[i].predictTime2._text}\n\n`; | ||
22 | + } | ||
23 | + | ||
24 | + resolve(result.substring(0, result.length - 2)); | ||
25 | + // const data1 = convert.xml2js(body, { compact: true }).response.msgBody.busArrivalList[0].predictTime1._text | ||
26 | + // const data2 = convert.xml2js(body, { compact: true }).response.msgBody.busArrivalList[0].predictTime2._text | ||
27 | + // if (data1) | ||
28 | + // { | ||
29 | + // //resolve(JSON.stringify(data.busArrivalList[0])); | ||
30 | + // console.log("첫 번째: " + data1) | ||
31 | + // resolve("첫 번째: " + data1 + "\n두 번째: "+data2); | ||
32 | + // //resolve(JSON.stringify(data)); | ||
33 | + // } | ||
34 | + // else | ||
35 | + // resolve("운행 중인 버스가 없습니다."); | ||
19 | }); | 36 | }); |
20 | }) | 37 | }) |
21 | } | 38 | } | ... | ... |
1 | module.exports = { | 1 | module.exports = { |
2 | + routeIdToBusNum: { | ||
2 | 200000115: "5100", | 3 | 200000115: "5100", |
3 | 200000103: "9", | 4 | 200000103: "9", |
4 | 234000016: "1112", | 5 | 234000016: "1112", |
5 | - 234000001: "5500_1", | ||
6 | 200000112: "7000", | 6 | 200000112: "7000", |
7 | + 234000001: "5500_1", | ||
7 | 234001243: "5107", | 8 | 234001243: "5107", |
9 | + }, | ||
10 | + busNumToRouteId: { | ||
11 | + "5100" : 200000115, | ||
12 | + "9" : 200000103, | ||
13 | + "1112" : 234000016, | ||
14 | + "7000" : 200000112, | ||
15 | + "5500_1" : 234000001, | ||
16 | + "5107" : 234001243, | ||
17 | + } | ||
8 | } | 18 | } |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment