From 105bdd6a8511e459ca711e568c990637cfdac4a0 Mon Sep 17 00:00:00 2001 From: zharkovstas Date: Sun, 19 May 2019 02:25:32 +0500 Subject: [PATCH] Return location name after finish --- app.py | 38 ++++++++++++++++++++++++++++---------- requirements.txt | 3 ++- yandex.py | 12 ++++++++++++ 3 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 yandex.py diff --git a/app.py b/app.py index 95f0c3b..2b9c45a 100644 --- a/app.py +++ b/app.py @@ -7,7 +7,10 @@ from street_predictor import parse_summary from osm.osm import describe_objects +from yandex import get_text_by_coordinates + import itertools +import bottle min_lat, max_lat, min_lon, max_lon = 56.807556, 56.847826, 60.570744, 60.657791 move_distance = 300 @@ -182,6 +185,10 @@ def post_game(): @get('/api/games/') @enable_cors def get_game(game_id): + + if not game_id in games: + return bottle.HTTPResponse(status=404, body='game not found') + game = games[game_id] return { @@ -200,6 +207,10 @@ def get_game(game_id): @post('/api/games//ask-tip') @enable_cors def get_game(game_id): + + if not game_id in games: + return bottle.HTTPResponse(status=404, body='game not found') + game = games[game_id] show_tips(game, 1) @@ -209,10 +220,17 @@ def get_game(game_id): @post('/api/games//move') @enable_cors def get_game(game_id): + + if not game_id in games: + return bottle.HTTPResponse(status=404, body='game not found') + game = games[game_id] direction = request.json['direction'] + if not direction: + return bottle.HTTPResponse(status=400, body='direction is required') + game.move(direction) game.tips = [] @@ -229,13 +247,11 @@ def get_game(game_id): @post('/api/games//finish') @enable_cors def finish_game(game_id): - game = games[game_id] - if game.is_finished: - return { - "right_coordinates": game.current_coordinates, - "distance": game.distance - } + if not game_id in games: + return bottle.HTTPResponse(status=404, body='game not found') + + game = games[game_id] answer = request.json @@ -243,13 +259,15 @@ def finish_game(game_id): d = distance(game.current_coordinates, answer_coordinates) - game.is_finished = True - game.answer_coordinates = answer_coordinates - game.distance = d + if not game.is_finished: + game.is_finished = True + game.answer_coordinates = answer_coordinates + game.distance = d return { "right_coordinates": game.current_coordinates, - "distance": d + "distance": d, + "address": get_text_by_coordinates(game.current_coordinates) } @get('/') diff --git a/requirements.txt b/requirements.txt index 67079f7..407b87f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,7 @@ Paste==3.0.8 requests==2.21.0 six==1.12.0 soupsieve==1.9.1 -urllib3==1.25.2 +urllib3==1.24.3 virtualenv==15.1.0 wikipedia==1.4.0 +yandex-geocoder==1.0.0 diff --git a/yandex.py b/yandex.py new file mode 100644 index 0000000..75d5fec --- /dev/null +++ b/yandex.py @@ -0,0 +1,12 @@ +from yandex_geocoder import Client + + +def get_text_by_coordinates(coordinates): + try: + result = Client.request(f'{coordinates[1]}, {coordinates[0]}') + return result['GeoObjectCollection']['featureMember'][0]['GeoObject']['name'] + except Exception as e: + print(e) + return None + + -- 2.50.1