]> git.xn--bdkaa.com Git - where-are-you.py.git/commitdiff
Add street tips
authorzharkovstas <zharkovstas@skbkontur.ru>
Sat, 18 May 2019 18:03:56 +0000 (23:03 +0500)
committerzharkovstas <zharkovstas@skbkontur.ru>
Sat, 18 May 2019 18:04:07 +0000 (23:04 +0500)
app.py
osm/osm.py
requirements.txt
street_predictor.py [new file with mode: 0644]

diff --git a/app.py b/app.py
index c62c71028a5cd36ba373e65f47d67896f320158f..30de07cd57fa0c9fbcac5a3624bd8fa07424ed0b 100644 (file)
--- a/app.py
+++ b/app.py
@@ -1,9 +1,13 @@
+from random import shuffle, random
+
 from bottle import get, post, run, request
 import uuid
 from geo import distance
+from street_predictor import parse_summary
 
 from osm.osm import describe_objects
 
+min_lat, max_lat, min_lon, max_lon = 56.807556, 56.847826, 60.570744, 60.657791
 
 class Game:
     def __init__(self, game_id, current_coordinates):
@@ -18,13 +22,22 @@ class Game:
 
 def add_tips(game):
     radius = 0.0025
-    cooridnate = game.current_coordinates
-    objects = describe_objects(cooridnate[0] - radius, cooridnate[1] - radius, cooridnate[0] + radius,
-                               cooridnate[1] + radius)
+    coordinate = game.current_coordinates
+    near_objects = describe_objects(coordinate[0] - radius, coordinate[1] - radius, coordinate[0] + radius,
+                               coordinate[1] + radius)
 
-    for o in objects['amenities']:
+    for o in near_objects['amenities']:
         game.tips.append(f'Рядом с вами находится {o["name"]}')
 
+    for s in near_objects['streets']:
+
+        success, summary = parse_summary(s['name'].replace('улица', '').replace('проспект', '').replace('переулок', '').strip())
+
+        if success:
+            game.tips.append(f'Недалеко есть улица, имя которой дал(а) {summary}')
+
+    shuffle(game.tips)
+
 
 def show_tips(game, count):
     not_shown_tips = [tip for tip in game.tips if tip not in game.shown_tips]
@@ -33,9 +46,12 @@ def show_tips(game, count):
         game.shown_tips.append(not_shown_tips[i])
 
 
-test_game = Game('test', (56.832469, 60.605989))
+lat = min_lat + (max_lat - min_lat) * random()
+lon = min_lon + (max_lon - min_lon) * random()
+
+test_game = Game('test', (lat, lon))
 add_tips(test_game)
-show_tips(test_game, 2)
+show_tips(test_game, 20)
 games = {'test': test_game}
 
 
index 0f8aa58d3a3463a14c79ecb973833f5b1f8f45d8..17d92bf0e1261fc5e6da8434d2a0ca86c1a4240b 100644 (file)
@@ -62,5 +62,6 @@ def describe_objects(min_lat, min_lon, max_lat, max_lon):
             'amenities': amenities}
 
 
-objects = describe_objects(min_lat, min_lon, max_lat, max_lon)
-pp.pprint(objects)
\ No newline at end of file
+if __name__ == '__main__':
+    objects = describe_objects(min_lat, min_lon, max_lat, max_lon)
+    pp.pprint(objects)
\ No newline at end of file
index e27a4155b068652eea7df4363fb679c37e013fa9..67079f752f31c032ecb8ae97224fee063b47be1c 100644 (file)
@@ -1,3 +1,4 @@
+beautifulsoup4==4.7.1\r
 bottle==0.12.16\r
 certifi==2019.3.9\r
 chardet==3.0.4\r
@@ -6,5 +7,7 @@ osmapi==1.2.2
 Paste==3.0.8\r
 requests==2.21.0\r
 six==1.12.0\r
+soupsieve==1.9.1\r
 urllib3==1.25.2\r
 virtualenv==15.1.0\r
+wikipedia==1.4.0\r
diff --git a/street_predictor.py b/street_predictor.py
new file mode 100644 (file)
index 0000000..bfea4b2
--- /dev/null
@@ -0,0 +1,18 @@
+import re
+import wikipedia as w
+
+w.set_lang('ru')
+
+
+def parse_summary(query='str'):
+    print('query: ' + query)
+    try:
+        summary = w.summary(query)
+        full_description = re.split(r'\) —', summary)[1]
+        description = re.split(r'\.', full_description)[0]
+        return True, description.strip()
+    except Exception:
+        return False, ''
+
+if __name__ == '__main__':
+    print(parse_summary('Бебеля'))