forked from leegao/pokemongo-api-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
206 lines (158 loc) · 6.03 KB
/
main.py
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
import requests
import re
import struct
import json
import argparse
import pokemon_pb2
from datetime import datetime
from geopy.geocoders import GoogleV3
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
API_URL = 'https://pgorelease.nianticlabs.com/plfe/rpc'
LOGIN_URL = 'https://sso.pokemon.com/sso/login?service=https%3A%2F%2Fsso.pokemon.com%2Fsso%2Foauth2.0%2FcallbackAuthorize'
LOGIN_OAUTH = 'https://sso.pokemon.com/sso/oauth2.0/accessToken'
PTC_CLIENT_SECRET = 'w8ScCUXJQc6kXKw8FiOhd8Fixzht18Dq3PEVkUCP5ZPxtgyWsbTvWHFLm2wNY0JR'
SESSION = requests.session()
SESSION.headers.update({'User-Agent': 'Niantic App'})
SESSION.verify = False
DEBUG = True
COORDS_LATITUDE = 0
COORDS_LONGITUDE = 0
COORDS_ALTITUDE = 0
def f2i(float):
return struct.unpack('<Q', struct.pack('<d', float))[0]
def f2h(float):
return hex(struct.unpack('<Q', struct.pack('<d', float))[0])
def h2f(hex):
return struct.unpack('<d', struct.pack('<Q', int(hex,16)))[0]
def set_location(location_name):
geolocator = GoogleV3()
loc = geolocator.geocode(location_name)
print('[!] Your given location: {}'.format(loc.address))
print('[!] lat/long/alt: {} {} {}'.format(loc.latitude, loc.longitude, loc.altitude))
set_location_coords(loc.latitude, loc.longitude, loc.altitude)
def set_location_coords(lat, long, alt):
global COORDS_LATITUDE, COORDS_LONGITUDE, COORDS_ALTITUDE
COORDS_LATITUDE = f2i(lat)
COORDS_LONGITUDE = f2i(long)
COORDS_ALTITUDE = f2i(alt)
def get_location_coords():
return (COORDS_LATITUDE, COORDS_LONGITUDE, COORDS_ALTITUDE)
def api_req(api_endpoint, access_token, req):
try:
p_req = pokemon_pb2.RequestEnvelop()
p_req.unknown1 = 2
p_req.rpc_id = 8145806132888207460
p_req.requests.MergeFrom(req)
p_req.latitude, p_req.longitude, p_req.altitude = get_location_coords()
p_req.unknown12 = 989
p_req.auth.provider = 'ptc'
p_req.auth.token.contents = access_token
p_req.auth.token.unknown13 = 59
protobuf = p_req.SerializeToString()
r = SESSION.post(api_endpoint, data=protobuf, verify=False)
p_ret = pokemon_pb2.ResponseEnvelop()
p_ret.ParseFromString(r.content)
return p_ret
except Exception,e:
if DEBUG:
print(e)
return None
def get_api_endpoint(access_token):
req = pokemon_pb2.RequestEnvelop()
req1 = req.requests.add()
req1.type = 2
req2 = req.requests.add()
req2.type = 126
req3 = req.requests.add()
req3.type = 4
req4 = req.requests.add()
req4.type = 129
req5 = req.requests.add()
req5.type = 5
req5.message.unknown4 = "4a2e9bc330dae60e7b74fc85b98868ab4700802e"
p_ret = api_req(API_URL, access_token, req.requests)
try:
return ('https://%s/rpc' % p_ret.api_url)
except:
return None
def get_profile(api_endpoint, access_token):
req = pokemon_pb2.RequestEnvelop()
req1 = req.requests.add()
req1.type = 2
return api_req(api_endpoint, access_token, req.requests)
def login_ptc(username, password):
print('[!] login for: {}'.format(username))
head = {'User-Agent': 'niantic'}
r = SESSION.get(LOGIN_URL, headers=head)
jdata = json.loads(r.content)
data = {
'lt': jdata['lt'],
'execution': jdata['execution'],
'_eventId': 'submit',
'username': username,
'password': password,
}
r1 = SESSION.post(LOGIN_URL, data=data, headers=head)
ticket = None
try:
ticket = re.sub('.*ticket=', '', r1.history[0].headers['Location'])
except Exception,e:
if DEBUG:
print(r1.json()['errors'][0])
return None
data1 = {
'client_id': 'mobile-app_pokemon-go',
'redirect_uri': 'https://www.nianticlabs.com/pokemongo/error',
'client_secret': PTC_CLIENT_SECRET,
'grant_type': 'refresh_token',
'code': ticket,
}
r2 = SESSION.post(LOGIN_OAUTH, data=data1)
access_token = re.sub('&expires.*', '', r2.content)
access_token = re.sub('.*access_token=', '', access_token)
return access_token
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--username", help="PTC Username", required=True)
parser.add_argument("-p", "--password", help="PTC Password", required=True)
parser.add_argument("-l", "--location", help="Location", required=True)
parser.add_argument("-d", "--debug", help="Debug Mode", action='store_true')
parser.add_argument("-s", "--client_secret", help="PTC Client Secret")
parser.set_defaults(DEBUG=True)
args = parser.parse_args()
if args.debug:
global DEBUG
DEBUG = True
print('[!] DEBUG mode on')
if args.client_secret is not None:
global PTC_CLIENT_SECRET
PTC_CLIENT_SECRET = args.client_secret
set_location(args.location)
access_token = login_ptc(args.username, args.password)
if access_token is None:
print('[-] Wrong username/password')
return
print('[+] RPC Session Token: {} ...'.format(access_token[:25]))
api_endpoint = get_api_endpoint(access_token)
if api_endpoint is None:
print('[-] RPC server offline')
return
print('[+] Received API endpoint: {}'.format(api_endpoint))
profile = get_profile(api_endpoint, access_token)
if profile is not None:
print('[+] Login successful')
profile = profile.payload[0].profile
print('[+] Username: {}'.format(profile.username))
creation_time = datetime.fromtimestamp(int(profile.creation_time)/1000)
print('[+] You are playing Pokemon Go since: {}'.format(
creation_time.strftime('%Y-%m-%d %H:%M:%S'),
))
print('[+] Poke Storage: {}'.format(profile.poke_storage))
print('[+] Item Storage: {}'.format(profile.item_storage))
for curr in profile.currency:
print('[+] {}: {}'.format(curr.type, curr.amount))
else:
print('[-] Ooops...')
if __name__ == '__main__':
main()