category.js 15.6 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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
var express = require('express');
var router = express.Router();
var request = require('request');
require('dotenv').config();

/* GET home page. */
let SIGUN_CODE;
let GYEONGI_API_KEY = process.env.GYEONGGI_APIKEY;
let ODSAY_API_KEY = process.env.ODSAY_APIKEY;
let user_sigun;
let user_gu;
let user_dong;
let userLocation;
let user_latitude;
let user_longitude;
let sigun = [{
        sigun_name: '가평군',
        sigun_code: 41820
    },
    {
        sigun_name: '경기도',
        sigun_code: 41000
    },
    {
        sigun_name: '고양시',
        sigun_code: 41280
    },
    {
        sigun_name: '과천시',
        sigun_code: 41290
    },
    {
        sigun_name: '광명시',
        sigun_code: 41210
    },
    {
        sigun_name: '광주시',
        sigun_code: 41610
    },
    {
        sigun_name: '구리시',
        sigun_code: 41310
    },
    {
        sigun_name: '군포시',
        sigun_code: 41410
    },
    {
        sigun_name: '김포시',
        sigun_code: 41570
    },
    {
        sigun_name: '남양주시',
        sigun_code: 41360
    },
    {
        sigun_name: '동두천시',
        sigun_code: 41250
    },
    {
        sigun_name: '부천시',
        sigun_code: 41190
    },
    {
        sigun_name: '성남시',
        sigun_code: 41130
    },
    {
        sigun_name: '수원시',
        sigun_code: 41110
    },
    {
        sigun_name: '시흥시',
        sigun_code: 41390
    },
    {
        sigun_name: '안산시',
        sigun_code: 41270
    },
    {
        sigun_name: '안성시',
        sigun_code: 41550
    },
    {
        sigun_name: '안양시',
        sigun_code: 41170
    },
    {
        sigun_name: '양주시',
        sigun_code: 41630
    },
    {
        sigun_name: '양평군',
        sigun_code: 41830
    },
    {
        sigun_name: '여주시',
        sigun_code: 41670
    },
    {
        sigun_name: '연천군',
        sigun_code: 41800
    },
    {
        sigun_name: '오산시',
        sigun_code: 41370
    },
    {
        sigun_name: '용인시',
        sigun_code: 41460
    },
    {
        sigun_name: '의왕시',
        sigun_code: 41430
    },
    {
        sigun_name: '의정부시',
        sigun_code: 41150
    },
    {
        sigun_name: '이천시',
        sigun_code: 41500
    },
    {
        sigun_name: '파주시',
        sigun_code: 41480
    },
    {
        sigun_name: '평택시',
        sigun_code: 41220
    },
    {
        sigun_name: '포천시',
        sigun_code: 41650
    },
    {
        sigun_name: '하남시',
        sigun_code: 41450
    },
    {
        sigun_name: '화성시',
        sigun_code: 41590
    },
]

let userArea = function(target, targetKey, user) {
    if (target[targetKey].includes(user)) {
        return true;
    } else {
        return false;
    }
}

// 사용자가 index 페이지에서 주소를 입력하고 넘어오면 
router.post('/', function(req, res) {
    console.log(req.body);
    user_sigun = req.body.sigun;
    user_gu = req.body.gu;
    user_dong = req.body.dong;
    userLocation = `${user_sigun} ${user_gu} ${user_dong}`;
    console.log(user_sigun);
    console.log(user_gu);
    console.log(user_dong);
    let KAKAO_API_KEY = process.env.KAKAO_APIKEY;
    let kakaoOptions = {
            url: 'https://dapi.kakao.com/v2/local/search/address.json',
            method: 'GET',
            headers: {
                'Authorization': `KakaoAK ${KAKAO_API_KEY}`
            },
            qs: {
                query: userLocation
            },
            encoding: 'UTF-8',
        }
        // sigun code 받는 부분
    for (var key in sigun) {
        if (userLocation.includes(sigun[key].sigun_name)) {
            console.log('hey!');
            SIGUN_CODE = sigun[key].sigun_code;
        }
        console.log(sigun[key].sigun_name);
    }
    console.log('SIGUN_CODE', SIGUN_CODE);
    request(kakaoOptions, function(err, res, body) {
        if (!err && res.statusCode == 200) {
            var result = JSON.parse(body);
            console.log('result', result);
            user_latitude = result.documents[0].address.x;
            user_longitude = result.documents[0].address.y;
            console.log('위도, 경도', user_latitude, user_longitude);
        }
    })
    res.render('category', { userLocation: userLocation });
});


router.get('/food', function(req, res) {

    // 맛집 리스트 조회
    let food_url = 'https://openapi.gg.go.kr/PlaceThatDoATasteyFoodSt';
    let fastfood_url = 'https://openapi.gg.go.kr/Genrestrtfastfood'
    let qs = `?KEY=${GYEONGI_API_KEY}&SIGUN_CD=${SIGUN_CODE}&Type=json`;

    request({
        url: food_url + qs,
        method: 'GET'
    }, function(err, response, body) {
        if (!err && res.statusCode == 200) {
            let food_result = JSON.parse(body);
            // console.log('food_result', food_result.PlaceThatDoATasteyFoodSt[1]);
            let stores = [];
            for (let i = 0; i < food_result.PlaceThatDoATasteyFoodSt[1].row.length; i++) {
                // 상호명, 음식명, 전화번호, 도로명주소(REFINE_ROADNM_ADDR), 지번 주소(REFINE_LOTNO_ADDR)
                store = food_result.PlaceThatDoATasteyFoodSt[1].row[i];
                if (userArea(store, 'REFINE_LOTNO_ADDR', user_dong)) {
                    console.log('store', store);
                    stores.push(store);
                }
            }
            console.log('food_result', food_result);
            console.log('stores', stores);
            request({
                url: fastfood_url + qs,
                method: 'GET'
            }, function(err, response, body) {
                if (!err && res.statusCode == 200) {
                    let fast_food_result = JSON.parse(body);
                    let fast_food_stores = [];
                    for (let i = 0; i < fast_food_result.Genrestrtfastfood[1].row.length; i++) {
                        fast_food_store = fast_food_result.Genrestrtfastfood[1].row[i];
                        // 폐업인 지점 제외 
                        if (fast_food_store['BSN_STATE_NM'] != '폐업' && userArea(fast_food_store, 'REFINE_LOTNO_ADDR', user_dong)) {
                            console.log('fast_store_name', fast_food_store);
                            fast_food_stores.push(fast_food_store);
                        }
                    }
                    res.render('result', { category: 'food', stores: stores, fast_food_stores: fast_food_stores, userLocation: userLocation });
                }

            })

        }
    });
});
router.get('/school', function(req, res) {

    let library_url = 'https://openapi.gg.go.kr/Tbggibllbrm';
    let institute_url = 'https://openapi.gg.go.kr/TninsttInstutM';
    let qs = `?KEY=${GYEONGI_API_KEY}&SIGUN_CD=${SIGUN_CODE}&Type=json`;

    request({
        url: library_url + qs,
        method: 'GET'
    }, function(err, response, body) {
        if (!err && res.statusCode == 200) {
            let library_result = JSON.parse(body);
            console.log(library_result);
            let librarys = [];
            for (let i = 0; i < library_result.Tbggibllbrm[1].row.length; i++) {
                library = library_result.Tbggibllbrm[1].row[i];
                if (userArea(library, 'REFINE_LOTNO_ADDR', user_dong)) {
                    console.log('library', library);
                    librarys.push(library);
                }

            }
            request({
                url: institute_url + qs,
                method: 'GET'
            }, function(err, response, body) {
                if (!err && res.statusCode == 200) {
                    let institute_result = JSON.parse(body);
                    console.log(institute_result);
                    let institutes = [];
                    for (let i = 0; i < institute_result.TninsttInstutM[1].row.length; i++) {
                        institute = institute_result.TninsttInstutM[1].row[i];
                        if (userArea(institute, 'REFINE_LOTNO_ADDR', user_dong)) {
                            console.log('institute', institute);
                            institutes.push(institute);
                        }
                    }
                    res.render('result', { category: 'school', librarys: librarys, institutes: institutes, userLocation: userLocation });
                }
            })
        }
    })
});
router.get('/park', function(req, res) {
    // let cityparknum = 0;
    let citypark_url = 'https://openapi.gg.go.kr/CityPark';

    let qs = `?Type=json&KEY=${GYEONGI_API_KEY}&SIGUN_CD=${SIGUN_CODE}`;
    request({
        url: citypark_url + qs,
        method: 'GET'
    }, function(err, response, body) {
        // console.log('park_result', park_result.CityPark[0].head);
        if (!err && res.statusCode == 200) {
            let city_park_result = JSON.parse(body);
            console.log('park', city_park_result)
            let city_parks = [];
            for (let i = 0; i < city_park_result.CityPark[1].row.length; i++) {
                city_park = city_park_result.CityPark[1].row[i];
                if (userArea(city_park, 'REFINE_LOTNO_ADDR', user_dong)) {
                    city_parks.push(city_park);
                }
            }
            res.render('result', { category: 'park', city_parks: city_parks, userLocation: userLocation });
        }
    })
});

router.get('/transport', function(req, res) {


    let bus_url = 'https://api.odsay.com/v1/api/pointSearch?';
    let subway_url = 'https://api.odsay.com/v1/api/pointSearch?';
    let qs = `?lang=0&x=${user_latitude}&y=${user_longitude}&radius=1000&stationClass=2&apiKey=${ODSAY_API_KEY}`;
    let busCnt = 0;
    let subwayCnt = 0;
    console.log(bus_url);

    request({
        url: bus_url + qs,
        method: 'GET'
    }, function(err, response, body) {

        if (!err && res.statusCode == 200) {
            //console.log("inside");
            let bus_result = JSON.parse(body);
            console.log('bus_result', bus_result.result.station);
            let busStations = [];
            console.log(bus_result.result)
            for (let i = 0; i < bus_result.result.station.length; i++) {
                station = bus_result.result.station[i];
                console.log('station', station)
                busStations.push(station);
            }
            console.log('busStations', busStations);
            request({
                url: subway_url + qs,
                method: 'GET'
            }, function(err, response, body) {
                if (!err && res.statusCode == 200) {
                    let subway_result = JSON.parse(body);
                    let subwayStations = [];
                    console.log('subway_result', subway_result);
                    for (let i = 0; i < subway_result.result.station.length; i++) {
                        station = subway_result.result.station[i];
                        subwayStations.push(station);
                    }
                    console.log(busStations);
                    console.log('subwayStaions', subwayStations);
                    res.render('result', { category: 'transport', busStations: busStations, subwayStations: subwayStations, userLocation: userLocation })
                }
            })

        }
    })

});
router.get('/safe', function(req, res) {

    let cctv_url = 'https://openapi.gg.go.kr/CCTV';
    let light_url = 'https://openapi.gg.go.kr/SECRTLGT'
    let qs = `?Type=json&KEY=${GYEONGI_API_KEY}&SIGUN_CD=${SIGUN_CODE}`;
    request({
        url: cctv_url + qs,
        method: 'GET'
    }, function(err, response, body) {
        if (!err && res.statusCode == 200) {
            let cctv_result = JSON.parse(body);
            let cctvs = [];
            console.log(cctv_result);
            for (let i = 0; i < cctv_result.CCTV[1].row.length; i++) {
                cctv = cctv_result.CCTV[1].row[i];
                if (userArea(cctv, 'REFINE_LOTNO_ADDR', user_dong)) {
                    console.log('cctv', cctv);
                    cctvs.push(cctv);
                }
            }
            request({
                url: light_url + qs,
                method: 'GET'
            }, function(err, response, body) {
                if (!err && res.statusCode == 200) {
                    let light_result = JSON.parse(body);
                    let lights = [];
                    console.log(light_result);
                    for (let i = 0; i < light_result.SECRTLGT[1].row.length; i++) {
                        light = light_result.SECRTLGT[1].row[i];
                        if (userArea(light, 'REFINE_LOTNO_ADDR', user_dong)) {
                            console.log('light', light);
                            lights.push(light);
                        }
                    }
                    res.render('result', { category: 'safe', cctvs: cctvs, lights: lights, userLocation: userLocation });
                }
            })
        }

    })
});

router.get('/culture', function(req, res) {
    // 영화관
    let movie_url = 'https://openapi.gg.go.kr/MovieTheater';
    let qs = `?Type=json&KEY=${GYEONGI_API_KEY}&SIGUN_CD=${SIGUN_CODE}`;
    request({
        url: movie_url + qs,
        method: 'GET'
    }, function(err, response, body) {
        if (!err && res.statusCode == 200) {
            let movie_result = JSON.parse(body);
            console.log(movie_result);
            let movies = [];
            for (let i = 0; i < movie_result.MovieTheater[1].row.length; i++) {
                movie = movie_result.MovieTheater[1].row[i];
                // 폐업인 지점 제외 
                if (!movie['BSN_STATE_NM'].includes('폐업') && userArea(movie, 'REFINE_LOTNO_ADDR', user_dong)) {
                    console.log('movie', movie);
                    movies.push(movie);
                }
            }
            res.render('result', { category: 'culture', movies: movies, userLocation: userLocation });
        }
    });
});

router.get('/shop', function(req, res) {

    let mallnum = 0;
    let shop_url = 'https://openapi.gg.go.kr/MrktStoreM';
    let qs = `?Type=json&KEY=${GYEONGI_API_KEY}&SIGUN_CD=${SIGUN_CODE}`;
    request({
        url: shop_url + qs,
        method: 'GET'
    }, function(err, response, body) {
        if (!err && res.statusCode == 200) {
            let shop_result = JSON.parse(body);
            let shops = [];
            for (let i = 0; i < shop_result.MrktStoreM[1].row.length; i++) {
                shop = shop_result.MrktStoreM[1].row[i];
                // 폐업인 지점 제외 
                if (userArea(shop, 'REFINE_LOTNO_ADDR', user_dong)) {
                    console.log('shop', shop);
                    shops.push(shop);
                }
            }
            res.render('result', { category: 'shop', shops: shops, userLocation: userLocation });
        }

    })
});

router.get('/gym', function(req, res) {
    let gym_url = 'https://openapi.gg.go.kr/PhysicaFitnessTrainingPlace';
    let qs = `?Type=json&KEY=${GYEONGI_API_KEY}&SIGUN_CD=${SIGUN_CODE}`;
    request({
        url: gym_url + qs,
        method: 'GET'
    }, function(err, response, body) {
        if (!err && res.statusCode == 200) {
            let gym_result = JSON.parse(body);
            let gyms = [];
            console.log(gym_result.PhysicaFitnessTrainingPlace[1]);
            for (let i = 0; i < gym_result.PhysicaFitnessTrainingPlace[1].row.length; i++) {
                gym = gym_result.PhysicaFitnessTrainingPlace[1].row[i];
                console.log(gym);
                if (userArea(gym, 'REFINE_LOTNO_ADDR', user_dong)) {
                    console.log('gym', gym);
                    gyms.push(gym);
                }
            }
            res.render('result', { category: 'gym', gyms: gyms, userLocation: userLocation });
        }
    })
});


module.exports = router;