-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
233 lines (201 loc) · 6.46 KB
/
index.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
///
/// EXTERNAL DEPENDENCIES
///
var Q = require('q');
var PokemonGO = require('pokemon-go-node-api');
var _ = require('lodash');
var geocoder = require('node-geocoder')({ provider: 'openstreetmap' });
var chalk = require('chalk');
var warning = chalk.bold.yellow;
var info = chalk.bold.cyan;
var Spinner = require('cli-spinner').Spinner;
var api = new PokemonGO.Pokeio();
api.playerInfo.debug = false;
var searchSpinner = new Spinner('Searching.. %s');
///
/// GLOBAL
///
var DEBUG = false;
///
/// HELPERS
///
var geo = require('./lib/geo-helper');
var utils = require('./lib/utils');
var Spotter = require('./lib/spotter');
var locWrap = utils.locWrap;
var convertPokemon = utils.convertPokemon;
function printWarning(spotterCount, locationCount) {
var batchSize = Math.ceil(locationCount/spotterCount);
var callTime = batchSize * 5;
console.warn('%s Important', warning('!'));
console.warn('%s Due to limitations in the API will take roughly %d seconds.', warning('!'), callTime);
console.warn('%s To reduce this time, pass in the constructor an array of optimally %s accounts.', warning('!'), locationCount);
}
function delayExecution(delay) {
return Q.Promise(function (resolve, reject) {
setTimeout(function () {
resolve();
}, delay);
});
}
function debug(msg) {
if (DEBUG) {
console.log('%s %s', info('i'), msg);
}
}
///
/// MODULE
///
/**
* Initializer for Pokespotter.
* Credentials can be passed as arguments or stored in the ENV variables:
* PGO_USERNAME, PGO_PASSWORD, PGO_PROVIDER
*
* @param {string} username Your Pokemon GO / Google username
* @param {string} password Your Pokemon Go / Google password
* @param {string} provider Can be 'ptc' or 'google'
* @returns Pokespotter instance
*/
function Pokespotter(users, password, provider) {
if (!Array.isArray(users) && !password) {
if (process.env.PGO_USERNAME && process.env.PGO_PASSWORD) {
users = [{
username: process.env.PGO_USERNAME,
password: process.env.PGO_PASSWORD,
provider: (process.env.PGO_PROVIDER || 'google')
}];
} else {
throw new Error('You need to pass a username and password');
}
} else if (!Array.isArray(users)) {
users = [{
username: users,
password: password,
provider: (provider || 'google')
}];
}
if (users.length === 0) {
throw new Error('Invalid or no credentials passed');
}
var spotters = users.map(function (u) { return Spotter(u, DEBUG) });
/**
* Gets all the Pokemon around a certain location.
* The location can be latitude, longitude or an address that will be checked with openstreetmap
* The options are:
* - steps: How much you should step in each direction
* - requestDelay: Artificially delays execution of calls to avoid limitations of API
*
* @param {string | {latitude, longitude}} location The central location to check
* @param {{steps, requestDelay}} options Alter behavior of the call
* @returns
*/
function get(location, options) {
options = _.defaults(options, {
steps: 1,
requestDelay: 0
});
debug('Retrieve location');
var getLocation;
if (typeof location === 'string') {
getLocation = geocoder.geocode(location).then(function (result) {
result = result[0] || { longitude: 0, latitude: 0 };
return {
longitude: result.longitude,
latitude: result.latitude
};
});
} else if (typeof location.longitude !== 'undefined' && typeof location.latitude !== 'undefined') {
getLocation = Q.when(location);
} else {
return Q.reject(new Error('Invalid coordinates. Must contain longitude and latitude'));
}
function searchLocation(spotter, locs, baseLocation) {
if (locs.length === 0 || !locs[0]) {
return Q([]);
}
var result = spotter.get(locs[0], baseLocation, options);
var pokemonList = [];
locs.forEach(function (loc, idx) {
if (idx !== 0) {
result = result.then(function () {
return delayExecution(utils.API_LIMIT_TIME).then(function () {
return spotter.get(loc, baseLocation, options);
});
});
}
result = result.then(function (p) {
pokemonList.push(p);
return p;
});
});
return result.then(function () {
return _.flatten(pokemonList);
});
}
return getLocation.then(function (baseLocation) {
debug('Location retrieved');
var locations = geo.getCoordinatesForSteps(baseLocation, options.steps);
var batchSize = Math.ceil(locations.length / spotters.length);
var locationCount = locations.length;
var searchPromises;
if (spotters.length !== locationCount) {
printWarning(spotters.length, locationCount);
}
debug('Search for Pokemon in ' + locationCount + ' locations');
if (DEBUG) {
searchSpinner.start();
}
if (spotters.length === 1) {
searchPromises = [searchLocation(spotters[0], locations, baseLocation)];
} else if (spotters.length === locations.length) {
searchPromises = spotters.map(function (spotter, idx) {
return spotter.get(locations[idx], baseLocation, options);
});
} else {
var spotterJobs = new Array(spotters.length);
for (var i = 0; i < spotters.length; i++) {
spotterJobs[i] = locations.splice(0, batchSize);
}
searchPromises = spotterJobs.map(function (job, idx) {
return searchLocation(spotters[idx], job, baseLocation);
});
}
return Q.all(searchPromises).then(function (pokemonFound) {
var result = _.chain(pokemonFound).flatten().uniqWith(function (a, b) {
return a.spawnPointId === b.spawnPointId && a.pokemonId === b.pokemonId;
}).value();
if (DEBUG) {
searchSpinner.stop(true);
}
debug(result.length + ' Pokemon found.');
return result;
});
});
}
var obj = {
get: get,
getNearby: get
};
Object.defineProperty(obj, 'DEBUG', {
set: function (val) {
DEBUG = val;
},
get: function () {
return DEBUG
}
});
return obj;
}
module.exports = Pokespotter;
module.exports.Pokespotter = Pokespotter;
module.exports.Pokedex = utils.Pokedex;
module.exports.getMapsUrl = utils.getMapsUrl;
Object.defineProperty(module.exports, 'DEBUG', {
set: function (val) {
DEBUG = val;
},
get: function () {
return DEBUG
}
});