Seokjin

[UPDATE]TommorowWeather

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