forked from sclorg/nodejs-ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleImport.js
198 lines (173 loc) · 5.22 KB
/
GoogleImport.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
function GoogleImporter(){
var that = this;
const GOOGLE_KEY = "AIzaSyC-hkv1dS7t-jXthfGmCOIjwyvPnUCLvDE";
that.SEARCH_RADIUS = 2000; // meters
const request = require('request');
var photoModule = require('./photoModule');
/**
* Converts a given location name to lat/long coordinates and executes the callback function on them
*/
that.convertToLatLng = function(str, callback){
var options = {
method: 'GET',
uri: "https://maps.googleapis.com/maps/api/geocode/json",
qs: {
key: GOOGLE_KEY,
address: str
}
};
console.log("Convert " + str + " to lat. and lng.");
request(options, function(err, res, body){
if(!err && res.statusCode == 200){
var loc = JSON.parse(body);
if(loc.status == "OK"){
console.log("Coordinates of " + str + " are: " + loc.results[0].geometry.location.lat + "," + loc.results[0].geometry.location.lng);
callback(loc.results[0].geometry.location); // object with lat:..., lng:...
} else {
console.log(loc.status);
callback(null);
}
} else {
console.log("Status Code: " + res.statusCode);
callback(null);
}
});
}
/**
* Imports a photo (defined by its reference) for a venue (defined by its id) and executes the callback function with status "OK" if successful
*/
that.getVenuePhoto = function(id, ref, callback){
var options = {
method: 'GET',
uri: "https://maps.googleapis.com/maps/api/place/photo",
qs: {
key: GOOGLE_KEY,
photoreference: ref,
maxwidth: 400
}
};
var optionsHead = {
method: 'HEAD',
uri: "https://maps.googleapis.com/maps/api/place/photo",
qs: {
key: GOOGLE_KEY,
photoreference: ref,
maxwidth: 400
}
};
console.log("Getting photo for venue: " + id);
request(optionsHead, function(err, res, body){
if(!err && res.statusCode == 200){
if(res.headers["content-type"] == "image/jpeg" || res.headers["content-type"] == "image/png"){
photoModule.addPhotoPathId(res.headers["content-type"] == "image/jpeg" ? ".jpg" : ".png", function (photo){
request(options).pipe(photoModule.fs.createWriteStream(photo.path)).on('close', function(){
photoModule.addPhotoToVenue(id, photo.id, null, function(str){
if(str)
callback("OK");
else
callback(null);
});
});
});
} else {
console.log("File type not supported: " + res.headers["content-type"]);
callback(null);
}
} else {
console.log("Status Code: " + res.statusCode);
callback(null)
}
});
};
/**
* Imports the details of a venue defined by its Google id and executes the callback on them
*/
that.getVenueDetails = function(id, callback){
var options = {
method: 'GET',
uri: "https://maps.googleapis.com/maps/api/place/details/json",
qs:{
key: GOOGLE_KEY,
placeid: id
}
};
console.log("Getting details vor venue: " + id);
request(options, function(err, res, body){
if(!err && res.statusCode == 200){
var details = JSON.parse(body);
if(details.status == "OK"){
callback(details.result);
} else {
console.log(details.status);
callback(null);
}
} else {
console.log("Status Code: " + res.statusCode);
callback(null);
}
});
};
/**
* Searches for and imports unique Google venue ids in a radius around a location (lat, lng) and executes the callback function on them
*/
that.getVenues = function(lat, lng, callback){
// typestr: 'restaurant' or 'bar' or ... (see Google Places -> Types)
function searchVenues(lat, lng, typestr){
var options = {
method: 'GET',
uri: 'https://maps.googleapis.com/maps/api/place/radarsearch/json',
qs: {
key: GOOGLE_KEY,
location: "" + lat + "," + lng,
radius: that.SEARCH_RADIUS,
type: typestr
}
};
return options;
}
function processVenues(callback){
return function(err, res, body){
var ids = [];
if(!err && res.statusCode == 200){
var venues = JSON.parse(body);
if(venues.status == "OK"){
for (var i = 0; i < venues.results.length; i++) {
ids.push(venues.results[i].place_id);
console.log(ids[i]);
}
callback(ids);
} else {
console.log(venues.status);
callback(ids);
}
} else {
console.log("Status Code: " + res.statusCode);
callback(ids);
}
};
}
var locations = []
request(searchVenues(lat, lng, 'restaurant'), processVenues(function(restaurants){
request(searchVenues(lat, lng, 'bar'), processVenues(function(bars){
request(searchVenues(lat, lng, 'bakery'), processVenues(function(bakeries){
request(searchVenues(lat, lng, 'cafe'), processVenues(function(cafes){
request(searchVenues(lat, lng, 'night_club'), processVenues(function(clubs){
locations = locations.concat(restaurants, bars, bakeries, cafes, clubs);
// remove duplicates
console.log("Removing duplicate results");
var noDups = {};
for(var i=0; i<locations.length; i++){
noDups[locations[i]] = locations[i];
}
locations = [];
for(var key in noDups)
locations.push(noDups[key]);
callback(locations);
}));
}));
}));
}));
}));
};
}
module.exports = new GoogleImporter();