category.js 10.1 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 gu_select;

// 사용법
router.get('/use', function (req, res) {
  res.render('result', { category: 'use' });
});

// 사용법
router.get('/use', function (req, res) {
  res.render('result', { category: 'use' });
});

// 사용자가 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;