From fb35a947922df5749aa71b3e58276ceef1051ab3 Mon Sep 17 00:00:00 2001 From: Mike DX Date: Sun, 24 Jul 2016 20:09:51 -0400 Subject: [PATCH 1/4] Attempting to solve the stale token issue by re-issuing if the token has less than 60 seconds until expiry --- pokemongo_bot/__init__.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index d6df1a38d7..6d20cf5e37 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -6,6 +6,7 @@ import json import random import threading +import time import datetime import sys import yaml @@ -37,6 +38,13 @@ def take_step(self): self.stepper.take_step() def work_on_cell(self, cell, position, include_fort_on_path): + # Check session expiry + if self.api._auth_provider and self.api._auth_provider._ticket_expire: + remaining_time = self.api._auth_provider._ticket_expire/1000 - time.time() + + logger.log("Session stale, re-logging in", 'yellow') + self.login() + if self.config.evolve_all: # Run evolve all once. Flip the bit. print('[#] Attempting to evolve all pokemons ...') @@ -117,6 +125,19 @@ def _setup_logging(self): logging.getLogger("pgoapi").setLevel(logging.ERROR) logging.getLogger("rpc_api").setLevel(logging.ERROR) + def login(self): + logger.log('[#] Attempting login to Pokemon Go.', 'white') + + self.api.set_position(*self.position) + + while not self.api.login(self.config.auth_service, + str(self.config.username), + str(self.config.password)): + logger.log('[X] Login Error, server busy', 'red') + time.sleep(10) + + logger.log('[+] Login to Pokemon Go successful.', 'green') + def _setup_api(self): # instantiate pgoapi self.api = PGoApi() @@ -133,11 +154,7 @@ def _setup_api(self): # provide player position on the earth self._set_starting_position() - if not self.api.login(self.config.auth_service, - str(self.config.username), - str(self.config.password)): - logger.log('Login Error, server busy', 'red') - exit(0) + self.login() # chain subrequests (methods) into one RPC call From 9e22c4d912c665f8ed5dedf9e102f278f57ad40f Mon Sep 17 00:00:00 2001 From: Mike DX Date: Sun, 24 Jul 2016 20:14:24 -0400 Subject: [PATCH 2/4] Fixes remaing time check --- pokemongo_bot/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 6d20cf5e37..270a03c35c 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -42,8 +42,9 @@ def work_on_cell(self, cell, position, include_fort_on_path): if self.api._auth_provider and self.api._auth_provider._ticket_expire: remaining_time = self.api._auth_provider._ticket_expire/1000 - time.time() - logger.log("Session stale, re-logging in", 'yellow') - self.login() + if remaining_time < 60: + logger.log("Session stale, re-logging in", 'yellow') + self.login() if self.config.evolve_all: # Run evolve all once. Flip the bit. From e8e146ccfdd78906be3f6a43ba461c49af1773d4 Mon Sep 17 00:00:00 2001 From: Mike DX Date: Sun, 24 Jul 2016 22:31:45 -0400 Subject: [PATCH 3/4] Reset pgoapi when rpc token is stale --- pokemongo_bot/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 270a03c35c..a0cdab3053 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -44,8 +44,9 @@ def work_on_cell(self, cell, position, include_fort_on_path): if remaining_time < 60: logger.log("Session stale, re-logging in", 'yellow') + self.position = position self.login() - + if self.config.evolve_all: # Run evolve all once. Flip the bit. print('[#] Attempting to evolve all pokemons ...') @@ -128,13 +129,17 @@ def _setup_logging(self): def login(self): logger.log('[#] Attempting login to Pokemon Go.', 'white') - + self.api._auth_token = None + self.api._auth_provider = None + self.api._api_endpoint = None self.api.set_position(*self.position) while not self.api.login(self.config.auth_service, str(self.config.username), str(self.config.password)): + logger.log('[X] Login Error, server busy', 'red') + logger.log('[X] Waiting 10 seconds to try again', 'red') time.sleep(10) logger.log('[+] Login to Pokemon Go successful.', 'green') From f966c87c464f9b865b7ebe22d9b4723ca9c219dc Mon Sep 17 00:00:00 2001 From: Mike DX Date: Mon, 25 Jul 2016 06:27:53 -0400 Subject: [PATCH 4/4] Refactored session expiry check --- pokemongo_bot/__init__.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 64c344e819..ec3ca0dafa 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -71,14 +71,8 @@ def find_close_cells(self, lat, lng): return map_cells def work_on_cell(self, cell, position): - # Check session expiry - if self.api._auth_provider and self.api._auth_provider._ticket_expire: - remaining_time = self.api._auth_provider._ticket_expire/1000 - time.time() - - if remaining_time < 60: - logger.log("Session stale, re-logging in", 'yellow') - self.position = position - self.login() + # Check if session token has expired + self.check_session(location) if self.config.evolve_all: # Will skip evolving if user wants to use an egg and there is none @@ -189,6 +183,17 @@ def _setup_logging(self): logging.getLogger("pgoapi").setLevel(logging.ERROR) logging.getLogger("rpc_api").setLevel(logging.ERROR) + def check_session(self, position): + # Check session expiry + if self.api._auth_provider and self.api._auth_provider._ticket_expire: + remaining_time = self.api._auth_provider._ticket_expire/1000 - time.time() + + if remaining_time < 60: + logger.log("Session stale, re-logging in", 'yellow') + self.position = position + self.login() + + def login(self): logger.log('[#] Attempting login to Pokemon Go.', 'white') self.api._auth_token = None