Seokjin

[MERGE]feature/clothes and feature/weather into backend

1 +var request = require('request');
2 +let express = require('express');
3 +let app=express();
4 +let bodyParser = require('body-parser');
5 +let session = require('express-session');
6 +let cors = require('cors');
7 +
8 +app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}))
9 +app.use(bodyParser.urlencoded({ extended: false }));
10 +app.use(bodyParser.json());
11 +app.use(cors());
12 +//nx,ny구하기
13 +
14 +//변수들
15 +let curaddress;
16 +var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
17 +var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/
18 +
19 +//오늘의 날짜 구하기
20 +let base,tommorow;
21 +
22 +let today=new Date();
23 +let CurDay=today.getFullYear().toString();
24 +if(today.getMonth()<9){
25 + CurDay+="0"+(today.getMonth()+1).toString();
26 +}
27 +else{
28 + CurDay+=(today.getMonth()+1).toString();
29 +}
30 +if(today.getDate()<10){
31 + base=CurDay+"0"+(today.getDate()-1).toString();
32 + tommorow=CurDay+"0"+(today.getDate()+1).toString();
33 +
34 + CurDay+="0"+today.getDate().toString();
35 +}
36 +else{
37 + base=CurDay+(today.getDate()-1).toString();
38 + tommorow=CurDay+(today.getDate()+1).toString();
39 +
40 + CurDay+=today.getDate().toString();
41 +}
42 +console.log(tommorow);
43 +//nx,ny구하기
44 +const xlsx=require('xlsx')
45 +const excel=xlsx.readFile('location.xlsx');
46 +const sheet=excel.SheetNames[0];
47 +const first=excel.Sheets[sheet];
48 +const jsonData=xlsx.utils.sheet_to_json(first,{defval:""});
49 +let nx,ny;
50 +app.post('/api/address', (req, res) => {
51 + let i=0;
52 + while(i<3788){
53 + if(jsonData[i].address1==req.body.address1 && jsonData[i].address2==req.body.address2 && jsonData[i].address3==req.body.address3){
54 + nx=jsonData[i].nx;
55 + ny=jsonData[i].ny;
56 + curaddress=jsonData[i];
57 + res.json({nx, ny});
58 + break;
59 + }
60 + i+=1;
61 + }
62 +});
63 +
64 +let a3=[], a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
65 +//입력받기
66 +app.post('/api/weather',(req,res)=>{
67 + let Nx = req.body.dotX;
68 + let Ny = req.body.dotY;
69 + var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/
70 + var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
71 +
72 + queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
73 + queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('1000'); /* */
74 + queryParams += '&' + encodeURIComponent('dataType') + '=' + encodeURIComponent('JSON'); /* */
75 + queryParams += '&' + encodeURIComponent('base_date') + '=' + encodeURIComponent(base); /* */
76 + queryParams += '&' + encodeURIComponent('base_time') + '=' + encodeURIComponent('2300'); /* */
77 + queryParams += '&' + encodeURIComponent('nx') + '=' + encodeURIComponent(Nx); /*nx*/
78 + queryParams += '&' + encodeURIComponent('ny') + '=' + encodeURIComponent(Ny); /*ny*/
79 +
80 + request({
81 + url: url + queryParams,
82 + method: 'GET'
83 + }, function (error, response, body) {
84 + let ex=JSON.parse(body);
85 + let item=ex.response.body.items.item;
86 +
87 + let i=0;
88 + let a=[];//, a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
89 + while(item[i].fcstDate==CurDay){
90 + if(item[i].category=='POP' || item[i].category=='TMP'){
91 + a.push(item[i]);
92 + }
93 + i+=1;
94 + }
95 + let result=[];
96 + i=0;
97 + let Json;
98 + while(i<a.length){
99 + if(a[i].fcstValue<=8){
100 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:0};
101 + }
102 + else if(a[i].fcstValue>8 && a[i].fcstValue<=12){
103 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:1};
104 + }
105 + else if(a[i].fcstValue>12 && a[i].fcstValue<=18){
106 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:2};
107 + }
108 + else if(a[i].fcstValue>18 && a[i].fcstValue<=23){
109 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:3};
110 + }
111 + else{
112 + Json={today:CurDay,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:4};
113 + };
114 + result.push(Json);
115 + i+=2;
116 + }
117 + res.json(result);
118 + });//pop:강수확률 tmp:한시간 기온
119 +});
120 +
121 +app.post('/api/tomorrow',(req,res)=>{
122 + const nx = req.body.dotX;
123 + const ny = req.body.dotY;
124 + var queryParams = '?' + encodeURIComponent('serviceKey') + '=3OcUyvx97Vx2YikiZ9IHyRQ6suapku7Xn8VlefQKQWrGIFOGaejhbevwagcubdHfSiQAqJwCV5lyIutw0%2BsppA%3D%3D'; /* Service Key*/
125 + var url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst';
126 +
127 + queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1'); /* */
128 + queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('1000'); /* */
129 + queryParams += '&' + encodeURIComponent('dataType') + '=' + encodeURIComponent('JSON'); /* */
130 + queryParams += '&' + encodeURIComponent('base_date') + '=' + encodeURIComponent(base); /* */
131 + queryParams += '&' + encodeURIComponent('base_time') + '=' + encodeURIComponent('2300'); /* */
132 + queryParams += '&' + encodeURIComponent('nx') + '=' + encodeURIComponent(nx); /*nx*/
133 + queryParams += '&' + encodeURIComponent('ny') + '=' + encodeURIComponent(ny); /*ny*/
134 +
135 + request({
136 + url: url + queryParams,
137 + method: 'GET'
138 + }, function (error, response, body) {
139 + let ex=JSON.parse(body);
140 + let item=ex.response.body.items.item;
141 +
142 + let i=0;
143 + let a=[];//, a4=[], a5=[], a6=[], a7=[], a8=[], a9=[], a10=[], a11=[], a12=[], a13=[], a14=[], a15=[], a16=[], a17=[], a18=[], a19=[], a20=[], a21=[], a22=[], a23=[];
144 + while(item[i].fcstDate==tommorow||item[i].fcstDate==CurDay){
145 + if((item[i].category=='POP' || item[i].category=='TMP') && item[i].fcstDate==tommorow){
146 + a.push(item[i]);
147 + }
148 + i+=1;
149 + }
150 + let result=[];
151 + i=0;
152 + let Json;
153 + while(i<a.length){
154 + if(a[i].fcstValue<=8){
155 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:0};
156 + }
157 + else if(a[i].fcstValue>8 && a[i].fcstValue<=12){
158 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:1};
159 + }
160 + else if(a[i].fcstValue>12 && a[i].fcstValue<=18){
161 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:2};
162 + }
163 + else if(a[i].fcstValue>18 && a[i].fcstValue<=23){
164 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:3};
165 + }
166 + else{
167 + Json={today:tommorow,time:a[i].fcstTime[0]+a[i].fcstTime[1],temperature:a[i].fcstValue,rainPer:a[i+1].fcstValue,weather:4};
168 + };
169 + result.push(Json);
170 + i+=2;
171 + }
172 + res.json(result);
173 + });//pop:강수확률 tmp:한시간 기온
174 +});
175 +
176 +app.listen(4000, () => console.log('Server On 4000'));
...\ No newline at end of file ...\ No newline at end of file