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"
|
2019-11-07 12:14:17 +00:00
|
|
|
|
|
|
|
|
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',
|
2019-11-07 12:14:17 +00:00
|
|
|
'subscribestar')
|
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_websiteStrings() -> []:
|
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:
|
2019-11-07 12:14:17 +00:00
|
|
|
"""Returns a link used for donations
|
|
|
|
"""
|
2021-12-26 10:29:52 +00:00
|
|
|
if not actor_json.get('attachment'):
|
2019-11-07 12:14:17 +00:00
|
|
|
return ''
|
2021-12-29 21:55:09 +00:00
|
|
|
donationType = _get_donation_types()
|
2021-12-26 10:32:45 +00:00
|
|
|
for property_value in actor_json['attachment']:
|
|
|
|
if not property_value.get('name'):
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
if property_value['name'].lower() not in donationType:
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
if not property_value.get('type'):
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
if not property_value.get('value'):
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
if property_value['type'] != 'PropertyValue':
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
if '<a href="' not in property_value['value']:
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-31 18:11:17 +00:00
|
|
|
donate_url = property_value['value'].split('<a href="')[1]
|
|
|
|
if '"' in donate_url:
|
|
|
|
return donate_url.split('"')[0]
|
2019-11-07 12:14:17 +00:00
|
|
|
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 ''
|
2021-12-28 18:13:52 +00:00
|
|
|
matchStrings = _get_websiteStrings()
|
2021-08-12 21:16:43 +00:00
|
|
|
matchStrings.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
|
2021-12-26 10:32:45 +00:00
|
|
|
if property_value['name'].lower() not in matchStrings:
|
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 ''
|
|
|
|
|
|
|
|
|
2021-12-31 18:11:17 +00:00
|
|
|
def set_donation_url(actor_json: {}, donate_url: str) -> None:
|
2019-11-07 12:14:17 +00:00
|
|
|
"""Sets a link used for donations
|
|
|
|
"""
|
2020-07-06 10:31:00 +00:00
|
|
|
notUrl = False
|
2021-12-31 18:11:17 +00:00
|
|
|
if '.' not in donate_url:
|
2020-07-06 10:31:00 +00:00
|
|
|
notUrl = True
|
2021-12-31 18:11:17 +00:00
|
|
|
if '://' not in donate_url:
|
2020-07-06 10:31:00 +00:00
|
|
|
notUrl = True
|
2021-12-31 18:11:17 +00:00
|
|
|
if ' ' in donate_url:
|
2020-07-06 10:31:00 +00:00
|
|
|
notUrl = True
|
2021-12-31 18:11:17 +00:00
|
|
|
if '<' in donate_url:
|
2020-12-12 15:31:28 +00:00
|
|
|
notUrl = 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'] = []
|
2019-11-07 12:14:17 +00:00
|
|
|
|
2021-12-29 21:55:09 +00:00
|
|
|
donationType = _get_donation_types()
|
2020-04-03 08:54:53 +00:00
|
|
|
donateName = None
|
2019-11-07 12:14:17 +00:00
|
|
|
for paymentService in donationType:
|
2021-12-31 18:11:17 +00:00
|
|
|
if paymentService in donate_url:
|
2020-04-03 08:54:53 +00:00
|
|
|
donateName = paymentService
|
2019-11-07 12:14:17 +00:00
|
|
|
if not donateName:
|
|
|
|
return
|
|
|
|
|
2019-12-17 23:35:59 +00:00
|
|
|
# remove any existing value
|
2020-04-03 08:54:53 +00:00
|
|
|
propertyFound = 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
|
2021-12-26 10:32:45 +00:00
|
|
|
if not property_value['name'].lower() != donateName:
|
2019-12-17 23:35:59 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
propertyFound = property_value
|
2019-12-17 23:35:59 +00:00
|
|
|
break
|
|
|
|
if propertyFound:
|
2021-12-26 10:29:52 +00:00
|
|
|
actor_json['attachment'].remove(propertyFound)
|
2020-07-06 10:31:00 +00:00
|
|
|
if notUrl:
|
|
|
|
return
|
2019-12-17 23:35:59 +00:00
|
|
|
|
2020-04-03 08:54:53 +00:00
|
|
|
donateValue = \
|
2021-12-31 18:11:17 +00:00
|
|
|
'<a href="' + donate_url + \
|
2020-04-03 08:54:53 +00:00
|
|
|
'" rel="me nofollow noopener noreferrer" target="_blank">' + \
|
2021-12-31 18:11:17 +00:00
|
|
|
donate_url + '</a>'
|
2019-11-07 12:14:17 +00:00
|
|
|
|
2021-12-26 10:32:45 +00:00
|
|
|
for property_value in actor_json['attachment']:
|
|
|
|
if not property_value.get('name'):
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
if not property_value.get('type'):
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
if property_value['name'].lower() != donateName:
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
if property_value['type'] != 'PropertyValue':
|
2019-11-07 12:14:17 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
property_value['value'] = donateValue
|
2019-11-07 12:14:17 +00:00
|
|
|
return
|
|
|
|
|
2020-04-03 08:54:53 +00:00
|
|
|
newDonate = {
|
2019-11-07 12:14:17 +00:00
|
|
|
"name": donateName,
|
|
|
|
"type": "PropertyValue",
|
|
|
|
"value": donateValue
|
|
|
|
}
|
2021-12-26 10:29:52 +00:00
|
|
|
actor_json['attachment'].append(newDonate)
|
2021-08-12 20:40:23 +00:00
|
|
|
|
|
|
|
|
2021-12-31 18:11:17 +00:00
|
|
|
def set_website(actor_json: {}, website_url: str, translate: {}) -> None:
|
2021-08-12 20:40:23 +00:00
|
|
|
"""Sets a web address
|
|
|
|
"""
|
2021-12-31 18:11:17 +00:00
|
|
|
website_url = website_url.strip()
|
2021-08-12 20:40:23 +00:00
|
|
|
notUrl = False
|
2021-12-31 18:11:17 +00:00
|
|
|
if '.' not in website_url:
|
2021-08-12 20:40:23 +00:00
|
|
|
notUrl = True
|
2021-12-31 18:11:17 +00:00
|
|
|
if '://' not in website_url:
|
2021-08-12 20:40:23 +00:00
|
|
|
notUrl = True
|
2021-12-31 18:11:17 +00:00
|
|
|
if ' ' in website_url:
|
2021-08-12 20:40:23 +00:00
|
|
|
notUrl = True
|
2021-12-31 18:11:17 +00:00
|
|
|
if '<' in website_url:
|
2021-08-12 20:40:23 +00:00
|
|
|
notUrl = True
|
|
|
|
|
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
|
|
|
|
2021-12-28 18:13:52 +00:00
|
|
|
matchStrings = _get_websiteStrings()
|
2021-08-12 21:16:43 +00:00
|
|
|
matchStrings.append(translate['Website'].lower())
|
2021-08-12 20:40:23 +00:00
|
|
|
|
|
|
|
# remove any existing value
|
|
|
|
propertyFound = 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
|
2021-12-26 10:32:45 +00:00
|
|
|
if property_value['name'].lower() not in matchStrings:
|
2021-08-12 20:40:23 +00:00
|
|
|
continue
|
2021-12-26 10:32:45 +00:00
|
|
|
propertyFound = property_value
|
2021-08-12 20:40:23 +00:00
|
|
|
break
|
|
|
|
if propertyFound:
|
2021-12-26 10:29:52 +00:00
|
|
|
actor_json['attachment'].remove(propertyFound)
|
2021-08-12 20:40:23 +00:00
|
|
|
if notUrl:
|
|
|
|
return
|
|
|
|
|
|
|
|
newEntry = {
|
|
|
|
"name": 'Website',
|
|
|
|
"type": "PropertyValue",
|
2021-12-31 18:11:17 +00:00
|
|
|
"value": website_url
|
2021-08-12 20:40:23 +00:00
|
|
|
}
|
2021-12-26 10:29:52 +00:00
|
|
|
actor_json['attachment'].append(newEntry)
|