Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling KeyboardInterrupt and a few other exceptions #2599

Merged
merged 1 commit into from
Aug 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 74 additions & 71 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,79 +51,82 @@
logger.setLevel(logging.INFO)

def main():

logger.info('PokemonGO Bot v1.0')
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)

config = init_config()
if not config:
return

logger.info('Configuration initialized')
health_record = BotEvent(config)
health_record.login_success()

finished = False

while not finished:
try:
bot = PokemonGoBot(config)
bot.start()
tree = TreeConfigBuilder(bot, config.raw_tasks).build()
bot.workers = tree
bot.metrics.capture_stats()

bot.event_manager.emit(
'bot_start',
sender=bot,
level='info',
formatted='Starting bot...'
)

while True:
bot.tick()

except KeyboardInterrupt:
bot.event_manager.emit(
'bot_exit',
sender=bot,
level='info',
formatted='Exiting bot.'
)
finished = True
try:
logger.info('PokemonGO Bot v1.0')
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)

config = init_config()
if not config:
return

logger.info('Configuration initialized')
health_record = BotEvent(config)
health_record.login_success()

finished = False

while not finished:
try:
bot = PokemonGoBot(config)
bot.start()
tree = TreeConfigBuilder(bot, config.raw_tasks).build()
bot.workers = tree
bot.metrics.capture_stats()

bot.event_manager.emit(
'bot_start',
sender=bot,
level='info',
formatted='Starting bot...'
)

while True:
bot.tick()

except KeyboardInterrupt:
bot.event_manager.emit(
'bot_exit',
sender=bot,
level='info',
formatted='Exiting bot.'
)
finished = True
report_summary(bot)

except NotLoggedInException:
wait_time = config.reconnecting_timeout * 60
bot.event_manager.emit(
'api_error',
sender=bot,
level='info',
formmated='Log logged in, reconnecting in {:s}'.format(wait_time)
)
time.sleep(wait_time)
except ServerBusyOrOfflineException:
bot.event_manager.emit(
'api_error',
sender=bot,
level='info',
formatted='Server busy or offline'
)
except ServerSideRequestThrottlingException:
bot.event_manager.emit(
'api_error',
sender=bot,
level='info',
formatted='Server is throttling, reconnecting in 30 seconds'
)
time.sleep(30)

except GeocoderQuotaExceeded:
raise Exception("Google Maps API key over requests limit.")
except Exception as e:
# always report session summary and then raise exception
if bot:
report_summary(bot)

except NotLoggedInException:
wait_time = config.reconnecting_timeout * 60
bot.event_manager.emit(
'api_error',
sender=bot,
level='info',
formmated='Log logged in, reconnecting in {:s}'.format(wait_time)
)
time.sleep(wait_time)
except ServerBusyOrOfflineException:
bot.event_manager.emit(
'api_error',
sender=bot,
level='info',
formatted='Server busy or offline'
)
except ServerSideRequestThrottlingException:
bot.event_manager.emit(
'api_error',
sender=bot,
level='info',
formatted='Server is throttling, reconnecting in 30 seconds'
)
time.sleep(30)
except GeocoderQuotaExceeded:
raise "Google Maps API key over requests limit."
except Exception as e:
# always report session summary and then raise exception
report_summary(bot)
raise e
raise e

def report_summary(bot):
if bot.metrics.start_time is None:
Expand Down