From 188f7e1b692b8274e81b9597af46911c9f791c70 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 20 May 2024 11:10:37 +0100 Subject: [PATCH] Support geo uri scheme for locations --- maps.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests.py | 11 +++++++++++ 2 files changed, 66 insertions(+) diff --git a/maps.py b/maps.py index dd2674586..038d40341 100644 --- a/maps.py +++ b/maps.py @@ -194,6 +194,32 @@ def _geocoords_from_osmand_link(url: str) -> (int, float, float): return zoom, latitude, longitude +def _geocoords_from_geo_link(url: str) -> (int, float, float): + """Returns geocoordinates from an geo link + https://en.wikipedia.org/wiki/Geo_URI_scheme + """ + latitude = None + longitude = None + zoom = 10 + + coords_str = url.split('geo:')[1] + if ',' in coords_str: + coords_sections = coords_str.split(',') + if len(coords_sections) == 2: + latitude_str = coords_sections[0] + longitude_str = coords_sections[1] + if ';' in longitude_str: + longitude_str = longitude_str.split(';')[0] + if '?' in longitude_str: + longitude_str = longitude_str.split('?')[0] + if ' ' in longitude_str: + longitude_str = longitude_str.split(' ')[0] + if is_float(latitude_str) and is_float(longitude_str): + latitude = float(latitude_str) + longitude = float(longitude_str) + return zoom, latitude, longitude + + def _geocoords_from_gmaps_link(url: str) -> (int, float, float): """Returns geocoordinates from a Gmaps link """ @@ -373,6 +399,8 @@ def geocoords_from_map_link(url: str, return _geocoords_from_waze_link(url) if 'wego.here.co' in url: return _geocoords_from_wego_link(url) + if 'geo:' in url and ',' in url: + return _geocoords_from_geo_link(url) return None, None, None @@ -506,6 +534,33 @@ def get_map_links_from_post_content(content: str) -> []: if url not in map_links: map_links.append(url) ctr += 1 + + # https://en.wikipedia.org/wiki/Geo_URI_scheme + ctr = 0 + sections = content.split('geo:') + for link_str in sections: + if ctr == 0: + ctr += 1 + continue + if ',' not in link_str: + continue + coords_str = '' + for char in link_str: + if not char.isnumeric() and char not in (',', '-', '.'): + break + coords_str += char + if ',' not in coords_str: + continue + coord_sections = coords_str.split(',') + if len(coord_sections) != 2: + continue + if not is_float(coord_sections[0]) or \ + not is_float(coord_sections[1]): + continue + url = 'geo:' + coords_str + map_links.append(url) + ctr += 1 + return map_links diff --git a/tests.py b/tests.py index 487f4056f..598da770b 100644 --- a/tests.py +++ b/tests.py @@ -7947,6 +7947,17 @@ def _test_combine_lines(): def _test_hashtag_maps(): print('hashtag_maps') + + content = \ + "

This is a test, with a geo link " + \ + "geo:52.90820,-3.59817;u=35, and some other stuff," + \ + " with commas

" + map_links = get_map_links_from_post_content(content) + link = "geo:52.90820,-3.59817" + if link not in map_links: + print('map_links: ' + str(map_links)) + assert link in map_links + content = \ "

This is a test, with a couple of links and a " + \ "