From 36544e214e48b162740f0f4f20ecf2bef501aeca Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 24 Apr 2025 13:29:46 +0100 Subject: [PATCH] Handle location url --- happening.py | 25 +++++++++++++++++++++++-- maps.py | 21 ++++++++++++++++++++- posts.py | 10 +++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/happening.py b/happening.py index 9ff3cec78..2f47f612e 100644 --- a/happening.py +++ b/happening.py @@ -14,6 +14,7 @@ from datetime import datetime from datetime import timedelta from flags import is_reminder from flags import is_public_post +from utils import resembles_url from utils import replace_strings from utils import date_from_numbers from utils import date_from_string_format @@ -944,6 +945,7 @@ def _dav_store_event(base_dir: str, nickname: str, domain: str, start_time = None end_time = None description = None + location = None for line in event_list: if line.startswith('DTSTAMP:'): timestamp = line.split(':', 1)[1] @@ -1060,18 +1062,37 @@ def _dav_store_event(base_dir: str, nickname: str, domain: str, } } if location: - event_json['object']['tag'].append({ + # get the location link + location_url = '' + if '://' in location: + location_words = location.split(' ') + for loc_wrd in location_words: + if '://' in loc_wrd: + if resembles_url(loc_wrd): + location_url = loc_wrd + # remove link from the location name + location = location.replace(location_url, '') + location = location.replace(' ', ' ') + break + # add a tag + location_tag = { '@context': [ 'https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1' ], 'type': 'Place', 'name': location - }) + } + event_json['object']['tag'].append(location_tag) + # add a location to the object event_json['object']['location'] = { 'type': 'Place', 'name': location } + if location_url: + # add the url to the location + event_json['object']['location']['url'] = location_url + location_tag['url'] = location_url handle = nickname + '@' + domain handle_dir = acct_handle_dir(base_dir, handle) outbox_dir = handle_dir + '/outbox' diff --git a/maps.py b/maps.py index 9b14057c9..e2dc9c4be 100644 --- a/maps.py +++ b/maps.py @@ -10,6 +10,7 @@ __module_group__ = "Core" import os from flags import is_float +from utils import resembles_url from utils import browser_supports_download_filename from utils import get_url_from_post from utils import acct_dir @@ -54,7 +55,15 @@ def _get_location_from_tags(tags: []) -> str: locn = get_location_dict_from_tags(tags) if locn: location_str = locn['name'].replace('\n', ' ') - return remove_html(location_str) + location_str = remove_html(location_str) + if locn.get('url'): + if isinstance(locn['url'], str): + if resembles_url(locn['url']): + location_str = \ + '' + \ + location_str + '' + return location_str return None @@ -62,6 +71,7 @@ def get_location_from_post(post_json_object: {}) -> str: """Returns the location for the given post """ locn = None + locn_url = None # location represented via a tag post_obj = post_json_object @@ -88,12 +98,21 @@ def get_location_from_post(post_json_object: {}) -> str: if locn2.get('name'): if isinstance(locn2['name'], str): locn = locn2['name'] + if locn2.get('url'): + if isinstance(locn2['url'], str): + locn_url = locn2['url'] if locn_exists: osm_domain = 'osm.org' zoom = 17 locn = _geocoords_to_osm_link(osm_domain, zoom, locn2['latitude'], locn2['longitude']) + elif locn_url: + if locn: + locn = '' + locn + '' + else: + locn = locn_url return locn diff --git a/posts.py b/posts.py index 3bdea596c..589531d3c 100644 --- a/posts.py +++ b/posts.py @@ -41,6 +41,7 @@ from flags import contains_private_key from flags import has_group_type from flags import is_premium_account from flags import url_permitted +from utils import resembles_url from utils import get_person_icon from utils import remove_post_from_index from utils import replace_strings @@ -1397,7 +1398,10 @@ def _create_post_s2s(base_dir: str, nickname: str, domain: str, port: int, 'type': 'Place', 'name': location['name'] } - + if location.get('url'): + if isinstance(location['url'], str): + if resembles_url(location['url']): + new_post['object']['location']['url'] = location['url'] if attach_image_filename: new_post['object'] = \ attach_media(base_dir, http_prefix, nickname, domain, port, @@ -1529,6 +1533,10 @@ def _create_post_c2s(base_dir: str, nickname: str, domain: str, port: int, 'type': 'Place', 'name': location['name'] } + if location.get('url'): + if isinstance(location['url'], str): + if resembles_url(location['url']): + new_post['object']['location']['url'] = location['url'] if attach_image_filename: new_post = \