epicyon/donate.py

175 lines
5.0 KiB
Python
Raw Normal View History

2020-04-03 08:54:53 +00:00
__filename__ = "donate.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-03 08:54:53 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-03 08:54:53 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Profile Metadata"
2021-12-29 21:55:09 +00:00
def _get_donation_types() -> []:
2020-04-03 08:54:53 +00:00
return ('patreon', 'paypal', 'gofundme', 'liberapay',
'kickstarter', 'indiegogo', 'crowdsupply',
'subscribestar')
2020-03-22 21:16:02 +00:00
2020-04-03 08:54:53 +00:00
2022-01-02 12:38:22 +00:00
def _get_website_strings() -> []:
2021-08-12 20:56:00 +00:00
return ['www', 'website', 'web', 'homepage']
2021-08-12 20:40:23 +00:00
2021-12-28 18:13:52 +00:00
def get_donation_url(actor_json: {}) -> str:
"""Returns a link used for donations
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
return ''
2022-01-02 12:38:22 +00:00
donation_type = _get_donation_types()
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
continue
2022-01-02 12:38:22 +00:00
if property_value['name'].lower() not in donation_type:
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('value'):
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
continue
2021-12-26 10:32:45 +00:00
if '<a href="' not in property_value['value']:
continue
donate_url = property_value['value'].split('<a href="')[1]
if '"' in donate_url:
return donate_url.split('"')[0]
return ''
2020-04-03 08:54:53 +00:00
2021-12-28 18:13:52 +00:00
def get_website(actor_json: {}, translate: {}) -> str:
2021-08-12 20:40:23 +00:00
"""Returns a web address link
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2021-08-12 20:40:23 +00:00
return ''
2022-01-02 12:38:22 +00:00
match_strings = _get_website_strings()
match_strings.append(translate['Website'].lower())
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2021-08-12 20:40:23 +00:00
continue
2022-01-02 12:38:22 +00:00
if property_value['name'].lower() not in match_strings:
2021-08-12 20:40:23 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2021-08-12 20:40:23 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('value'):
2021-08-12 20:40:23 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2021-08-12 20:40:23 +00:00
continue
2021-12-26 10:32:45 +00:00
return property_value['value']
2021-08-12 20:40:23 +00:00
return ''
def set_donation_url(actor_json: {}, donate_url: str) -> None:
"""Sets a link used for donations
"""
2022-01-02 12:38:22 +00:00
not_url = False
if '.' not in donate_url:
2022-01-02 12:38:22 +00:00
not_url = True
if '://' not in donate_url:
2022-01-02 12:38:22 +00:00
not_url = True
if ' ' in donate_url:
2022-01-02 12:38:22 +00:00
not_url = True
if '<' in donate_url:
2022-01-02 12:38:22 +00:00
not_url = True
2020-07-06 10:31:00 +00:00
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['attachment'] = []
2022-01-02 12:38:22 +00:00
donation_type = _get_donation_types()
donate_name = None
for payment_service in donation_type:
if payment_service in donate_url:
donate_name = payment_service
if not donate_name:
return
2019-12-17 23:35:59 +00:00
# remove any existing value
2022-01-02 12:38:22 +00:00
property_found = None
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2019-12-17 23:35:59 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2019-12-17 23:35:59 +00:00
continue
2022-01-02 12:38:22 +00:00
if not property_value['name'].lower() != donate_name:
2019-12-17 23:35:59 +00:00
continue
2022-01-02 12:38:22 +00:00
property_found = property_value
2019-12-17 23:35:59 +00:00
break
2022-01-02 12:38:22 +00:00
if property_found:
actor_json['attachment'].remove(property_found)
if not_url:
2020-07-06 10:31:00 +00:00
return
2019-12-17 23:35:59 +00:00
2022-01-02 12:38:22 +00:00
donate_value = \
'<a href="' + donate_url + \
2020-04-03 08:54:53 +00:00
'" rel="me nofollow noopener noreferrer" target="_blank">' + \
donate_url + '</a>'
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
continue
2022-01-02 12:38:22 +00:00
if property_value['name'].lower() != donate_name:
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
continue
2022-01-02 12:38:22 +00:00
property_value['value'] = donate_value
return
2022-01-02 12:38:22 +00:00
new_donate = {
"name": donate_name,
"type": "PropertyValue",
2022-01-02 12:38:22 +00:00
"value": donate_value
}
2022-01-02 12:38:22 +00:00
actor_json['attachment'].append(new_donate)
2021-08-12 20:40:23 +00:00
def set_website(actor_json: {}, website_url: str, translate: {}) -> None:
2021-08-12 20:40:23 +00:00
"""Sets a web address
"""
website_url = website_url.strip()
2022-01-02 12:38:22 +00:00
not_url = False
if '.' not in website_url:
2022-01-02 12:38:22 +00:00
not_url = True
if '://' not in website_url:
2022-01-02 12:38:22 +00:00
not_url = True
if ' ' in website_url:
2022-01-02 12:38:22 +00:00
not_url = True
if '<' in website_url:
2022-01-02 12:38:22 +00:00
not_url = True
2021-08-12 20:40:23 +00:00
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['attachment'] = []
2021-08-12 20:40:23 +00:00
2022-01-02 12:38:22 +00:00
match_strings = _get_website_strings()
match_strings.append(translate['Website'].lower())
2021-08-12 20:40:23 +00:00
# remove any existing value
2022-01-02 12:38:22 +00:00
property_found = None
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2021-08-12 20:40:23 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2021-08-12 20:40:23 +00:00
continue
2022-01-02 12:38:22 +00:00
if property_value['name'].lower() not in match_strings:
2021-08-12 20:40:23 +00:00
continue
2022-01-02 12:38:22 +00:00
property_found = property_value
2021-08-12 20:40:23 +00:00
break
2022-01-02 12:38:22 +00:00
if property_found:
actor_json['attachment'].remove(property_found)
if not_url:
2021-08-12 20:40:23 +00:00
return
2022-01-02 12:38:22 +00:00
new_entry = {
2021-08-12 20:40:23 +00:00
"name": 'Website',
"type": "PropertyValue",
"value": website_url
2021-08-12 20:40:23 +00:00
}
2022-01-02 12:38:22 +00:00
actor_json['attachment'].append(new_entry)