category.js 10.5 KB
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 user_gu;
let user_latitude;
let user_longitude;


//동물병원
router.post('/hospital', function (req, res) {
  //gu 받아오기
  var body = req.body;
  var gu_select = body.user_gu;
  var menu = '동물병원';

  // encoding for url
  var menu_encode = urlencode(menu);
  var gu_select_encode = urlencode(gu_select);

  //api

  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) {
        var hospital_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) {
          // 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
        for (i = 0; i < hospital_list.length; i++) {
          titles = titles + hospital_list[i].title._text + '\n';
        }

        if (titles == '') { console.log('nothing'); }
        else { console.log(titles); }
      }
    }
    res.render('result', { category: 'hospital', titles: titles, hospital_list: hospital_list });
  })
});

module.exports = router;



//동물약국
router.post('/medicine', function (req, res) {
  //gu 받아오기
  var body = req.body;
  var gu_select = body.user_gu;
  var menu = '동물약국';

  // encoding for url
  var menu_encode = urlencode(menu);
  var gu_select_encode = urlencode(gu_select);

  //api
  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) {
        var medicine_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) {
          // 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
        for (i = 0; i < medicine_list.length; i++) {
          titles = titles + medicine_list[i].title._text + '\n';
        }
        if (titles == '') { console.log('nothing'); }
        else { console.log(titles); }
      }
    }
    res.render('result', { category: 'medicine', titles: titles, medicine_list: medicine_list });
  })
});

//의료기기
router.post('/appliance', function (req, res) {
  //gu 받아오기
  var body = req.body;
  var gu_select = body.user_gu;
  var menu = "동물의료기기 판매 및 임대업체";

  // encoding for url
  var menu_encode = urlencode(menu);
  var gu_select_encode = urlencode(gu_select);

  //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}&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) {
          // 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
        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: 'appliance', titles: titles, appliance_list: appliance_list });
  })
});

//체육시설
router.post('/park', function (req, res) {
  //gu 받아오기
  var body = req.body;
  var gu_select = body.user_gu;
  var menu = ["생활체육관", "학교체육시설", "풋살장", "축구장"];

  // encoding for url
  var menu_encode = [];
  for (var j = 0; j < menu.length; j++) {
    menu_encode[j] = urlencode(menu[j]);
  }
  var gu_select_encode = urlencode(gu_select);

  //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;