forked from Noctem/Monocle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
executable file
·282 lines (240 loc) · 8.25 KB
/
scan.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3
import asyncio
try:
from uvloop import EventLoopPolicy
asyncio.set_event_loop_policy(EventLoopPolicy())
except ImportError:
pass
from multiprocessing.managers import BaseManager, DictProxy
from queue import Queue, Full
from argparse import ArgumentParser
from signal import signal, SIGINT, SIG_IGN
from logging import getLogger, basicConfig, WARNING, INFO
from pogo_async.session import Session
from os.path import exists, join
from threading import Thread
from sys import platform
import time
try:
from pokeminer import config
except ImportError as e:
raise ImportError('Please copy config.example.py to config.py and customize it.') from e
# Check whether config has all necessary attributes
_required = (
'DB_ENGINE',
'GRID',
'MAP_START',
'MAP_END'
)
for setting_name in _required:
if not hasattr(config, setting_name):
raise AttributeError('Please set "{}" in config'.format(setting_name))
# Set defaults for missing config options
_optional = {
'PROXIES': None,
'NOTIFY_IDS': None,
'NOTIFY_RANKING': None,
'CONTROL_SOCKS': None,
'HASH_KEY': None,
'MAX_CAPTCHAS': 0,
'ACCOUNTS': (),
'ENCOUNTER': None,
'NOTIFY': False,
'AUTHKEY': b'm3wtw0',
'SPIN_POKESTOPS': False,
'SPIN_COOLDOWN': 300,
'COMPLETE_TUTORIAL': False,
'INCUBATE_EGGS': False,
'MAP_WORKERS': True,
'APP_SIMULATION': True,
'ITEM_LIMITS': None,
'MAX_RETRIES': 3,
'MORE_POINTS': True,
'GIVE_UP_KNOWN': 75,
'GIVE_UP_UNKNOWN': 60,
'SKIP_SPAWN': 90,
'CACHE_CELLS': True,
'LOGIN_TIMEOUT': 2.5,
'PLAYER_LOCALE': {'country': 'US', 'language': 'en', 'timezone': 'America/Denver'},
'CAPTCHA_KEY': None,
'CAPTCHAS_ALLOWED': 3,
'DIRECTORY': None
}
for setting_name, default in _optional.items():
if not hasattr(config, setting_name):
setattr(config, setting_name, default)
del (_optional, _required)
# validate PROXIES input and cast to set if needed
if config.PROXIES:
if isinstance(config.PROXIES, (tuple, list)):
config.PROXIES = set(config.PROXIES)
elif isinstance(config.PROXIES, str):
config.PROXIES = {config.PROXIES}
elif not isinstance(config.PROXIES, set):
raise ValueError('PROXIES must be either a list, set, tuple, or str.')
# ensure that user's latitudes and longitudes are different
if (config.MAP_START[0] == config.MAP_END[0]
or config.MAP_START[1] == config.MAP_END[1]):
raise ValueError('The latitudes and longitudes of your MAP_START and MAP_END must differ.')
# disable bag cleaning if not spinning PokéStops
if config.ITEM_LIMITS and not config.SPIN_POKESTOPS:
config.ITEM_LIMITS = None
# ensure that numbers are valid
try:
if config.SCAN_DELAY < 10:
raise ValueError('SCAN_DELAY must be at least 10.')
except (TypeError, AttributeError):
config.SCAN_DELAY = 10
try:
if 1 > config.SPEED_LIMIT > 25:
raise ValueError('Speed limit should be between 1 and 25 MPH.')
except (TypeError, AttributeError):
config.SPEED_LIMIT = 19
try:
if config.SIMULTANEOUS_LOGINS < 1:
raise ValueError('SIMULTANEOUS_LOGINS must be at least 1.')
except (TypeError, AttributeError):
config.SIMULTANEOUS_LOGINS = 4
if config.ENCOUNTER not in (None, 'notifying', 'all'):
raise ValueError("Valid ENCOUNTER settings are: None, 'notifying', and 'all'")
if config.DIRECTORY is None:
if exists(join('..', 'pickles')):
config.DIRECTORY = '..'
else:
config.DIRECTORY = ''
from pokeminer.utils import get_address, dump_pickle
from pokeminer.worker import Worker
from pokeminer.overseer import Overseer
class AccountManager(BaseManager):
pass
class CustomQueue(Queue):
def full_wait(self, maxsize=0, timeout=None):
'''Block until queue size falls below maxsize'''
starttime = time.monotonic()
with self.not_full:
if maxsize > 0:
if timeout is None:
while self._qsize() >= maxsize:
self.not_full.wait()
elif timeout < 0:
raise ValueError("'timeout' must be a non-negative number")
else:
endtime = time.monotonic() + timeout
while self._qsize() >= maxsize:
remaining = endtime - time.monotonic()
if remaining <= 0.0:
raise Full
self.not_full.wait(remaining)
self.not_empty.notify()
endtime = time.monotonic()
return endtime - starttime
_captcha_queue = CustomQueue()
_extra_queue = Queue()
_worker_dict = {}
def get_captchas():
return _captcha_queue
def get_extras():
return _extra_queue
def get_workers():
return _worker_dict
def mgr_init():
signal(SIGINT, SIG_IGN)
def parse_args():
parser = ArgumentParser()
parser.add_argument(
'--no-status-bar',
dest='status_bar',
help='Log to console instead of displaying status bar',
action='store_false'
)
parser.add_argument(
'--log-level',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
default=WARNING
)
parser.add_argument(
'--bootstrap',
dest='bootstrap',
help='Bootstrap even if spawns are known.',
action='store_true'
)
parser.add_argument(
'--no-pickle',
dest='pickle',
help='Do not load spawns from pickle',
action='store_false'
)
return parser.parse_args()
def configure_logger(filename='scan.log'):
basicConfig(
filename=filename,
format=(
'[%(asctime)s][%(levelname)8s][%(name)s] '
'%(message)s'
),
style='%',
level=INFO,
)
def exception_handler(loop, context):
logger = getLogger('eventloop')
logger.exception('A wild exception appeared!')
logger.error(context)
def main():
args = parse_args()
logger = getLogger()
if args.status_bar:
configure_logger(filename=join(config.DIRECTORY, 'scan.log'))
logger.info('-' * 30)
logger.info('Starting up!')
else:
configure_logger(filename=None)
logger.setLevel(args.log_level)
AccountManager.register('captcha_queue', callable=get_captchas)
AccountManager.register('extra_queue', callable=get_extras)
if config.MAP_WORKERS:
AccountManager.register('worker_dict', callable=get_workers,
proxytype=DictProxy)
address = get_address()
manager = AccountManager(address=address, authkey=config.AUTHKEY)
try:
manager.start(mgr_init)
except (OSError, EOFError) as e:
if platform == 'win32' or not isinstance(address, str):
raise OSError('Another instance is running with the same manager address. Stop that process or change your MANAGER_ADDRESS.') from e
else:
raise OSError('Another instance is running with the same socket. Stop that process or: rm {}'.format(address)) from e
loop = asyncio.get_event_loop()
loop.set_exception_handler(exception_handler)
overseer = Overseer(status_bar=args.status_bar, manager=manager)
overseer.start()
overseer_thread = Thread(target=overseer.check, name='overseer', daemon=True)
overseer_thread.start()
launcher_thread = Thread(target=overseer.launch, name='launcher', daemon=True, args=(args.bootstrap, args.pickle))
launcher_thread.start()
try:
loop.run_forever()
except KeyboardInterrupt:
print('Exiting, please wait until all tasks finish')
overseer.kill()
dump_pickle('accounts', Worker.accounts)
if config.CACHE_CELLS:
dump_pickle('cells', Worker.cell_ids)
pending = asyncio.Task.all_tasks(loop=loop)
try:
loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
except Exception:
logger.exception('A wild exception appeared during exit!')
Worker.db_processor.stop()
try:
Worker.spawns.update()
except (ProgrammingError, OperationalError):
pass
Worker.spawns.session.close()
manager.shutdown()
Session.close()
try:
loop.close()
except RuntimeError:
pass
if __name__ == '__main__':
main()