def cleanup(overseer, manager): try: if hasattr(overseer, 'print_handle'): overseer.print_handle.cancel() if hasattr(overseer, 'worker30'): overseer.worker30.cancel() if hasattr(overseer, 'worker_raider'): overseer.worker_raider.cancel() overseer.running = False print('Exiting, please wait until all tasks finish') log = get_logger('cleanup') print('Finishing tasks...') LOOP.create_task(overseer.exit_progress()) pending = gather(*Task.all_tasks(loop=LOOP), return_exceptions=True) try: LOOP.run_until_complete(wait_for(pending, 40)) except TimeoutError as e: print('Coroutine completion timed out, moving on.') except Exception as e: log = get_logger('cleanup') log.exception('A wild {} appeared during exit!', e.__class__.__name__) db_proc.stop() overseer.refresh_dict() print('Dumping pickles...') dump_pickle('accounts', get_accounts()) dump_pickle('accounts30', get_accounts30()) FORT_CACHE.pickle() altitudes.pickle() if conf.CACHE_CELLS: dump_pickle('cells', Worker.cells) spawns.pickle() while not db_proc.queue.empty(): pending = db_proc.queue.qsize() # Spaces at the end are important, as they clear previously printed # output - \r doesn't clean whole line print('{} DB items pending '.format(pending), end='\r') sleep(.5) finally: print('Closing pipes, sessions, and event loop...') manager.shutdown() SessionManager.close() close_sessions() LOOP.close() print('Done.')
from monocle.accounts import get_accounts accounts_file = monocle_dir / 'accounts.csv' try: now = datetime.now().strftime("%Y-%m-%d-%H%M") accounts_file.rename('accounts-{}.csv'.format(now)) except FileNotFoundError: pass banned = [] with accounts_file.open('w') as csvfile: writer = csv.writer(csvfile, delimiter=',') writer.writerow(('username', 'password', 'provider', 'model', 'iOS', 'id')) ACCOUNTS = get_accounts() for account in ACCOUNTS.values(): if account.get('banned', False): banned.append(account) continue writer.writerow( (account['username'], account['password'], account['provider'], account['model'], account['iOS'], account['id'])) if banned: banned_file = monocle_dir / 'banned.csv' write_header = not banned_file.exists() with banned_file.open('a') as csvfile: writer = csv.writer(csvfile, delimiter=',') if write_header: writer.writerow(('username', 'password', 'provider', 'level',