APIhandler.js 4.51 KB
let request = require('request');
let options = {
  'method': 'GET',
  'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaBasedList?ServiceKey=2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D&contentTypeId=15&areaCode=&sigunguCode=&cat1=&cat2=&cat3=&listYN=Y&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&arrange=C&numOfRows=12&pageNo=1&_type=json',
  'headers': {
  }
};
var contentId = new Array();
request(options, function (error, response, body) {
  if (error) {
    throw new Error(error);
  }
let info = JSON.parse(body); 
for(i in info['response']['body']['items']['item']){
  contentId[i]=info['response']['body']['items']['item'][i]['contentid'];
  let Info ={
        'public':{
                  'method': 'GET',
                  'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?ServiceKey=2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D&contentTypeId=15&contentId='+contentId[i]+'&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y&transGuideYN=Y&_type=json',
                  'headers': {}
                },
        'detail':{
                  'method': 'GET',
                  'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?ServiceKey=2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D&contentTypeId=15&contentId='+contentId[i]+'&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&introYN=Y&_type=json',
                  'headers': {}
                },
        'weather':
        {
          'method': 'GET',
          'url': 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?serviceKey=2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D&pageNo=1&numOfRows=50&dataType=JSON&base_date=20211129&base_time=0500&nx=55&ny=127',
          'headers': {}
        }
  };
  request(Info.public, function (error, response, body) {
          if (error) {
            throw new Error(error);
          }
        let pinfo = JSON.parse(body);
        console.log('축제 : ' + pinfo['response']['body']['items']['item']['title']);
        console.log('축제 주소 : ' + pinfo['response']['body']['items']['item']['addr1']);
        console.log('전화번호 : ' + pinfo['response']['body']['items']['item']['tel']);
        console.log('축제위도 : ' + pinfo['response']['body']['items']['item']['mapx']);
        console.log('축제경도 : ' + pinfo['response']['body']['items']['item']['mapy']);
        console.log('ID : ' + pinfo['response']['body']['items']['item']['contentid']);
        console.log('축제 정보 : ' + pinfo['response']['body']['items']['item']['overview']);
        console.log('이미지 Url : ' + pinfo['response']['body']['items']['item']['firstimage']);
        console.log('홈페이지 Url : ' + pinfo['response']['body']['items']['item']['homepage']);
        console.log('')
        });
  request(Info.detail, function (error, response, body) {
                if (error) {
                  throw new Error(error);
                }
              let dinfo = JSON.parse(body); 
              console.log('축제 시작일 : ' + dinfo['response']['body']['items']['item']['eventstartdate']);
              console.log('축제 종료일 : ' + dinfo['response']['body']['items']['item']['eventenddate']);
              console.log('나이제한 : ' + dinfo['response']['body']['items']['item']['agelimit']);
              });
  request(Info.weather, function (error, response, body) {
                if (error) {
                  throw new Error(error);
                }
              let winfo = JSON.parse(body); 
              
              for( let item of winfo['response']['body']['items']['item'])
              {
                
                if(item['category']=='TMP')
                {
                  console.log('온도: '+item['fcstValue']);
                }
                if(item['category']=='PTY')
                {
                let weather_code = item['fcstValue']
          
                 if (weather_code == '1'){ console.log('날씨 : 비');}
                 else if (weather_code == '2'){ console.log('날씨 :비/눈') ;}
                 else if (weather_code == '3'){ console.log('날씨 :눈') ;}
                 else if (weather_code == '4'){ console.log('날씨 :소나기') ;}
                 else{console.log('없음') ;}
               }
             }
      });
  }
});