]> git.xn--bdkaa.com Git - where-are-you.py.git/commitdiff
Return location name after finish
authorzharkovstas <zharkovstas@skbkontur.ru>
Sat, 18 May 2019 21:25:32 +0000 (02:25 +0500)
committerzharkovstas <zharkovstas@skbkontur.ru>
Sat, 18 May 2019 21:25:45 +0000 (02:25 +0500)
app.py
requirements.txt
yandex.py [new file with mode: 0644]

diff --git a/app.py b/app.py
index 95f0c3b2396e4d2d3edbb457cc0b2f0209f0090b..2b9c45a6ca4cf4a02c59096655ec57edce38f093 100644 (file)
--- 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/<game_id>')
 @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/<game_id>/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/<game_id>/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/<game_id>/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('/')
index 67079f752f31c032ecb8ae97224fee063b47be1c..407b87f4789bfb0c30d01918fb95070c59e31c16 100644 (file)
@@ -8,6 +8,7 @@ Paste==3.0.8
 requests==2.21.0\r
 six==1.12.0\r
 soupsieve==1.9.1\r
-urllib3==1.25.2\r
+urllib3==1.24.3\r
 virtualenv==15.1.0\r
 wikipedia==1.4.0\r
+yandex-geocoder==1.0.0\r
diff --git a/yandex.py b/yandex.py
new file mode 100644 (file)
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
+
+