forked from useruser3/pokemongo-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Skiplagged.php
266 lines (206 loc) · 8.72 KB
/
Skiplagged.php
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
require_once 'utils/General.php';
require_once 'utils/Pokemon.php';
require_once 'auth/Google.php';
require_once 'auth/PokemonTrainerClub.php';
class Skiplagged {
private $SKIPLAGGED_API = 'http://skiplagged.com/api/pokemon.php';
private $GENERAL_API = 'https://pgorelease.nianticlabs.com/plfe/rpc';
private $SPECIFIC_API = null;
private $PROFILE = null;
private $PROFILE_RAW = null;
private $curl_skiplagged_session = null;
private $curl_niantic_session = null;
private $username = null;
private $password = null;
private $access_token = null;
private $auth_provider = 'ptc';
public function __construct() {
$this->curl_skiplagged_session = Utils\General::getCurlSession('pokemongo-php');
$this->curl_niantic_session = Utils\General::getCurlSession('Niantic App');
}
public function __destruct() {
curl_close($this->curl_skiplagged_session);
curl_close($this->curl_niantic_session);
}
// Login
public function loginWithGoogle($username, $password) {
Utils\General::log('called loginWithGoogle');
$google_auth = new Auth\Google();
$auth_provider = $google_auth->getAuthProvider();
$access_token = $google_auth->getAccessToken($username, $password);
$access_token = $access_token;
return $this->updateLogin($auth_provider, $access_token, $username, $password);
}
public function loginWithPokemonTrainer($username, $password) {
Utils\General::log('called loginWithPokemonTrainer');
$ptc_auth = new Auth\PokemonTrainerClub();
$auth_provider = $ptc_auth->getAuthProvider();
$access_token = $ptc_auth->getAccessToken($username, $password);
if (empty($access_token)) {
throw new Exception('failed to get access_token');
}
return $this->updateLogin($auth_provider, $access_token, $username, $password);
}
protected function updateLogin($auth_provider, $access_token, $username, $password) {
if (!empty($access_token)) {
$this->auth_provider = $auth_provider;
$this->access_token = $access_token;
$this->username = $username;
$this->password = $password;
return [$this->auth_provider, $this->access_token];
}
return false;
}
public function isLoggedIn() {
return !empty($this->access_token);
}
protected function refreshLogin() {
if (!$this->isLoggedIn()) {
throw new Exception('needs an existing log in');
}
$this->SPECIFIC_API = null;
$this->PROFILE = null;
$this->PROFILE_RAW = null;
if ($this->auth_provider == 'google') {
return $this->loginWithGoogle($this->username, $this->password);
} else if ($this->auth_provider == 'ptc') {
return $this->loginWithPokemonTrainer($this->username, $this->password);
}
}
public function getAccessToken() {
return $this->access_token;
}
public function getAuthProvider() {
return $this->auth_provider;
}
// Calls
protected function call($endpoint, $data) {
$is_skiplagged_api = strpos($endpoint, 'skiplagged') !== false;
$curl_session = $is_skiplagged_api ? $this->curl_skiplagged_session : $this->curl_niantic_session;
while (1) {
try {
curl_setopt($curl_session, CURLOPT_URL, $endpoint);
if ($is_skiplagged_api) {
sleep(1);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, $data);
return json_decode(curl_exec($curl_session), true);
} else {
curl_setopt($curl_session, CURLOPT_POSTFIELDS, base64_decode($data));
return base64_encode(curl_exec($curl_session));
}
} catch (Exception $e) {
Utils\General::log(sprintf('post exception %s', $e));
sleep(1);
}
}
}
public function getSpecificApiEndpoint() {
Utils\General::log('called getSpecificApiEndpoint');
if (!$this->isLoggedIn()) {
throw new Exception('need to log in first');
}
$response = $this->call($this->SKIPLAGGED_API, [
'access_token' => $this->getAccessToken(),
'auth_provider' => $this->getAuthProvider()
]);
if (!isset($response['pdata'])) {
throw new Exception('failed to get pdata');
}
$response = $this->call($this->GENERAL_API, $response['pdata']);
if (empty($response)) {
throw new Exception('pdata api call failed');
}
$response = $this->call($this->SKIPLAGGED_API, [
'access_token' => $this->getAccessToken(),
'auth_provider' => $this->getAuthProvider(),
'pdata' => $response
]);
if (!isset($response['api_endpoint']) || empty($response['api_endpoint'])) {
throw new Exception('failed to retrieve specific api endpoint');
}
$this->SPECIFIC_API = $response['api_endpoint'];
return $this->SPECIFIC_API;
}
public function getProfile() {
Utils\General::log('called getProfile');
if (empty($this->SPECIFIC_API)) {
return $this->getSpecificApiEndpoint();
}
$response = $this->call($this->SKIPLAGGED_API, [
'access_token' => $this->getAccessToken(),
'auth_provider' => $this->getAuthProvider(),
'api_endpoint' => $this->SPECIFIC_API
]);
if (!isset($response['pdata'])) {
throw new Exception('failed to get pdata');
}
$response = $this->call($this->SPECIFIC_API, $response['pdata']);
if (empty($response)) {
throw new Exception('pdata api call failed');
}
$this->PROFILE_RAW = $response;
$response = $this->call($this->SKIPLAGGED_API, [
'access_token' => $this->getAccessToken(),
'auth_provider' => $this->getAuthProvider(),
'api_endpoint' => $this->SPECIFIC_API,
'pdata' => $this->PROFILE_RAW
]);
if (!isset($response['username'])) {
throw new Exception('failed to retrieve profile');
}
$this->PROFILE = $response;
return $this->PROFILE;
}
/**
* Generates a realistic path to traverse the bounds and find spawned pokemon.
* Processed sequentially and with delay to minimize chance of getting account banned.
* Lower step size means higher accuracy, but takes more time to traverse.
*/
public function findPokemon($bounds, $pokemon_callback=null, $step_size=0.002) {
Utils\General::log('called getProfile');
if (empty($this->PROFILE_RAW)) {
$this->getProfile();
}
$bounds = sprintf('%f,%f,%f,%f', $bounds[0][0], $bounds[0][1], $bounds[1][0], $bounds[1][1]);
$response = $this->call($this->SKIPLAGGED_API, [
'access_token' => $this->getAccessToken(),
'auth_provider' => $this->getAuthProvider(),
'profile' => $this->PROFILE_RAW,
'bounds' => $bounds,
'step_size' => $step_size
]);
if (!isset($response['requests'])) {
throw new Exception('failed to get requests');
}
foreach ($response['requests'] as $request) {
Utils\General::log('moving player');
$pokemon_data = $this->call($this->SPECIFIC_API, $request['pdata']);
$response = $this->call($this->SKIPLAGGED_API, ['pdata' => $pokemon_data]);
$num_pokemon_found = count($response['pokemons']);
if ($num_pokemon_found > 0) {
Utils\General::log(sprintf('found %d pokemon', $num_pokemon_found));
}
foreach ($response['pokemons'] as $_pokemon) {
$pokemon = new Utils\Pokemon($_pokemon);
if (!is_null($pokemon_callback)) {
call_user_func_array($pokemon_callback, [$pokemon]);
} else {
echo $pokemon."\n";
}
}
sleep(.5);
}
}
public function getBoundsForAddress($address, $offset=0.002) {
$curl = curl_init('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
$bounds = $response['results'][0]['geometry']['viewport'];
return [
[$bounds['southwest']['lat'] - $offset, $bounds['southwest']['lng'] - $offset],
[$bounds['northeast']['lat'] + $offset, $bounds['northeast']['lng'] + $offset]
];
}
}