forked from JE0112/emergencyRoom-ChatBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emergency_api.js
116 lines (91 loc) · 4.33 KB
/
emergency_api.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
const { resolve } = require('path');
const request = require('request');
const convert = require("xml-js");
require("dotenv").config( {path: "/home/ec2-user/emergencyRoom-ChatBot/.env"} );
const EMERGENCY_KEY = process.env.EMERGENCY_KEY;
var url = 'http://apis.data.go.kr/B552657/ErmctInfoInqireService/getSrsillDissAceptncPosblInfoInqire';
var url2 = 'http://apis.data.go.kr/B552657/ErmctInfoInqireService/getEgytListInfoInqire';
/* 함수 만들기
function getspot(cap, city){
1. 주소(시도) - cap (cities and provinces)
2. 주소(시군구) - city
}
*/
const emergencys = [];
const getspot = (cap, city) => {
var queryParams = '?' + encodeURIComponent('serviceKey') + EMERGENCY_KEY; /* Service Key*/
queryParams += '&' + encodeURIComponent('STAGE1') + '=' + encodeURIComponent(cap); /* */
queryParams += '&' + encodeURIComponent('STAGE2') + '=' + encodeURIComponent(city); /* */
queryParams += '&' + encodeURIComponent('SM_TYPE') + '=' + encodeURIComponent(''); /* */
queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('10'); /* */
return new Promise((resolve, reject)=> {
request({
url: url + queryParams,
method: 'GET'
}, async (err, res, body) => {
if(err){
reject(err);
}
else{
var result = body
var xmlTojson = await convert.xml2json(result, {compact: true, spaces:0}); // xml 파일 json 변환
const test = await JSON.parse(xmlTojson)
const items = test.response.body.items
var i;
for(i = 0; i < items.item.length; i++){ // 병원 이름과 응급실 가능여부 확인 반복
const item = items.item[i]
const hospitalname = item.dutyName._text
const emergency = item.MKioskTy25._text
emergencys.push({"병원이름": hospitalname, "가용여부":emergency});
}
}
resolve(emergencys);
}
);
})
};
// const saveData = async (req, res) => {
// try{
// getspot("서울", "송파구").then((res)=> {
// console.log("res", res);
// })
// }
// catch(e){
// console.log(e);
// }
// }
// saveData();
const getspot_xy= (cap, city) =>{
var queryParams = '?' + encodeURIComponent('serviceKey') + '=52tXHgaW46YUpGn9k0r3IQrduIl6kBOl3Ta8Idra1%2BpPMYhL4qVCDu9itW8FVbDtMF4f9LAT9NJXEx7pvEJv%2FQ%3D%3D'; /* Service Key*/
queryParams += '&' + encodeURIComponent('Q0') + '=' + encodeURIComponent(cap); /* */
queryParams += '&' + encodeURIComponent('Q1') + '=' + encodeURIComponent(city); /* */
queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('10'); /* */
return new Promise((resolve, reject)=> {
request({
url: url2 + queryParams,
method: 'GET'
}, async (err, res, body) =>{
if(err){
reject(err);
}
else{
var emergency_xy = []
var result = body
var xmlTojson = convert.xml2json(result, {compact: true, spaces:0}); // xml 파일 json 변환
const test = await JSON.parse(xmlTojson)
const items = test.response.body.items
var i;
for(i = 0; i < items.item.length; i++){ // 제가 실행을 못해서 item.length가 있는지 확인은 못했습니다ㅠ
let address_x = test.response.body.items.item[i].wgs84Lat._text;
let address_y = test.response.body.items.item[i].wgs84Lon._text;
let address_name = test.response.body.items.item[i].dutyName._text;
console.log(address_name,address_x,address_y)
emergency_xy.push({"name": address_name, "y":address_x, "x": address_y,"distance":0,"duration":0});
}
resolve(emergency_xy);
}
})}
)};
module.exports = {getspot,getspot_xy}