From 49235ac71f590298f1b119047d26dd49753a1c7a Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 22 May 2022 13:37:57 +0100 Subject: [PATCH] Remember map site url and coords --- maps.py | 75 +++++++++++++++++++++++++++++++++++++++++-- webapp_create_post.py | 37 ++++++++++++++++++++- webapp_post.py | 29 +++++++++++++++++ 3 files changed, 138 insertions(+), 3 deletions(-) diff --git a/maps.py b/maps.py index c7e42f24a..8c4a52223 100644 --- a/maps.py +++ b/maps.py @@ -8,7 +8,11 @@ __status__ = "Production" __module_group__ = "Core" +import os from utils import is_float +from utils import acct_dir +from utils import load_json +from utils import save_json def _geocoords_from_osm_link(url: str, osm_domain: str) -> (int, float, float): @@ -190,7 +194,7 @@ def _geocoords_from_wego_link(url: str) -> (int, float, float): return zoom, latitude, longitude -def _geocoords_from_map_link(url: str, osm_domain: str) -> (int, float, float): +def geocoords_from_map_link(url: str, osm_domain: str) -> (int, float, float): """Returns geocoordinates from a map link url """ if osm_domain in url: @@ -212,7 +216,7 @@ def html_open_street_map(url: str, """Returns embed html for an OSM link """ osm_domain = 'openstreetmap.org' - zoom, latitude, longitude = _geocoords_from_map_link(url, osm_domain) + zoom, latitude, longitude = geocoords_from_map_link(url, osm_domain) if not latitude: return '' if not longitude: @@ -236,3 +240,70 @@ def html_open_street_map(url: str, str(zoom) + '/' + str(latitude) + '/' + str(longitude) + \ '">' + translate['View Larger Map'] + '\n' return html_str + + +def set_map_preferences_url(base_dir: str, nickname: str, domain: str, + maps_website_url: str) -> None: + """Sets the preferred maps website for an account + """ + maps_filename = \ + acct_dir(base_dir, nickname, domain) + '/map_preferences.json' + if os.path.isfile(maps_filename): + maps_json = load_json(maps_filename) + maps_json['url'] = maps_website_url + else: + maps_json = { + 'url': maps_website_url + } + save_json(maps_json, maps_filename) + + +def get_map_preferences_url(base_dir: str, nickname: str, domain: str) -> str: + """Gets the preferred maps website for an account + """ + maps_filename = \ + acct_dir(base_dir, nickname, domain) + '/map_preferences.json' + if os.path.isfile(maps_filename): + maps_json = load_json(maps_filename) + if maps_json.get('url'): + return maps_json['url'] + return None + + +def set_map_preferences_coords(base_dir: str, nickname: str, domain: str, + latitude: float, longitude: float, + zoom: int) -> None: + """Sets the preferred maps website coordinates for an account + """ + maps_filename = \ + acct_dir(base_dir, nickname, domain) + '/map_preferences.json' + if os.path.isfile(maps_filename): + maps_json = load_json(maps_filename) + maps_json['latitude'] = latitude + maps_json['longitude'] = longitude + maps_json['zoom'] = zoom + else: + maps_json = { + 'latitude': latitude, + 'longitude': longitude, + 'zoom': zoom + } + save_json(maps_json, maps_filename) + + +def get_map_preferences_coords(base_dir: str, + nickname: str, + domain: str) -> (float, float, int): + """Gets the preferred maps website coordinates for an account + """ + maps_filename = \ + acct_dir(base_dir, nickname, domain) + '/map_preferences.json' + if os.path.isfile(maps_filename): + maps_json = load_json(maps_filename) + if maps_json.get('latitude') and \ + maps_json.get('longitude') and \ + maps_json.get('zoom'): + return maps_json['latitude'], \ + maps_json['longitude'], \ + maps_json['zoom'] + return None, None, None diff --git a/webapp_create_post.py b/webapp_create_post.py index b57866348..b2fc01f5d 100644 --- a/webapp_create_post.py +++ b/webapp_create_post.py @@ -29,6 +29,8 @@ from webapp_utils import edit_text_field from webapp_utils import edit_number_field from webapp_utils import edit_currency_field from webapp_post import individual_post_as_html +from maps import get_map_preferences_url +from maps import get_map_preferences_coords def _html_following_data_list(base_dir: str, nickname: str, @@ -681,11 +683,44 @@ def html_new_post(css_cache: {}, media_instance: bool, translate: {}, date_and_location += date_and_time_str - maps_url = get_config_param(base_dir, 'mapsUrl') + maps_url = get_map_preferences_url(base_dir, nickname, domain) if not maps_url: maps_url = 'https://www.openstreetmap.org' if '://' not in maps_url: maps_url = 'https://' + maps_url + maps_latitude, maps_longitude, maps_zoom = \ + get_map_preferences_coords(base_dir, nickname, domain) + if maps_latitude and maps_longitude and maps_zoom: + if 'openstreetmap.org' in maps_url: + maps_url = \ + 'https://www.openstreetmap.org/#map=' + \ + str(maps_zoom) + '/' + \ + str(maps_latitude) + '/' + \ + str(maps_longitude) + elif '.google.co' in maps_url: + maps_url = \ + 'https://www.google.com/maps/@' + \ + str(maps_latitude) + ',' + \ + str(maps_longitude) + ',' + \ + str(maps_zoom) + 'z' + elif '.bing.co' in maps_url: + maps_url = \ + 'https://www.bing.com/maps?cp=' + \ + str(maps_latitude) + '~' + \ + str(maps_longitude) + '&lvl=' + \ + str(maps_zoom) + elif '.waze.co' in maps_url: + maps_url = \ + 'https://ul.waze.com/ul?ll=' + \ + str(maps_latitude) + '%2C' + \ + str(maps_longitude) + '&zoom=' + \ + str(maps_zoom) + elif 'wego.here.co' in maps_url: + maps_url = \ + 'https://wego.here.com/?x=ep&map=' + \ + str(maps_latitude) + ',' + \ + str(maps_longitude) + ',' + \ + str(maps_zoom) + ',normal' location_label_with_link = \ '' + \ diff --git a/webapp_post.py b/webapp_post.py index 92678289b..12fd9451f 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -94,6 +94,9 @@ from blocking import is_blocked from blocking import add_cw_from_lists from reaction import html_emoji_reactions from maps import html_open_street_map +from maps import set_map_preferences_coords +from maps import set_map_preferences_url +from maps import geocoords_from_map_link def _get_location_from_tags(tags: []) -> str: @@ -2165,6 +2168,32 @@ def individual_post_as_html(signing_priv_key_pem: str, translate) if map_str: map_str = '
\n' + map_str + '
\n' + if map_str and post_json_object['object'].get('attributedTo'): + attrib = post_json_object['object']['attributedTo'] + # is this being sent by the author? + if '://' + domain_full + '/users/' + nickname in attrib: + location_domain = location_str + if '://' in location_str: + location_domain = location_str.split('://')[1] + if '/' in location_domain: + location_domain = location_domain.split('/')[0] + location_domain = \ + location_str.split('://')[0] + '://' + location_domain + else: + if '/' in location_domain: + location_domain = location_domain.split('/')[0] + location_domain = 'https://' + location_domain + # remember the map site used + set_map_preferences_url(base_dir, nickname, domain, + location_domain) + # remember the coordinates + osm_domain = 'openstreetmap.org' + map_zoom, map_latitude, map_longitude = \ + geocoords_from_map_link(location_str, osm_domain) + if map_zoom and map_latitude and map_longitude: + set_map_preferences_coords(base_dir, nickname, domain, + map_latitude, map_longitude, + map_zoom) if is_muted: content_str = ''