-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaverDB.js
104 lines (91 loc) · 3.03 KB
/
naverDB.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var request = require("request")
var urlencode = require('urlencode');
var mongoose = require('mongoose');
var delay = require('delay');
mongoose.connect("mongodb://localhost:/dudco", (err) => {
if(err){
console.log("db connected error");
throw err;
}
});
var LocSchema = mongoose.Schema({
title : String,
address : String,
roadAddress : String,
longitude : String,
latitude : String
})
LocData = mongoose.model('location', LocSchema);
// 1 ~ 99 ; 100 ~ 199 ; 200 ~ 299
function requestDB(num){
var options = {
url: 'https://openapi.naver.com/v1/search/local.json?display=9&start='+ num +'&query='+urlencode('공중전화'),
headers: {
'X-Naver-Client-Id': 'djXqKlGSzwJRLfpChMsX',
'X-Naver-Client-Secret':'pRfku1I1au'
},
};
request.get(options, (err, res, body)=>{
//console.log(body.items)
var jbody = JSON.parse(body)
console.log(jbody.items.length)
for(var i = 0 ; i < jbody.items.length ; i++){
setTimeout(function(i){
toWGS(jbody.items[i].mapx, jbody.items[i].mapy, (data)=>{
//console.log(data.x + " " + data.y)
LocData.findOne({"title" : jbody.items[i].title}, (loc) =>{
if(loc){
console.log("data exist")
}else{
var nLoc = LocData({
title : jbody.items[i].title,
address : jbody.items[i].address,
roadAddress : jbody.items[i].roadAddress,
longitude : data.x,
latitude : data.y
})
nLoc.save((err)=>{
if(err){
console.log("save err")
}
}).then((loc)=>{
console.log(loc.title + "saved!")
})
}
})
})
}, 800 * i, i)
}
})
}
function toWGS(x, y, callback){
var toWGSOptions = {
url: 'https://dapi.kakao.com/v2/local/geo/transcoord.json?input_coord=KTM&output_coord=WGS84&x='+x+'&y='+y,
headers:{
'Authorization':'KakaoAK 4943231fa67b4c345b3478622ec20144'
}
};
request.get(toWGSOptions, (err, res, body)=>{
try{
var jbody = JSON.parse(body)
if(jbody.documents){
callback(jbody.documents[0])
// return jbody.documents[0]
}
}catch(e){
console.log(body)
}
})
}
function getDB(){
requestDB(1)
for(var i = 1; i <= 100 ; i++){
setTimeout(function(i){
console.log(i)
num = i*10
console.log(num)
requestDB(num)
}, 8000 * i, i)
}
}
getDB()