Skip to content

Commit

Permalink
Throttling api requests. Reverting log change (#1562)
Browse files Browse the repository at this point in the history
  • Loading branch information
elicwhite authored and solderzzc committed Jul 29, 2016
1 parent dd15c06 commit f78ea35
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pokemongo_bot/api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from pgoapi import PGoApi
from pgoapi.exceptions import NotLoggedInException
from human_behaviour import sleep
import time
import logger

class ApiWrapper(object):
def __init__(self, api):
self._api = api
self.request_callers = []
self.reset_auth()
self.last_api_request_time = None
self.requests_per_seconds = 2

def reset_auth(self):
self._api._auth_token = None
Expand Down Expand Up @@ -54,22 +57,26 @@ def call(self, max_retry=5):
if not self._can_call():
return False # currently this is never ran, exceptions are raised before

request_timestamp = None

api_req_method_list = self._api._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()
if not self._is_response_valid(result, request_callers):
try_cnt += 1
if try_cnt > 1:
logger.log('Server seems to be busy or offline - try again - {}/{}'.format(try_cnt, max_retry), 'red')

logger.log('Server seems to be busy or offline - try again - {}/{}'.format(try_cnt, max_retry), 'red')

if try_cnt >= max_retry:
raise ServerBusyOrOfflineException()
sleep(1)
else:
break

self.last_api_request_time = request_timestamp
return result

def login(self, provider, username, password):
Expand All @@ -82,5 +89,14 @@ def __getattr__(self, func):
self.request_callers.append(func)
return getattr(self._api, func)

def throttle_sleep(self):
now_milliseconds = time.time() * 1000
required_delay_between_requests = 1000 / self.requests_per_seconds

difference = now_milliseconds - (self.last_api_request_time if self.last_api_request_time else 0)

if (self.last_api_request_time != None and difference < required_delay_between_requests):
sleep_time = required_delay_between_requests - difference
time.sleep(sleep_time / 1000)

return now_milliseconds

0 comments on commit f78ea35

Please sign in to comment.