Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Slow down retry intervals, clear session when encountering errors
Browse files Browse the repository at this point in the history
  • Loading branch information
karlhe committed Jul 23, 2016
1 parent 6378154 commit c5eb83e
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@

global_password = None
global_token = None
access_token = None
DEBUG = True
VERBOSE_DEBUG = False # if you want to write raw request/response to the console
COORDS_LATITUDE = 0
Expand All @@ -66,7 +65,6 @@
NEXT_LONG = 0
auto_refresh = 0
default_step = 0.001
api_endpoint = None
pokemons = {}
gyms = {}
pokestops = {}
Expand All @@ -80,9 +78,11 @@
is_ampm_clock = False

# stuff for in-background search thread

search_thread = None

# Login session
login_session = None

def memoize(obj):
cache = obj.cache = {}

Expand Down Expand Up @@ -208,7 +208,7 @@ def retrying_api_req(service, api_endpoint, access_token, *args, **kwargs):
except (InvalidURL, ConnectionError, DecodeError), e:
debug('retrying_api_req: request error ({}), retrying'.format(
str(e)))
time.sleep(1)
time.sleep(5)


def api_req(service, api_endpoint, access_token, *args, **kwargs):
Expand Down Expand Up @@ -262,11 +262,12 @@ def get_api_endpoint(service, access_token, api=API_URL):
debug(
'retrying_get_profile: get_profile returned no api_url, retrying')
profile_response = None
continue
if not len(profile_response.api_url):
time.sleep(10)
elif not len(profile_response.api_url):
debug(
'get_api_endpoint: retrying_get_profile returned no-len api_url, retrying')
profile_response = None
time.sleep(10)

return 'https://%s/rpc' % profile_response.api_url

Expand All @@ -279,11 +280,12 @@ def retrying_get_profile(service, access_token, api, useauth, *reqq):
debug(
'retrying_get_profile: get_profile returned no payload, retrying')
profile_response = None
continue
if not profile_response.payload:
time.sleep(10)
elif not profile_response.payload:
debug(
'retrying_get_profile: get_profile returned no-len payload, retrying')
profile_response = None
time.sleep(10)

return profile_response

Expand Down Expand Up @@ -467,8 +469,11 @@ def get_args():
vars(namespace)[key] = default_args[key]
return namespace

@memoize
def login(args):
global login_session
if login_session:
return login_session

global global_password
if not global_password:
if args.password:
Expand Down Expand Up @@ -509,7 +514,8 @@ def login(args):
for curr in profile.profile.currency:
print '[+] {}: {}'.format(curr.type, curr.amount)

return api_endpoint, access_token, profile_response
login_session = api_endpoint, access_token, profile_response
return login_session

def main():
full_path = os.path.realpath(__file__)
Expand Down Expand Up @@ -649,6 +655,10 @@ def process_step(args, api_endpoint, access_token, profile_response,
Fort.Longitude, expire_time]
except AttributeError, e:
debug("Encountered error while searching for pokemon: {}".format(str(e)))
# Reset login session if problems happen
global login_session, global_token
login_session = None
global_token = None
break

for poke in visible:
Expand Down

0 comments on commit c5eb83e

Please sign in to comment.