hellowhales

APIhandelr Alpha version

1 let request = require('request'); 1 let request = require('request');
2 let options = { 2 let options = {
3 'method': 'GET', 3 'method': 'GET',
4 - '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', 4 + 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaBasedList?ServiceKey=3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%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',
5 'headers': { 5 'headers': {
6 } 6 }
7 }; 7 };
...@@ -18,7 +18,7 @@ for(i in info['response']['body']['items']['item']){ ...@@ -18,7 +18,7 @@ for(i in info['response']['body']['items']['item']){
18 'public': { 18 'public': {
19 'method': 'GET', 19 'method': 'GET',
20 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?' 20 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?'
21 - + 'ServiceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D' 21 + + 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
22 + '&contentTypeId=' + '15' 22 + '&contentTypeId=' + '15'
23 + '&contentId=' + +contentId[i] 23 + '&contentId=' + +contentId[i]
24 + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y&transGuideYN=Y&_type=json', 24 + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y&transGuideYN=Y&_type=json',
...@@ -27,7 +27,7 @@ for(i in info['response']['body']['items']['item']){ ...@@ -27,7 +27,7 @@ for(i in info['response']['body']['items']['item']){
27 'detail': { 27 'detail': {
28 'method': 'GET', 28 'method': 'GET',
29 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?' 29 'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?'
30 - + 'ServiceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D' 30 + + 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
31 + '&contentTypeId=' + '15' 31 + '&contentTypeId=' + '15'
32 + '&contentId=' + contentId[i] 32 + '&contentId=' + contentId[i]
33 + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&introYN=Y&_type=json', 33 + '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&introYN=Y&_type=json',
...@@ -36,7 +36,7 @@ for(i in info['response']['body']['items']['item']){ ...@@ -36,7 +36,7 @@ for(i in info['response']['body']['items']['item']){
36 'weather': { 36 'weather': {
37 'method': 'GET', 37 'method': 'GET',
38 'url': 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?' 38 'url': 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?'
39 - + 'serviceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D' 39 + + 'serviceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
40 + '&pageNo=' + '1' 40 + '&pageNo=' + '1'
41 + '&numOfRows=' + '50' 41 + '&numOfRows=' + '50'
42 + '&dataType=' + 'JSON' 42 + '&dataType=' + 'JSON'
......
...@@ -6,12 +6,25 @@ const festivalSchema = new Schema({ ...@@ -6,12 +6,25 @@ const festivalSchema = new Schema({
6 addr: String, 6 addr: String,
7 tel: String, 7 tel: String,
8 mapx : Number, 8 mapx : Number,
9 - mapy : Number 9 + mapy : Number,
10 + eventstartdate : String,
11 + eventenddate : String,
12 + overview : String,
13 + firstimage : String,
14 + homepage : String,
15 + district : String,
16 + city : String,
17 + temparature : Number,
18 + weathers : String
19 + // weather : {
20 + // date1: { weather : String, temp : Number},
21 + // date2: { weather : String, temp : Number},
22 + // date3: { weather : String, temp : Number},
23 + // }
10 }, 24 },
11 { 25 {
12 - timestamps: true 26 + versionKey: false
13 -} 27 +});
14 -);
15 28
16 29
17 module.exports = mongoose.model('Festival',festivalSchema); 30 module.exports = mongoose.model('Festival',festivalSchema);
......
...@@ -2,15 +2,20 @@ const express = require('express'); ...@@ -2,15 +2,20 @@ const express = require('express');
2 const mongoose = require('mongoose'); 2 const mongoose = require('mongoose');
3 const db = require('mongodb'); 3 const db = require('mongodb');
4 const Festival = require('./models/Festival'); 4 const Festival = require('./models/Festival');
5 +const request = require('request-promise-native');
5 require("dotenv").config({ path: "variables.env"}); 6 require("dotenv").config({ path: "variables.env"});
6 7
7 -const COORDINATES = require('./coordinates'); 8 +const COORDINATES = require('./coordinates')['COORDINATES'];
8 const DISTRICT = [ 9 const DISTRICT = [
9 "서울특별시", "부산광역시", "울산광역시", "대구광역시", "대전광역시", 10 "서울특별시", "부산광역시", "울산광역시", "대구광역시", "대전광역시",
10 "인천광역시", "광주광역시", "세종특별자치시", "제주특별자치도", 11 "인천광역시", "광주광역시", "세종특별자치시", "제주특별자치도",
11 "경기도", "강원도", "충청북도", "충청남도", "경상북도", 12 "경기도", "강원도", "충청북도", "충청남도", "경상북도",
12 "경상남도", "전라북도", "전라남도" 13 "경상남도", "전라북도", "전라남도"
13 ] 14 ]
15 +const WEATHERTYPE = [
16 + '맑음', '비', '비/눈', '눈', '소나기'
17 +];
18 +
14 function parseDistrict(addr) { 19 function parseDistrict(addr) {
15 const words = addr.split(" "); 20 const words = addr.split(" ");
16 if( DISTRICT.includes(words[0]) ) { 21 if( DISTRICT.includes(words[0]) ) {
...@@ -19,18 +24,18 @@ function parseDistrict(addr) { ...@@ -19,18 +24,18 @@ function parseDistrict(addr) {
19 return []; 24 return [];
20 } 25 }
21 } 26 }
27 +function leftPad(value) { if (value >= 10) { return value; } return `0${value}`; }
22 28
23 -let request = require('request-promise-native'); 29 +setInterval(() => {
24 -let options = { 30 + //여기에 밑에 애들 들어감
25 - 'method': 'GET', 31 +}, 86400000);
26 - 'url' : 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaBasedList'
27 - + '?ServiceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D'
28 - + '&contentTypeId=15&areaCode=&sigunguCode=&cat1=&cat2=&cat3=&listYN=Y&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&arrange=C&numOfRows=12'
29 - + '&pageNo='+ 1
30 - + '&_type=json',
31 32
32 - 'headers': {} 33 +var today = new Date();
33 -}; 34 +var yesterday = new Date();
35 +yesterday.setDate(yesterday.getDate() - 1);
36 +let todayString = "" + (today.getFullYear())+leftPad(today.getMonth()+1)+leftPad(today.getDate());
37 +let yesterdayString = "" + (yesterday.getFullYear())+leftPad(yesterday.getMonth()+1)+leftPad(yesterday.getDate()-1);
38 +var todayTime = leftPad(today.getHours()) + "00";
34 39
35 mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ 40 mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
36 if(err) { 41 if(err) {
...@@ -41,6 +46,19 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -41,6 +46,19 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
41 console.log(err + "Reset Failed!"); 46 console.log(err + "Reset Failed!");
42 } else { 47 } else {
43 console.log(result + "Reset Success!"); 48 console.log(result + "Reset Success!");
49 +
50 + for(let i = 1; i <= 5; i++) {
51 + let options = {
52 + 'method': 'GET',
53 + 'url' : 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaBasedList'
54 + + '?ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
55 + + '&contentTypeId=15&areaCode=&sigunguCode=&cat1=&cat2=&cat3=&listYN=Y&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&arrange=C&numOfRows=12'
56 + + '&pageNo='+ i
57 + + '&_type=json',
58 +
59 + 'headers': {}
60 + };
61 +
44 request(options, async function (error, response, body) { 62 request(options, async function (error, response, body) {
45 if (error) { 63 if (error) {
46 throw new Error(error); 64 throw new Error(error);
...@@ -69,15 +87,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -69,15 +87,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
69 }, 87 },
70 'weather': { 88 'weather': {
71 'method': 'GET', 89 'method': 'GET',
72 - 'url': 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?' 90 + 'url': '',
73 - + 'serviceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D'
74 - + '&pageNo=' + '1'
75 - + '&numOfRows=' + '50'
76 - + '&dataType=' + 'JSON'
77 - + '&base_date=' + '20211129'
78 - + '&base_time=' + '0500'
79 - + '&nx=' + '55'
80 - + '&ny=' + '127',
81 'headers': {} 91 'headers': {}
82 } 92 }
83 }; 93 };
...@@ -85,9 +95,11 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -85,9 +95,11 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
85 if (error) { 95 if (error) {
86 throw new Error(error); 96 throw new Error(error);
87 } 97 }
98 +
99 + let toSave = true;
88 let pinfo = JSON.parse(body); 100 let pinfo = JSON.parse(body);
89 let distriction = parseDistrict(pinfo['response']['body']['items']['item']['addr1']); 101 let distriction = parseDistrict(pinfo['response']['body']['items']['item']['addr1']);
90 - if (!distriction) reject("탈출"); 102 + if (distriction.length == 0) toSave = false;
91 103
92 const newFestival = new Festival(); 104 const newFestival = new Festival();
93 newFestival.title = pinfo['response']['body']['items']['item']['title']; 105 newFestival.title = pinfo['response']['body']['items']['item']['title'];
...@@ -99,7 +111,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -99,7 +111,7 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
99 newFestival.firstimage = pinfo['response']['body']['items']['item']['firstimage']; 111 newFestival.firstimage = pinfo['response']['body']['items']['item']['firstimage'];
100 newFestival.homepage = pinfo ['response']['body']['items']['item']['homepage']; 112 newFestival.homepage = pinfo ['response']['body']['items']['item']['homepage'];
101 newFestival.district = distriction[0]; // 광역시/도 113 newFestival.district = distriction[0]; // 광역시/도
102 - newFestival.city = parseDistrict[1]; // 시군구 114 + newFestival.city = distriction[1]; // 시군구
103 115
104 await request(Info.detail, function (error, response, body) { 116 await request(Info.detail, function (error, response, body) {
105 if (error) { 117 if (error) {
...@@ -108,51 +120,53 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{ ...@@ -108,51 +120,53 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
108 let dinfo = JSON.parse(body); 120 let dinfo = JSON.parse(body);
109 newFestival.eventstartdate = dinfo['response']['body']['items']['item']['eventstartdate']; 121 newFestival.eventstartdate = dinfo['response']['body']['items']['item']['eventstartdate'];
110 newFestival.eventenddate = dinfo['response']['body']['items']['item']['eventenddate']; 122 newFestival.eventenddate = dinfo['response']['body']['items']['item']['eventenddate'];
123 +
124 + if (newFestival.eventenddate < todayString) toSave = false;
111 }); 125 });
112 126
113 - // let [nx, ny] = COORDINATES[distriction[0]][distriction[1]]; 127 + let [nx, ny] = COORDINATES[distriction[0]][distriction[1]];
114 - 128 + let curDate = ('0500' < todayTime ? todayString : yesterdayString);
115 - // Info.weather.url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?' 129 +
116 - // + 'serviceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D' 130 + Info.weather.url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?'
117 - // + '&pageNo=' + '1' 131 + + 'serviceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D'
118 - // + '&numOfRows=' + '50' 132 + + '&pageNo=' + '1'
119 - // + '&dataType=' + 'JSON' 133 + + '&numOfRows=' + '2000'
120 - // + '&base_date=' + '20211129' 134 + + '&dataType=' + 'JSON'
121 - // + '&base_time=' + '0500' 135 + + '&base_date=' + '20211129'
122 - // + '&nx=' + nx 136 + + '&base_time=' + '0500'
123 - // + '&ny=' + ny; 137 + + '&nx=' + nx
124 - 138 + + '&ny=' + ny;
125 - // await request(Info.weather, function (error, response, body) {
126 - // if (error) {
127 - // throw new Error(error);
128 - // }
129 - // let winfo = JSON.parse(body);
130 -
131 - // for( let item of winfo['response']['body']['items']['item'] ) {
132 -
133 - // if(item['category']=='TMP') {
134 - // console.log('온도: '+item['fcstValue']);
135 - // } else if(item['category']=='PTY') {
136 - // let weather_code = item['fcstValue']
137 -
138 - // if (weather_code == '1'){ console.log('날씨 : 비');}
139 - // else if (weather_code == '2'){ console.log('날씨 :비/눈') ;}
140 - // else if (weather_code == '3'){ console.log('날씨 :눈') ;}
141 - // else if (weather_code == '4'){ console.log('날씨 :소나기') ;}
142 - // else{console.log('날씨 :맑음') ;}
143 - // }
144 - // }
145 - // });
146 139
140 + await request(Info.weather, function (error, response, body) {
141 + if (error) {
142 + throw new Error(error);
143 + }
144 + let winfo = JSON.parse(body);
145 +
146 + let weathers = {};
147 + for( let item of winfo['response']['body']['items']['item'] ) {
148 + if(item['fcstTime'] === '1200') { // 최고기온 + 날씨
149 + let fcstDate = item['fcstDate'];
150 + if(!weathers[fcstDate]) weathers[fcstDate] = {};
151 + if(item['category']=='TMP') {
152 + weathers[fcstDate]['temp'] = item['fcstValue'];
153 + } else if(item['category']=='PTY') {
154 + weathers[fcstDate]['weather'] = WEATHERTYPE[item['fcstValue']];
155 + }
156 + }
157 + }
158 + newFestival.weathers = JSON.stringify(weathers);
159 + });
160 +
161 + if (toSave)
147 await newFestival.save().then((festival) => { 162 await newFestival.save().then((festival) => {
148 console.log(festival, "Save success!"); 163 console.log(festival, "Save success!");
149 }); 164 });
150 - })/*.catch(error => { 165 + })
151 - console.log(error);
152 - });*/
153 } 166 }
154 }); 167 });
155 } 168 }
169 + }
156 }); 170 });
157 } 171 }
158 }); 172 });
...\ No newline at end of file ...\ No newline at end of file
......