weather.js 6.59 KB
var request = require('request');
let express = require('express');
let app=express();
let bodyParser = require('body-parser');
let session = require('express-session')

app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//nx,ny구하기

//변수들
let curaddress;
var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/






//nx,ny구하기
const xlsx=require('xlsx')
const excel=xlsx.readFile('location.xlsx');
const sheet=excel.SheetNames[0];
const first=excel.Sheets[sheet];
const jsonData=xlsx.utils.sheet_to_json(first,{defval:""});
let nx,ny,add;
app.post('/address', (req, res) => {
    let i=0;
    while(i<3788){  
        if(jsonData[i].address1==req.body.address1 && jsonData[i].address2==req.body.address2 && jsonData[i].address3==req.body.address3){
            nx=jsonData[i].nx;
            ny=jsonData[i].ny;
            curaddress=jsonData[i];
            queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
            queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('1000'); /* */
            queryParams += '&' + encodeURIComponent('dataType') + '=' + encodeURIComponent('JSON'); /* */
            queryParams += '&' + encodeURIComponent('base_date') + '=' + encodeURIComponent(CurDay); /* */
            queryParams += '&' + encodeURIComponent('base_time') + '=' + encodeURIComponent('0200'); /* */
            queryParams += '&' + encodeURIComponent('nx') + '=' + encodeURIComponent(nx); /*nx*/
            queryParams += '&' + encodeURIComponent('ny') + '=' + encodeURIComponent(ny); /*ny*/
            res.send("좌표는 "+nx+" "+ny+"입니다.");
            add=req.body.address1+' '+req.body.address2+' '+req.body.address3;
            break;
        }
        i+=1;
    }
});


//오늘의 날짜 구하기
let today=new Date();
let CurDay=today.getFullYear().toString();
if(today.getMonth()<9){
    CurDay+="0"+(today.getMonth()+1).toString();
}
else{
    CurDay+=(today.getMonth()+1).toString();
}
if(today.getDate()<10){
    CurDay+="0"+today.getDate().toString();
}
else{
    CurDay+=today.getDate().toString();
}








let a3=[], a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
//입력받기
app.post('/mainpage',(req,res)=>{
    request({
        url: url + queryParams,
        method: 'GET'
    }, function (error, response, body) {
        let ex=JSON.parse(body);
        let item=ex.response.body.items.item;
        // console.log(items);
        let i=0;
        let a=[];//, a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
        while(item[i].fcstDate==CurDay){
            if(item[i].category=='POP' || item[i].category=='TMP'){
                a.push(item[i]);
            }
            i+=1;
        }
        let result=[];
        i=0;
        // while(i<a.length){
        //     let str=CurDay+' '+add+'의 '+a[i].fcstTime[0]+a[i].fcstTime[1]+'시 기온은 '+a[i].fcstValue+'이고, 강수확률은 '+a[i+1].fcstValue+'이다.';
        //     result.push(str);
        //     i+=2;
        //}//정보를 string으로 저장.
        while(i<a.length){
            let Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue};
            result.push(Json);
            i+=2;
        }//정보를 Json형식으로 저장.
        //return result를 해주고 싶은데 방법을 잘 모르겠음
        res.send(result);
    });//pop:강수확률 tmp:한시간 기온
});


//시간대별 괜찮은 옷
app.post('/clothes', (req, res) => {
    request({
        url: url + queryParams,
        method: 'GET'
    }, function (error, response, body) {
        let ex=JSON.parse(body);
        let item=ex.response.body.items.item;
        // console.log(items);
        let i=0;
        let a=[];
        while(item[i].fcstDate==CurDay){
            if(item[i].category=='POP' || item[i].category=='TMP'){
                a.push(item[i]);
            }
            i+=1;
        }
        let result=[];
        i=0;
        let Json={};
        while(i<a.length){
            let umb=0
            if(Number(a[i+1].fcstValue)>0){
                umb=1// 비가올 확률이 있을 시 우산을 챙김
            };
            if(Number(a[i].fcstValue)>=28){
                Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],top:"반팔, 민소매, 원피스",pants:"반바지",umbrella:umb};
            }
            else if(Number(a[i].fcstValue)<28 && Number(a[i].fcstValue)>=23){
                Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],top:"반팔, 얇은 셔츠",pants:"반바지, 면바지",umbrella:umb};
            }
            else if(Number(a[i].fcstValue)<23 && Number(a[i].fcstValue)>=20){
                Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],top:"얇은 가디건, 긴팔",pants:"면바지, 청바지",umbrella:umb};
            }
            else if(Number(a[i].fcstValue)<20 && Number(a[i].fcstValue)>=17){
                Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],top:"얇은 니트, 맨투맨, 가디건",pants:"면바지",umbrella:umb};
            }
            else if(Number(a[i].fcstValue)<17 && Number(a[i].fcstValue)>=12){
                Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],top:"자켓, 가디건, 야상",pants:"스타킹, 청바지, 면바지",umbrella:umb};
            }
            else if(Number(a[i].fcstValue)<12 && Number(a[i].fcstValue)>=9){
                Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],top:"자켓, 트렌치코트, 야상, 니트",pants:"청바지, 스타킹",umbrella:umb};
            }
            else if(Number(a[i].fcstValue)<9 && Number(a[i].fcstValue)>=5){
                Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],top:"코트, 가죽자켓, 히트텍, 니트",pants:"청바지, 레깅스",umbrella:umb};
            }
            else{
                Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],top:"패딩, 두꺼운코트, 목도리",pants:"기모제품, 내복바지",umbrella:umb};
            }
            result.push(Json);
            i+=2;
        }
        res.send(result);
    });
});

app.listen(4000, () => console.log('Server On 4000'));