diff --git a/aiopogo/__init__.py b/aiopogo/__init__.py old mode 100755 new mode 100644 index 16b82f7b..054baaad --- a/aiopogo/__init__.py +++ b/aiopogo/__init__.py @@ -41,6 +41,6 @@ def close_sessions(): HashServer.close_session() -def activate_hash_server(hash_token, conn_limit=300): +def activate_hash_server(hash_token, conn_limit=300, go_hash=False): HashServer.set_token(hash_token) - HashServer.activate_session(conn_limit) + HashServer.activate_session(conn_limit, go_hash) diff --git a/aiopogo/hash_server.py b/aiopogo/hash_server.py index 1b408ffa..fedd7f96 100644 --- a/aiopogo/hash_server.py +++ b/aiopogo/hash_server.py @@ -16,9 +16,11 @@ class HashServer: _session = None multi = False + goHash = False loop = get_event_loop() status = {} log = getLogger('hashing') + endPointUrl = "http://pokehash.buddyauth.com/api/v137_1/hash" def __init__(self): try: @@ -38,12 +40,20 @@ async def hash(self, timestamp, latitude, longitude, accuracy, authticket, sessi iteration += 1 else: self.log.info('Out of hashes, waiting for new period.') - await sleep(status['refresh'] - time() + 1, loop=self.loop) - break + if not self.goHash: + await sleep(status['refresh'] - time() + 1, loop=self.loop) + break + # Go Hash doesn't have an expiry period so you would top up credit, we will try again in 5 seconds + else: + await sleep(5, loop=self.loop) + break except KeyError: pass - headers = {'X-AuthToken': self.instance_token} - + + headers = {'X-AuthToken': self.instance_token} + if self.goHash: + # extra header ensures no more than 5000 hashes per minute when using Go Hash. You can up this if your CPU can handle it + headers = {'X-AuthToken': self.instance_token, 'X-RateLimit':'5000'} payload = { 'Timestamp': timestamp, 'Latitude64': f2i(latitude), @@ -57,7 +67,7 @@ async def hash(self, timestamp, latitude, longitude, accuracy, authticket, sessi # request hashes from hashing server for attempt in range(3): try: - async with self._session.post("http://pokehash.buddyauth.com/api/v137_1/hash", headers=headers, json=payload) as resp: + async with self._session.post(self.endPointUrl, headers=headers, json=payload) as resp: if resp.status == 400: status['failures'] += 1 @@ -88,11 +98,23 @@ async def hash(self, timestamp, latitude, longitude, accuracy, authticket, sessi except ClientResponseError as e: if e.code == 403: raise TempHashingBanException('Your IP was temporarily banned for sending too many requests with invalid keys') - elif e.code == 429: + # allow for 429 from bossland AND for 430 from goHash + elif e.code == 429 or e.code == 430: status['remaining'] = 0 self.instance_token = self.auth_token + if e.code == 429: + if self.goHash: + self.log.warning("Error 429 - Artificial hash limit reached, consider a higher value for X-RateLimit header in Go Hash request.") + else: + self.log.warning("Error 429 - Out of hashes for this period.") + else: + self.log.warning("Error 430 - No credit remaining on the Go Hash key.") return await self.hash(timestamp, latitude, longitude, accuracy, authticket, sessiondata, requests) elif e.code >= 500 or e.code == 404: + if e.code == 503 and self.goHash: + self.log.warning("Error 503 - Go Hash server cannot handle the load.") + if e.code == 549 or e.code == 550 and self.goHash: + self.log.warning("Error 549|550 something bad happened between Bossland and Go Hash not successful after multiple retries.") raise HashingOfflineException( 'Hashing server error {}: {}'.format( e.code, e.message)) @@ -140,9 +162,15 @@ def _multi_status(self): return self.key_statuses[self.instance_token] @classmethod - def activate_session(cls, conn_limit=300): + def activate_session(cls, conn_limit=300, go_hash=False): + cls.goHash = go_hash if cls._session and not cls._session.closed: return + if cls.goHash: + cls.endPointUrl = "http://hash.goman.io/api/v137_1/hash" + cls.log.warning("Hash server set to Go Hash mode. Please ensure you are using a Go Hash NOT a Bossland hash key.") + else: + cls.log.warning("Hash server set to Bossland mode. Please ensure you are using a Bossland NOT a Go Hash hash key.") conn = TimedConnector(loop=cls.loop, limit=conn_limit, verify_ssl=False) @@ -186,3 +214,4 @@ def set_token(cls, token): else: cls.auth_token = token cls.key_status = {'failures': 0} + \ No newline at end of file diff --git a/aiopogo/pgoapi.py b/aiopogo/pgoapi.py old mode 100755 new mode 100644 index 6c64fa96..4f106bc5 --- a/aiopogo/pgoapi.py +++ b/aiopogo/pgoapi.py @@ -67,9 +67,9 @@ def create_request(self): return PGoApiRequest(self) @staticmethod - def activate_hash_server(hash_token, conn_limit=300): + def activate_hash_server(hash_token, conn_limit=300, go_hash=False): HashServer.set_token(hash_token) - HashServer.activate_session(conn_limit) + HashServer.activate_session(conn_limit, go_hash) @property def position(self):