From 53785cae80a20c2fc841853b786316cd6dcdecc3 Mon Sep 17 00:00:00 2001 From: Ovidiu Ghinet Date: Thu, 1 Sep 2016 02:15:08 +0200 Subject: [PATCH] handle IndexError in Polyline do not raise --- pokemongo_bot/walkers/polyline_generator.py | 30 +++++++++------------ pokemongo_bot/walkers/polyline_walker.py | 4 --- pokemongo_bot/walkers/walker_factory.py | 5 +--- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/pokemongo_bot/walkers/polyline_generator.py b/pokemongo_bot/walkers/polyline_generator.py index 394134ec07..2068bcc5ed 100644 --- a/pokemongo_bot/walkers/polyline_generator.py +++ b/pokemongo_bot/walkers/polyline_generator.py @@ -59,28 +59,22 @@ def __init__(self, origin, destination, speed): self.directions_response = requests.get(self.DIRECTIONS_URL).json() try: - # Polyline walker starts teleporting after reaching api query limit. - # throw error here atm, catch it at factory and return StepWalker - # - # In case of API limit reached we get back we get a status 200 code with an empty routes [] - # - # {u'error_message': u'You have exceeded your rate-limit for this API.', - # u'routes': [], - # u'status': u'OVER_QUERY_LIMIT' - # } - self.polyline_points = [x['polyline']['points'] for x in self.directions_response['routes'][0]['legs'][0]['steps']] - # This handles both cases: - # a) the above API Quota reached self.directions_response['routes'] = [] - # b) ZERO_RESULTS { - # "geocoded_waypoints" : [ {}, {} ], - # "routes" : [], - # "status" : "ZERO_RESULTS" - # } except IndexError: + # This handles both cases: + # a) In case of API limit reached we get back we get a status 200 code with an empty routes [] + # {u'error_message': u'You have exceeded your rate-limit for this API.', + # u'routes': [], + # u'status': u'OVER_QUERY_LIMIT' + # } + # b) In case that google does not have any directions proposals we get: + # ZERO_RESULTS { + # "geocoded_waypoints" : [ {}, {} ], + # "routes" : [], + # "status" : "ZERO_RESULTS" + # } self.polyline_points = self.directions_response['routes'] - raise # catch at factory atm... self.points = [self.origin] + self.get_points(self.polyline_points) + [self.destination] self.speed = float(speed) self.lat, self.long = self.points[0][0], self.points[0][1] diff --git a/pokemongo_bot/walkers/polyline_walker.py b/pokemongo_bot/walkers/polyline_walker.py index 16bbd4a98f..883eb10863 100644 --- a/pokemongo_bot/walkers/polyline_walker.py +++ b/pokemongo_bot/walkers/polyline_walker.py @@ -8,10 +8,6 @@ from pokemongo_bot.constants import Constants class PolylineWalker(StepWalker): - ''' - Heavy multi-botting can cause issues, since the directions API has limits. - StepWalker is generated by the factory in the case. - ''' def __init__(self, bot, dest_lat, dest_lng): super(PolylineWalker, self).__init__(bot, dest_lat, dest_lng) diff --git a/pokemongo_bot/walkers/walker_factory.py b/pokemongo_bot/walkers/walker_factory.py index 2124592f1c..03a2f4e8c7 100644 --- a/pokemongo_bot/walkers/walker_factory.py +++ b/pokemongo_bot/walkers/walker_factory.py @@ -8,8 +8,5 @@ def walker_factory(name, bot, dest_lat, dest_lng, dest_alt=None, *args, **kwargs if 'StepWalker' == name: ret = StepWalker(bot, dest_lat, dest_lng, dest_alt) elif 'PolylineWalker' == name: - try: - ret = PolylineWalker(bot, dest_lat, dest_lng) - except: - ret = StepWalker(bot, dest_lat, dest_lng, dest_alt) + ret = PolylineWalker(bot, dest_lat, dest_lng) return ret