From 4c7cbd63415175e65e80d8cc590e344ebabdb295 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 17 Aug 2024 14:51:21 +0100 Subject: [PATCH] Move website functions to their own module --- daemon_post_profile.py | 8 +- daemon_utils.py | 4 +- donate.py | 176 -------------------------------------- pgp.py | 2 +- webapp_profile.py | 4 +- website.py | 189 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 198 insertions(+), 185 deletions(-) create mode 100644 website.py diff --git a/daemon_post_profile.py b/daemon_post_profile.py index 03b2fbfa1..d861da4cf 100644 --- a/daemon_post_profile.py +++ b/daemon_post_profile.py @@ -120,12 +120,12 @@ from cwtch import get_cwtch_address from cwtch import set_cwtch_address from enigma import get_enigma_pub_key from enigma import set_enigma_pub_key +from website import get_website +from website import set_website +from website import get_gemini_link +from website import set_gemini_link from donate import get_donation_url from donate import set_donation_url -from donate import get_website -from donate import set_website -from donate import get_gemini_link -from donate import set_gemini_link from person import get_featured_hashtags from person import set_featured_hashtags from blocking import save_block_federated_endpoints diff --git a/daemon_utils.py b/daemon_utils.py index 3c16c5375..32ea92481 100644 --- a/daemon_utils.py +++ b/daemon_utils.py @@ -41,9 +41,9 @@ from utils import resembles_url from utils import is_system_account from cache import check_for_changed_actor from cache import get_person_from_cache +from website import get_website +from website import get_gemini_link from donate import get_donation_url -from donate import get_website -from donate import get_gemini_link from pronouns import get_pronouns from discord import get_discord from art import get_art_site_url diff --git a/donate.py b/donate.py index 5221bbded..d925dd75d 100644 --- a/donate.py +++ b/donate.py @@ -24,14 +24,6 @@ def _get_donation_types() -> []: 'bountysource', 'coindrop', 'gitpay', 'tipeee') -def _get_website_strings() -> []: - return ['www', 'website', 'web', 'homepage', 'home', 'contact'] - - -def _get_gemini_strings() -> []: - return ['gemini', 'capsule', 'gemlog'] - - def get_donation_url(actor_json: {}) -> str: """Returns a link used for donations """ @@ -77,87 +69,6 @@ def get_donation_url(actor_json: {}) -> str: return '' -def get_website(actor_json: {}, translate: {}) -> str: - """Returns a web address link - """ - if not actor_json.get('attachment'): - return '' - match_strings = _get_website_strings() - match_strings.append(translate['Website'].lower()) - for property_value in actor_json['attachment']: - name_value = None - if property_value.get('name'): - name_value = property_value['name'] - elif property_value.get('schema:name'): - name_value = property_value['schema:name'] - if not name_value: - continue - found = False - for possible_str in match_strings: - if possible_str in name_value.lower(): - found = True - break - if not found: - continue - if not property_value.get('type'): - continue - prop_value_name, _ = \ - get_attachment_property_value(property_value) - if not prop_value_name: - continue - if not property_value['type'].endswith('PropertyValue'): - continue - value_str = remove_html(property_value[prop_value_name]) - if 'https://' not in value_str and \ - 'http://' not in value_str: - continue - return remove_link_tracking(value_str) - return '' - - -def get_gemini_link(actor_json: {}) -> str: - """Returns a gemini link - """ - if not actor_json.get('attachment'): - return '' - match_strings = _get_gemini_strings() - for property_value in actor_json['attachment']: - name_value = None - if property_value.get('name'): - name_value = property_value['name'] - elif property_value.get('schema:name'): - name_value = property_value['schema:name'] - if not name_value: - continue - if name_value.lower() not in match_strings: - continue - if not property_value.get('type'): - continue - prop_value_name, _ = \ - get_attachment_property_value(property_value) - if not prop_value_name: - continue - if not property_value['type'].endswith('PropertyValue'): - continue - url = remove_html(property_value[prop_value_name]) - return remove_link_tracking(url) - - for property_value in actor_json['attachment']: - if not property_value.get('type'): - continue - prop_value_name, _ = \ - get_attachment_property_value(property_value) - if not prop_value_name: - continue - if not property_value['type'].endswith('PropertyValue'): - continue - url = remove_html(property_value[prop_value_name]) - if 'gemini://' not in url: - continue - return remove_link_tracking(url) - return '' - - def set_donation_url(actor_json: {}, donate_url: str) -> None: """Sets a link used for donations """ @@ -236,90 +147,3 @@ def set_donation_url(actor_json: {}, donate_url: str) -> None: "rel": "payment" } actor_json['attachment'].append(new_donate) - - -def set_website(actor_json: {}, website_url: str, translate: {}) -> None: - """Sets a web address - """ - website_url = website_url.strip() - not_url = False - if '.' not in website_url: - not_url = True - if '://' not in website_url: - not_url = True - if ' ' in website_url: - not_url = True - if '<' in website_url: - not_url = True - - if not actor_json.get('attachment'): - actor_json['attachment'] = [] - - match_strings = _get_website_strings() - match_strings.append(translate['Website'].lower()) - - # remove any existing value - property_found = None - for property_value in actor_json['attachment']: - if not property_value.get('name'): - continue - if not property_value.get('type'): - continue - if property_value['name'].lower() not in match_strings: - continue - property_found = property_value - break - if property_found: - actor_json['attachment'].remove(property_found) - if not_url: - return - - new_entry = { - "name": 'Website', - "type": "PropertyValue", - "value": website_url - } - actor_json['attachment'].append(new_entry) - - -def set_gemini_link(actor_json: {}, gemini_link: str) -> None: - """Sets a gemini link - """ - gemini_link = gemini_link.strip() - not_link = False - if '.' not in gemini_link: - not_link = True - if '://' not in gemini_link: - not_link = True - if ' ' in gemini_link: - not_link = True - if '<' in gemini_link: - not_link = True - - if not actor_json.get('attachment'): - actor_json['attachment'] = [] - - match_strings = _get_gemini_strings() - - # remove any existing value - property_found = None - for property_value in actor_json['attachment']: - if not property_value.get('name'): - continue - if not property_value.get('type'): - continue - if property_value['name'].lower() not in match_strings: - continue - property_found = property_value - break - if property_found: - actor_json['attachment'].remove(property_found) - if not_link: - return - - new_entry = { - "name": 'Gemini', - "type": "PropertyValue", - "value": gemini_link - } - actor_json['attachment'].append(new_entry) diff --git a/pgp.py b/pgp.py index 3d18922c3..78fed330a 100644 --- a/pgp.py +++ b/pgp.py @@ -38,7 +38,7 @@ from matrix import get_matrix_address from briar import get_briar_address from cwtch import get_cwtch_address from blog import get_blog_address -from donate import get_website +from website import get_website from utils import get_attachment_property_value diff --git a/webapp_profile.py b/webapp_profile.py index 9e1ceb858..8bf61f210 100644 --- a/webapp_profile.py +++ b/webapp_profile.py @@ -63,9 +63,9 @@ from posts import is_moderator from posts import parse_user_feed from posts import is_create_inside_announce from posts import get_max_profile_posts +from website import get_website +from website import get_gemini_link from donate import get_donation_url -from donate import get_website -from donate import get_gemini_link from pronouns import get_pronouns from pixelfed import get_pixelfed from discord import get_discord diff --git a/website.py b/website.py new file mode 100644 index 000000000..64b52a730 --- /dev/null +++ b/website.py @@ -0,0 +1,189 @@ +__filename__ = "website.py" +__author__ = "Bob Mottram" +__license__ = "AGPL3+" +__version__ = "1.5.0" +__maintainer__ = "Bob Mottram" +__email__ = "bob@libreserver.org" +__status__ = "Production" +__module_group__ = "Profile Metadata" + + +from utils import get_attachment_property_value +from utils import remove_html +from utils import remove_link_tracking + + +def _get_website_strings() -> []: + return ['www', 'website', 'web', 'homepage', 'home', 'contact'] + + +def _get_gemini_strings() -> []: + return ['gemini', 'capsule', 'gemlog'] + + +def get_website(actor_json: {}, translate: {}) -> str: + """Returns a web address link + """ + if not actor_json.get('attachment'): + return '' + match_strings = _get_website_strings() + match_strings.append(translate['Website'].lower()) + for property_value in actor_json['attachment']: + name_value = None + if property_value.get('name'): + name_value = property_value['name'] + elif property_value.get('schema:name'): + name_value = property_value['schema:name'] + if not name_value: + continue + found = False + for possible_str in match_strings: + if possible_str in name_value.lower(): + found = True + break + if not found: + continue + if not property_value.get('type'): + continue + prop_value_name, _ = \ + get_attachment_property_value(property_value) + if not prop_value_name: + continue + if not property_value['type'].endswith('PropertyValue'): + continue + value_str = remove_html(property_value[prop_value_name]) + if 'https://' not in value_str and \ + 'http://' not in value_str: + continue + return remove_link_tracking(value_str) + return '' + + +def get_gemini_link(actor_json: {}) -> str: + """Returns a gemini link + """ + if not actor_json.get('attachment'): + return '' + match_strings = _get_gemini_strings() + for property_value in actor_json['attachment']: + name_value = None + if property_value.get('name'): + name_value = property_value['name'] + elif property_value.get('schema:name'): + name_value = property_value['schema:name'] + if not name_value: + continue + if name_value.lower() not in match_strings: + continue + if not property_value.get('type'): + continue + prop_value_name, _ = \ + get_attachment_property_value(property_value) + if not prop_value_name: + continue + if not property_value['type'].endswith('PropertyValue'): + continue + url = remove_html(property_value[prop_value_name]) + return remove_link_tracking(url) + + for property_value in actor_json['attachment']: + if not property_value.get('type'): + continue + prop_value_name, _ = \ + get_attachment_property_value(property_value) + if not prop_value_name: + continue + if not property_value['type'].endswith('PropertyValue'): + continue + url = remove_html(property_value[prop_value_name]) + if 'gemini://' not in url: + continue + return remove_link_tracking(url) + return '' + + +def set_website(actor_json: {}, website_url: str, translate: {}) -> None: + """Sets a web address + """ + website_url = website_url.strip() + not_url = False + if '.' not in website_url: + not_url = True + if '://' not in website_url: + not_url = True + if ' ' in website_url: + not_url = True + if '<' in website_url: + not_url = True + + if not actor_json.get('attachment'): + actor_json['attachment'] = [] + + match_strings = _get_website_strings() + match_strings.append(translate['Website'].lower()) + + # remove any existing value + property_found = None + for property_value in actor_json['attachment']: + if not property_value.get('name'): + continue + if not property_value.get('type'): + continue + if property_value['name'].lower() not in match_strings: + continue + property_found = property_value + break + if property_found: + actor_json['attachment'].remove(property_found) + if not_url: + return + + new_entry = { + "name": 'Website', + "type": "PropertyValue", + "value": website_url + } + actor_json['attachment'].append(new_entry) + + +def set_gemini_link(actor_json: {}, gemini_link: str) -> None: + """Sets a gemini link + """ + gemini_link = gemini_link.strip() + not_link = False + if '.' not in gemini_link: + not_link = True + if '://' not in gemini_link: + not_link = True + if ' ' in gemini_link: + not_link = True + if '<' in gemini_link: + not_link = True + + if not actor_json.get('attachment'): + actor_json['attachment'] = [] + + match_strings = _get_gemini_strings() + + # remove any existing value + property_found = None + for property_value in actor_json['attachment']: + if not property_value.get('name'): + continue + if not property_value.get('type'): + continue + if property_value['name'].lower() not in match_strings: + continue + property_found = property_value + break + if property_found: + actor_json['attachment'].remove(property_found) + if not_link: + return + + new_entry = { + "name": 'Gemini', + "type": "PropertyValue", + "value": gemini_link + } + actor_json['attachment'].append(new_entry)