hellowhales

APIhandelr Alpha version

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',
'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',
'headers': {
}
};
......@@ -18,7 +18,7 @@ for(i in info['response']['body']['items']['item']){
'public': {
'method': 'GET',
'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?'
+ 'ServiceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D'
+ 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%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',
......@@ -27,7 +27,7 @@ for(i in info['response']['body']['items']['item']){
'detail': {
'method': 'GET',
'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?'
+ 'ServiceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D'
+ 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
+ '&contentTypeId=' + '15'
+ '&contentId=' + contentId[i]
+ '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&introYN=Y&_type=json',
......@@ -36,7 +36,7 @@ for(i in info['response']['body']['items']['item']){
'weather': {
'method': 'GET',
'url': 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?'
+ 'serviceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D'
+ 'serviceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
+ '&pageNo=' + '1'
+ '&numOfRows=' + '50'
+ '&dataType=' + 'JSON'
......
......@@ -6,12 +6,25 @@ const festivalSchema = new Schema({
addr: String,
tel: String,
mapx : Number,
mapy : Number
mapy : Number,
eventstartdate : String,
eventenddate : String,
overview : String,
firstimage : String,
homepage : String,
district : String,
city : String,
temparature : Number,
weathers : String
// weather : {
// date1: { weather : String, temp : Number},
// date2: { weather : String, temp : Number},
// date3: { weather : String, temp : Number},
// }
},
{
timestamps: true
}
);
versionKey: false
});
module.exports = mongoose.model('Festival',festivalSchema);
......
......@@ -2,15 +2,20 @@ const express = require('express');
const mongoose = require('mongoose');
const db = require('mongodb');
const Festival = require('./models/Festival');
const request = require('request-promise-native');
require("dotenv").config({ path: "variables.env"});
const COORDINATES = require('./coordinates');
const COORDINATES = require('./coordinates')['COORDINATES'];
const DISTRICT = [
"서울특별시", "부산광역시", "울산광역시", "대구광역시", "대전광역시",
"인천광역시", "광주광역시", "세종특별자치시", "제주특별자치도",
"경기도", "강원도", "충청북도", "충청남도", "경상북도",
"경상남도", "전라북도", "전라남도"
]
const WEATHERTYPE = [
'맑음', '비', '비/눈', '눈', '소나기'
];
function parseDistrict(addr) {
const words = addr.split(" ");
if( DISTRICT.includes(words[0]) ) {
......@@ -19,18 +24,18 @@ function parseDistrict(addr) {
return [];
}
}
function leftPad(value) { if (value >= 10) { return value; } return `0${value}`; }
let request = require('request-promise-native');
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',
setInterval(() => {
//여기에 밑에 애들 들어감
}, 86400000);
'headers': {}
};
var today = new Date();
var yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
let todayString = "" + (today.getFullYear())+leftPad(today.getMonth()+1)+leftPad(today.getDate());
let yesterdayString = "" + (yesterday.getFullYear())+leftPad(yesterday.getMonth()+1)+leftPad(yesterday.getDate()-1);
var todayTime = leftPad(today.getHours()) + "00";
mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
if(err) {
......@@ -41,117 +46,126 @@ mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
console.log(err + "Reset Failed!");
} else {
console.log(result + "Reset Success!");
request(options, async function (error, response, body) {
if (error) {
throw new Error(error);
}
let info = JSON.parse(body);
let items = info['response']['body']['items']['item'];
for(item of items) {
let Info = {
'public': {
'method': 'GET',
'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?'
+ 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
+ '&contentTypeId=' + '15'
+ '&contentId=' + item['contentid']
+ '&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=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
+ '&contentTypeId=' + '15'
+ '&contentId=' + item['contentid']
+ '&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': {}
}
};
await request(Info.public, async function (error, response, body) {
if (error) {
throw new Error(error);
}
let pinfo = JSON.parse(body);
let distriction = parseDistrict(pinfo['response']['body']['items']['item']['addr1']);
if (!distriction) reject("탈출");
const newFestival = new Festival();
newFestival.title = pinfo['response']['body']['items']['item']['title'];
newFestival.addr = pinfo['response']['body']['items']['item']['addr1'];
newFestival.tel = pinfo['response']['body']['items']['item']['tel'];
newFestival.mapx = pinfo['response']['body']['items']['item']['mapx'];
newFestival.mapy = pinfo['response']['body']['items']['item']['mapy'];
newFestival.overview= pinfo['response']['body']['items']['item']['overview'];
newFestival.firstimage = pinfo['response']['body']['items']['item']['firstimage'];
newFestival.homepage = pinfo ['response']['body']['items']['item']['homepage'];
newFestival.district = distriction[0]; // 광역시/도
newFestival.city = parseDistrict[1]; // 시군구
for(let i = 1; i <= 5; i++) {
let options = {
'method': 'GET',
'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='+ i
+ '&_type=json',
'headers': {}
};
await request(Info.detail, function (error, response, body) {
request(options, async function (error, response, body) {
if (error) {
throw new Error(error);
}
let info = JSON.parse(body);
let items = info['response']['body']['items']['item'];
for(item of items) {
let Info = {
'public': {
'method': 'GET',
'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?'
+ 'ServiceKey=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
+ '&contentTypeId=' + '15'
+ '&contentId=' + item['contentid']
+ '&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=' + '3zrQDvoNwUV9Se%2BHZv8DjCCNWRGJisQ7jjHP6LsbJqoRQ2cJpQKrHUGC4uslgXSVO9Dzb06BSC3kp9BunvIPSw%3D%3D'
+ '&contentTypeId=' + '15'
+ '&contentId=' + item['contentid']
+ '&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&introYN=Y&_type=json',
'headers': {}
},
'weather': {
'method': 'GET',
'url': '',
'headers': {}
}
};
await request(Info.public, async function (error, response, body) {
if (error) {
throw new Error(error);
}
let dinfo = JSON.parse(body);
newFestival.eventstartdate = dinfo['response']['body']['items']['item']['eventstartdate'];
newFestival.eventenddate = dinfo['response']['body']['items']['item']['eventenddate'];
});
// let [nx, ny] = COORDINATES[distriction[0]][distriction[1]];
// Info.weather.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=' + nx
// + '&ny=' + ny;
let toSave = true;
let pinfo = JSON.parse(body);
let distriction = parseDistrict(pinfo['response']['body']['items']['item']['addr1']);
if (distriction.length == 0) toSave = false;
// await 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']);
// } else if(item['category']=='PTY') {
// let weather_code = item['fcstValue']
const newFestival = new Festival();
newFestival.title = pinfo['response']['body']['items']['item']['title'];
newFestival.addr = pinfo['response']['body']['items']['item']['addr1'];
newFestival.tel = pinfo['response']['body']['items']['item']['tel'];
newFestival.mapx = pinfo['response']['body']['items']['item']['mapx'];
newFestival.mapy = pinfo['response']['body']['items']['item']['mapy'];
newFestival.overview= pinfo['response']['body']['items']['item']['overview'];
newFestival.firstimage = pinfo['response']['body']['items']['item']['firstimage'];
newFestival.homepage = pinfo ['response']['body']['items']['item']['homepage'];
newFestival.district = distriction[0]; // 광역시/도
newFestival.city = distriction[1]; // 시군구
await request(Info.detail, function (error, response, body) {
if (error) {
throw new Error(error);
}
let dinfo = JSON.parse(body);
newFestival.eventstartdate = dinfo['response']['body']['items']['item']['eventstartdate'];
newFestival.eventenddate = dinfo['response']['body']['items']['item']['eventenddate'];
if (newFestival.eventenddate < todayString) toSave = false;
});
let [nx, ny] = COORDINATES[distriction[0]][distriction[1]];
let curDate = ('0500' < todayTime ? todayString : yesterdayString);
Info.weather.url = 'http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0/getVilageFcst?'
+ 'serviceKey=' + '2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D'
+ '&pageNo=' + '1'
+ '&numOfRows=' + '2000'
+ '&dataType=' + 'JSON'
+ '&base_date=' + '20211129'
+ '&base_time=' + '0500'
+ '&nx=' + nx
+ '&ny=' + ny;
await request(Info.weather, function (error, response, body) {
if (error) {
throw new Error(error);
}
let winfo = JSON.parse(body);
// 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('날씨 :맑음') ;}
// }
// }
// });
let weathers = {};
for( let item of winfo['response']['body']['items']['item'] ) {
if(item['fcstTime'] === '1200') { // 최고기온 + 날씨
let fcstDate = item['fcstDate'];
if(!weathers[fcstDate]) weathers[fcstDate] = {};
if(item['category']=='TMP') {
weathers[fcstDate]['temp'] = item['fcstValue'];
} else if(item['category']=='PTY') {
weathers[fcstDate]['weather'] = WEATHERTYPE[item['fcstValue']];
}
}
}
newFestival.weathers = JSON.stringify(weathers);
});
await newFestival.save().then((festival) => {
console.log(festival, "Save success!");
});
})/*.catch(error => {
console.log(error);
});*/
}
});
if (toSave)
await newFestival.save().then((festival) => {
console.log(festival, "Save success!");
});
})
}
});
}
}
});
}
......