forked from PokemonGoF/PokemonGo-Bot
/
bot.py
213 lines (180 loc) · 9.26 KB
/
bot.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
207
208
209
210
211
212
213
import logging
import googlemaps
import json
import random
import threading
import time
import datetime
from pgoapi import PGoApi
from pgoapi.utilities import f2i, h2f
from cell_workers import PokemonCatchWorker, SeenFortWorker
from stepper import Stepper
from geopy.geocoders import GoogleV3
from math import radians, sqrt, sin, cos, atan2
class PokemonGoBot(object):
def __init__(self, config):
self.config = config
self.pokemon_list=json.load(open('pokemon.json'))
self.item_list=json.load(open('items.json'))
def start(self):
self._setup_logging()
self._setup_api()
self.stepper = Stepper(self)
random.seed()
def take_step(self):
self.stepper.take_step()
def work_on_cell(self, cell, position):
if 'catchable_pokemons' in cell:
print 'Something rustles nearby!'
for pokemon in cell['catchable_pokemons']:
worker = PokemonCatchWorker(pokemon, self)
worker.work()
if 'wild_pokemons' in cell:
for pokemon in cell['wild_pokemons']:
worker = PokemonCatchWorker(pokemon, self)
worker.work()
if self.config.spinstop:
if 'forts' in cell:
# Only include those with a lat/long
forts = [fort for fort in cell['forts'] if 'latitude' in fort and 'type' in fort]
# Sort all by distance from current pos- eventually this should build graph & A* it
forts.sort(key=lambda x: SeenFortWorker.geocalc(self.position[0], self.position[1], fort['latitude'], fort['longitude']))
for fort in cell['forts']:
worker = SeenFortWorker(fort, self)
hack_chain = worker.work()
if hack_chain > 10:
print('need a rest')
break
def _setup_logging(self):
self.log = logging.getLogger(__name__)
# log settings
# log format
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(module)10s] [%(levelname)5s] %(message)s')
# log level for http request class
logging.getLogger("requests").setLevel(logging.WARNING)
# log level for main pgoapi class
logging.getLogger("pgoapi").setLevel(logging.INFO)
# log level for internal pgoapi class
logging.getLogger("rpc_api").setLevel(logging.INFO)
def _setup_api(self):
# instantiate pgoapi
self.api = PGoApi()
# provide player position on the earth
self._set_starting_position()
if not self.api.login(self.config.auth_service, self.config.username, self.config.password):
return
# chain subrequests (methods) into one RPC call
# get player profile call
# ----------------------
self.api.get_player()
response_dict = self.api.call()
#print('Response dictionary: \n\r{}'.format(json.dumps(response_dict, indent=2)))
currency_1="0"
currency_2="0"
player = response_dict['responses']['GET_PLAYER']['profile']
### @@@ TODO: Convert this to d/m/Y H:M:S
creation_date = datetime.datetime.fromtimestamp(player['creation_time'] / 1e3)
pokecoins = '0'
stardust = '0'
if 'amount' in player['currency'][0]:
pokecoins = player['currency'][0]['amount']
if 'amount' in player['currency'][1]:
stardust = player['currency'][1]['amount']
#try:
if 'amount' in response_dict['responses']['GET_PLAYER']['profile']['currency'][0]:
currency_1=response_dict['responses']['GET_PLAYER']['profile']['currency'][0]['amount']
if 'amount' in response_dict['responses']['GET_PLAYER']['profile']['currency'][1]:
currency_2=response_dict['responses']['GET_PLAYER']['profile']['currency'][1]['amount']
print('[#]')
print('[#] Username: ' + str(player['username']))
print('[#] Acccount Creation: ' + str(creation_date))
print('[#] Bag Storage: ' + str(self.getInventoryCount('item')) + '/' + str(player['item_storage']))
print('[#] Pokemon Storage: ' + str(self.getInventoryCount('pokemon')) + '/' + str(player['poke_storage']))
print('[#] Stardust: ' + str(stardust))
print('[#] Pokecoins: ' + str(pokecoins))
self.getPlayerInfo()
print('[#]')
# except:
# print('Exception during print player profile')
self.update_inventory();
def update_inventory(self):
self.api.get_inventory()
response = self.api.call()
self.inventory = list()
if 'responses' in response:
if 'GET_INVENTORY' in response['responses']:
if 'inventory_delta' in response['responses']['GET_INVENTORY']:
if 'inventory_items' in response['responses']['GET_INVENTORY']['inventory_delta']:
for item in response['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']:
if not 'inventory_item_data' in item:
continue
if not 'item' in item['inventory_item_data']:
continue
if not 'item' in item['inventory_item_data']['item']:
continue
if not 'count' in item['inventory_item_data']['item']:
continue
self.inventory.append(item['inventory_item_data']['item'])
def _set_starting_position(self):
self.position = self._get_pos_by_name(self.config.location)
self.api.set_position(*self.position)
print('[x] Address found: ' + self.config.location)
print('[x] Position in-game set as: ' + str(self.position))
if self.config.test:
return
def _get_pos_by_name(self, location_name):
geolocator = GoogleV3(api_key=self.config.gmapkey)
loc = geolocator.geocode(location_name)
print
#self.log.info('Your given location: %s', loc.address.encode('utf-8'))
#self.log.info('lat/long/alt: %s %s %s', loc.latitude, loc.longitude, loc.altitude)
return (loc.latitude, loc.longitude, loc.altitude)
###########################################
## @eggins pretty print functions
###########################################
## - Get count of inventory items and return the output for each
def getInventoryCount(self, what):
# Get contents of inventory
self.api.get_inventory()
response_dict = self.api.call()
if 'responses' in response_dict:
if 'GET_INVENTORY' in response_dict['responses']:
if 'inventory_delta' in response_dict['responses']['GET_INVENTORY']:
if 'inventory_items' in response_dict['responses']['GET_INVENTORY']['inventory_delta']:
pokecount = 0
itemcount = 1
for item in response_dict['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']:
#print('item {}'.format(item))
if 'inventory_item_data' in item:
if 'pokemon' in item['inventory_item_data']:
pokecount = pokecount + 1
if 'item' in item['inventory_item_data']:
if 'count' in item['inventory_item_data']['item']:
itemcount = itemcount + item['inventory_item_data']['item']['count']
if 'pokemon' in what:
return pokecount
if 'item' in what:
return itemcount
return '0'
## - Get more player information
def getPlayerInfo(self):
# Get contents of inventory
self.api.get_inventory()
response_dict = self.api.call()
if 'responses' in response_dict:
if 'GET_INVENTORY' in response_dict['responses']:
if 'inventory_delta' in response_dict['responses']['GET_INVENTORY']:
if 'inventory_items' in response_dict['responses']['GET_INVENTORY']['inventory_delta']:
pokecount = 0
itemcount = 1
for item in response_dict['responses']['GET_INVENTORY']['inventory_delta']['inventory_items']:
#print('item {}'.format(item))
if 'inventory_item_data' in item:
if 'player_stats' in item['inventory_item_data']:
playerdata = item['inventory_item_data']['player_stats']
nextlvlxp = (int(playerdata['next_level_xp']) - int(playerdata['experience']))
print('[#] -- Level: ' + str(playerdata['level']))
print('[#] -- Experience: ' + str(playerdata['experience']))
print('[#] -- Experience until next level: ' + str(nextlvlxp))
print('[#] -- Pokemon Captured: ' + str(playerdata['pokemons_captured']))
print('[#] -- Pokestops Visited: ' + str(playerdata['poke_stop_visits']))