category.js
9.94 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
var express = require('express');
var router = express.Router();
var request = require('request');
const convert = require('xml-js');
require('dotenv').config();
var urlencode = require('urlencode');
/* GET home page. */
let GU_CODE;
let ANIMAL_INFO_API_KEY = process.env.REACT_APP_API_KEY;
let gu_select;
// 사용자가 index 페이지에서 주소선택. 입력값받아오는 함수
router.post('/', function (req, res) {
var body = req.body;
gu_select = body.user_gu;
console.log(gu_select);
res.send('Good');
});
//동물병원 함수
router.get('/hospital', function (req, res) {
//api
var gu_select_encode = urlencode(gu_select);
let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=200&pageNo=1&keyword=%EB%8F%99%EB%AC%BC%EB%B3%91%EC%9B%90&where=${gu_select_encode}`;
request(pet_url, function (err, response, body) {
if (err) {
console.log(`err => ${err}`)
}
else {
if (res.statusCode == 200) {
let hospital_list = [];
var result = convert.xml2json(body, { compact: true, spaces: 4 });
var petJson = JSON.parse(result)
var itemList = petJson.response.body.items;
var numRows = petJson.response.body.totalCount._text; //개수
if (numRows == 1) {
// state 정상인 것만 추리기
if (itemList.item.state._text == '정상') {
hospital_list.push(itemList.item);
}
}
else if (numRows == 0) { }
else {
for (i = 0; i < numRows; i++) {
// state 정상인 것만 추리기
if (itemList.item[i].state._text == '정상') {
hospital_list.push(itemList.item[i]);
}
}
}
//테스트용 console.log
var titles = '';
for (i = 0; i < hospital_list.length; i++) {
titles = titles + hospital_list[i].title._text + '\n';
}
console.log(titles);
res.render('result', { category: 'hospital', titles: titles, hospital_list: hospital_list });
}
}
})
});
//동물약국
router.get('/medicine', function (req, res) {
//api
var gu_select_encode = urlencode(gu_select);
var menu = '동물약국';
var menu_encode = urlencode(menu);
let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=200&pageNo=1&keyword=${menu_encode}&where=${gu_select_encode}`;
request(pet_url, function (err, response, body) {
if (err) {
console.log(`err => ${err}`)
}
else {
if (res.statusCode == 200) {
let category_list = [];
var result = convert.xml2json(body, { compact: true, spaces: 4 });
var petJson = JSON.parse(result)
var itemList = petJson.response.body.items;
var numRows = petJson.response.body.totalCount._text; //개수
if (numRows == 1) {
// state 정상인 것만 추리기
if (itemList.item.state._text == '정상') {
category_list.push(itemList.item);
}
}
else if (numRows == 0) { }
else {
for (i = 0; i < numRows; i++) {
// state 정상인 것만 추리기
if (itemList.item[i].state._text == '정상') {
category_list.push(itemList.item[i]);
}
}
}
//테스트용 console.log
var titles = '';
for (i = 0; i < category_list.length; i++) {
titles = titles + category_list[i].title._text + '\n';
}
console.log(titles);
res.render('result', { category: 'medicine', titles: titles, category_list: category_list });
}
}
})
});
//의료기기
router.get('/appliance', function (req, res) {
//api
var gu_select_encode = urlencode(gu_select);
var menu = '동물의료기기';
var menu_encode = urlencode(menu);
let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=200&pageNo=1&keyword=${menu_encode}&where=${gu_select_encode}`;
request(pet_url, function (err, response, body) {
if (err) {
console.log(`err => ${err}`)
}
else {
if (res.statusCode == 200) {
let appliance_list = [];
var result = convert.xml2json(body, { compact: true, spaces: 4 });
var petJson = JSON.parse(result)
var itemList = petJson.response.body.items;
var numRows = petJson.response.body.totalCount._text; //개수
if (numRows == 1) {
// state 정상인 것만 추리기
if (itemList.item.state._text == '정상') {
appliance_list.push(itemList.item);
}
}
else if (numRows == 0) { }
else {
for (i = 0; i < numRows; i++) {
// state 정상인 것만 추리기
if (itemList.item[i].state._text == '정상') {
appliance_list.push(itemList.item[i]);
}
}
}
//테스트용 console.log
var titles = '';
for (i = 0; i < appliance_list.length; i++) {
titles = titles + appliance_list[i].title._text + '\n';
}
console.log(titles);
res.render('result', { category: 'appliance', titles: titles, appliance_list: appliance_list });
}
}
})
});
//체육시설
router.get('/park', function (req, res) {
var gu_select_encode = urlencode(gu_select);
var menu = ["생활체육관", "학교체육시설", "풋살장", "축구장"];
// encoding for url
var menu_encode = [];
for (var j = 0; j < menu.length; j++) {
menu_encode[j] = urlencode(menu[j]);
}
//api
let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=100&pageNo=1&keyword=${menu_encode[0]}&where=${gu_select_encode}`;
request(pet_url, function (err, response, body) {
if (err) {
console.log(`err => ${err}`)
}
else {
if (res.statusCode == 200) {
var appliance_list = [];
var titles = '';
var result = convert.xml2json(body, { compact: true, spaces: 4 });
var petJson = JSON.parse(result)
var itemList = petJson.response.body.items;
var numRows = petJson.response.body.totalCount._text; //개수
if (numRows == 1) {
appliance_list.push(itemList.item);
}
else if (numRows == 0) { }
else {
for (i = 0; i < numRows; i++) {
appliance_list.push(itemList.item[i]);
}
}
}
}
//api
let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=100&pageNo=1&keyword=${menu_encode[1]}&where=${gu_select_encode}`;
//let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=100&pageNo=1&keyword=%EC%83%9D%ED%99%9C%EC%B2%B4%EC%9C%A1%EA%B4%80&where=${gu_select_encode}`;
request(pet_url, function (err, response, body) {
if (err) {
console.log(`err => ${err}`)
}
else {
if (res.statusCode == 200) {
var result = convert.xml2json(body, { compact: true, spaces: 4 });
var petJson = JSON.parse(result)
var itemList = petJson.response.body.items;
var numRows = petJson.response.body.totalCount._text; //개수
if (numRows == 1) {
appliance_list.push(itemList.item);
}
else if (numRows == 0) { }
else {
for (i = 0; i < numRows; i++) {
appliance_list.push(itemList.item[i]);
}
}
}
}
//api
let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=100&pageNo=1&keyword=${menu_encode[2]}&where=${gu_select_encode}`;
request(pet_url, function (err, response, body) {
if (err) {
console.log(`err => ${err}`)
}
else {
if (res.statusCode == 200) {
var result = convert.xml2json(body, { compact: true, spaces: 4 });
var petJson = JSON.parse(result)
var itemList = petJson.response.body.items;
var numRows = petJson.response.body.totalCount._text; //개수
if (numRows == 1) {
appliance_list.push(itemList.item);
}
else if (numRows == 0) { }
else {
for (i = 0; i < numRows; i++) {
appliance_list.push(itemList.item[i]);
}
}
}
}
//api
let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=100&pageNo=1&keyword=${menu_encode[3]}&where=${gu_select_encode}`;
request(pet_url, function (err, response, body) {
if (err) {
console.log(`err => ${err}`)
}
else {
if (res.statusCode == 200) {
var result = convert.xml2json(body, { compact: true, spaces: 4 });
var petJson = JSON.parse(result)
var itemList = petJson.response.body.items;
var numRows = petJson.response.body.totalCount._text; //개수
if (numRows == 1) {
appliance_list.push(itemList.item);
}
else if (numRows == 0) { }
else {
for (i = 0; i < numRows; i++) {
appliance_list.push(itemList.item[i]);
}
}
//테스트용 console.log
for (i = 0; i < appliance_list.length; i++) {
titles = titles + appliance_list[i].title._text + '\n';
}
if (titles == '') { console.log('nothing'); }
else { console.log(titles); }
}
}
res.render('result', { category: 'park', titles: titles, appliance_list: appliance_list });
});
});
});
});
});
module.exports = router;