Skip to content

Commit

Permalink
Updated api wrapper to handle pgoapi updates
Browse files Browse the repository at this point in the history
  • Loading branch information
reddivision committed Aug 2, 2016
1 parent a05a6db commit ec893a8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
38 changes: 31 additions & 7 deletions pokemongo_bot/api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

from pgoapi.exceptions import (NotLoggedInException,
ServerBusyOrOfflineException)

try:
from pgoapi.exceptions import ServerSideRequestThrottlingException
from pgoapi.pgoapi import PGoApiRequest
except ImportError:
pass # frig off, Lahey
import logger
from human_behaviour import sleep

Expand All @@ -14,15 +18,20 @@ def __init__(self, api):
self.reset_auth()
self.last_api_request_time = None
self.requests_per_seconds = 2
self.current_request = None
try:
self.useNewRequest = self._api.create_request is not None
except AttributeError:
self.useNewRequest = False

def reset_auth(self):
self._api._auth_token = None
self._api._auth_provider = None
self._api._api_endpoint = None

# wrap api methods
def _can_call(self):
if not self._api._req_method_list or len(self._api._req_method_list) == 0:
request_holder = self.current_request if self.useNewRequest else self._api
if not request_holder._req_method_list or len(request_holder._req_method_list) == 0:
raise RuntimeError('Trying to call the api without setting any request')
if self._api._auth_provider is None or not self._api._auth_provider.is_login():
logger.log('Not logged in!', 'red')
Expand Down Expand Up @@ -58,14 +67,24 @@ def call(self, max_retry=5):
return False # currently this is never ran, exceptions are raised before

request_timestamp = None

api_req_method_list = self._api._req_method_list
request_holder = self.current_request if self.useNewRequest else self._api
api_req_method_list = request_holder._req_method_list
result = None
try_cnt = 0
while True:
request_timestamp = self.throttle_sleep()
self._api._req_method_list = [req_method for req_method in api_req_method_list] # api internally clear this field after a call
result = self._api.call()
request_holder._req_method_list = [req_method for req_method in api_req_method_list] # api internally clear this field after a call
if self.useNewRequest:
try:
result = self.current_request.call()
except ServerSideRequestThrottlingException:
logger.log('Request throttled by server. Trying again...','red')
try_cnt += 1
if try_cnt >= max_retry:
raise ServerBusyOrOfflineException()
continue
else:
result = self._api.call()
if not self._is_response_valid(result, request_callers):
try_cnt += 1
logger.log('Server seems to be busy or offline - try again - {}/{}'.format(try_cnt, max_retry), 'red')
Expand All @@ -75,6 +94,7 @@ def call(self, max_retry=5):
sleep(1)
else:
break
self.current_request = None

self.last_api_request_time = request_timestamp
return result
Expand All @@ -87,6 +107,10 @@ def __getattr__(self, func):
DEFAULT_ATTRS = ['_position_lat', '_position_lng', '_auth_provider', '_api_endpoint', 'set_position', 'get_position']
if func not in DEFAULT_ATTRS:
self.request_callers.append(func)
if self.useNewRequest:
if not self.current_request:
self.current_request = self._api.create_request()
return getattr(self.current_request, func)
return getattr(self._api, func)

def throttle_sleep(self):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
numpy==1.11.0
networkx==1.11
-e git+https://github.com/tejado/pgoapi.git@81e786cabf027a1c8fbd1e9a07e1c11aa3d8ee8b#egg=pgoapi
-e git+https://github.com/tejado/pgoapi.git#egg=pgoapi
geopy==1.11.0
protobuf==3.0.0b4
requests==2.10.0
Expand Down

0 comments on commit ec893a8

Please sign in to comment.