From 0e68c21ae936f2f5025d5fc30ab394d70d2b5429 Mon Sep 17 00:00:00 2001 From: zharkovstas Date: Sat, 18 May 2019 23:03:56 +0500 Subject: [PATCH] Add street tips --- app.py | 28 ++++++++++++++++++++++------ osm/osm.py | 5 +++-- requirements.txt | 3 +++ street_predictor.py | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 street_predictor.py diff --git a/app.py b/app.py index c62c710..30de07c 100644 --- 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} diff --git a/osm/osm.py b/osm/osm.py index 0f8aa58..17d92bf 100644 --- a/osm/osm.py +++ b/osm/osm.py @@ -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 diff --git a/requirements.txt b/requirements.txt index e27a415..67079f7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +beautifulsoup4==4.7.1 bottle==0.12.16 certifi==2019.3.9 chardet==3.0.4 @@ -6,5 +7,7 @@ osmapi==1.2.2 Paste==3.0.8 requests==2.21.0 six==1.12.0 +soupsieve==1.9.1 urllib3==1.25.2 virtualenv==15.1.0 +wikipedia==1.4.0 diff --git a/street_predictor.py b/street_predictor.py new file mode 100644 index 0000000..bfea4b2 --- /dev/null +++ b/street_predictor.py @@ -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('Бебеля')) -- 2.50.1