epicyon/donate.py

150 lines
4.9 KiB
Python
Raw Normal View History

2020-04-03 08:54:53 +00:00
__filename__ = "donate.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2024-01-21 19:01:20 +00:00
__version__ = "1.5.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"
from utils import get_attachment_property_value
from utils import remove_html
2024-04-24 19:35:04 +00:00
from utils import remove_link_tracking
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',
2024-08-12 21:02:09 +00:00
'subscribestar', 'kofi', 'ko-fi',
'fundly', 'crowdrise',
2023-09-08 11:00:47 +00:00
'justgiving', 'globalgiving', 'givedirectly',
2023-09-08 11:07:16 +00:00
'fundrazr', 'kiva', 'thebiggive', 'donorbox',
'opencollective', 'buymeacoffee', 'flattr',
'bountysource', 'coindrop', 'gitpay', 'tipeee')
2020-03-22 21:16:02 +00:00
2020-04-03 08:54:53 +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 ''
2023-11-29 10:58:48 +00:00
if not isinstance(actor_json['attachment'], list):
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']:
2022-05-11 16:10:38 +00:00
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
2024-08-12 17:41:09 +00:00
name_value_lower = name_value.lower()
if name_value_lower not in donation_type:
2024-08-12 21:03:30 +00:00
if 'support' not in name_value_lower and \
'buy me ' not in name_value_lower and \
'fund' not in name_value_lower:
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
continue
prop_value_name, prop_value = \
get_attachment_property_value(property_value)
if not prop_value:
continue
2022-05-11 16:16:34 +00:00
if not property_value['type'].endswith('PropertyValue'):
continue
2024-08-12 21:08:01 +00:00
if '<a href="' in property_value[prop_value_name]:
donate_url = property_value[prop_value_name].split('<a href="')[1]
if '"' in donate_url:
donate_url = donate_url.split('"')[0]
donate_url = remove_html(donate_url)
return remove_link_tracking(donate_url)
else:
donate_url = remove_html(property_value[prop_value_name])
2024-08-12 21:10:54 +00:00
if ' ' in donate_url:
donate_url = donate_url.split(' ')[0]
if '://' in donate_url:
return remove_link_tracking(donate_url)
return ''
2020-04-03 08:54:53 +00:00
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']:
2022-05-11 16:10:38 +00:00
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:
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-05-11 16:10:38 +00:00
if not name_value.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']:
2022-05-11 16:10:38 +00:00
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
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
continue
2022-05-11 16:10:38 +00:00
if name_value.lower() != donate_name:
continue
2022-05-11 16:16:34 +00:00
if not property_value['type'].endswith('PropertyValue'):
continue
prop_value_name, _ = \
get_attachment_property_value(property_value)
if not prop_value_name:
continue
property_value[prop_value_name] = donate_value
return
2022-01-02 12:38:22 +00:00
new_donate = {
"name": donate_name,
"type": "PropertyValue",
2023-05-12 17:40:31 +00:00
"value": donate_value,
"rel": "payment"
}
2022-01-02 12:38:22 +00:00
actor_json['attachment'].append(new_donate)