epicyon/donate.py

175 lines
4.8 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-08-12 20:56:00 +00:00
def _getDonationTypes() -> []:
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
2021-08-12 20:56:00 +00:00
def _getWebsiteStrings() -> []:
return ['www', 'website', 'web', 'homepage']
2021-08-12 20:40:23 +00:00
def getDonationUrl(actorJson: {}) -> str:
"""Returns a link used for donations
"""
if not actorJson.get('attachment'):
return ''
donationType = _getDonationTypes()
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if propertyValue['name'].lower() not in donationType:
continue
if not propertyValue.get('type'):
continue
if not propertyValue.get('value'):
continue
2020-04-03 08:54:53 +00:00
if propertyValue['type'] != 'PropertyValue':
continue
if '<a href="' not in propertyValue['value']:
continue
2020-04-03 08:54:53 +00:00
donateUrl = propertyValue['value'].split('<a href="')[1]
if '"' in donateUrl:
return donateUrl.split('"')[0]
return ''
2020-04-03 08:54:53 +00:00
2021-08-12 20:56:00 +00:00
def getWebsite(actorJson: {}, translate: {}) -> str:
2021-08-12 20:40:23 +00:00
"""Returns a web address link
"""
if not actorJson.get('attachment'):
return ''
matchStrings = _getWebsiteStrings()
2021-08-12 21:16:43 +00:00
matchStrings.append(translate['Website'].lower())
2021-08-12 20:40:23 +00:00
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if propertyValue['name'].lower() not in matchStrings:
continue
if not propertyValue.get('type'):
continue
if not propertyValue.get('value'):
continue
if propertyValue['type'] != 'PropertyValue':
continue
return propertyValue['value']
return ''
2020-04-03 08:54:53 +00:00
def setDonationUrl(actorJson: {}, donateUrl: str) -> None:
"""Sets a link used for donations
"""
2020-07-06 10:31:00 +00:00
notUrl = False
if '.' not in donateUrl:
notUrl = True
if '://' not in donateUrl:
notUrl = True
if ' ' in donateUrl:
notUrl = True
if '<' in donateUrl:
notUrl = True
2020-07-06 10:31:00 +00:00
if not actorJson.get('attachment'):
2020-04-03 08:54:53 +00:00
actorJson['attachment'] = []
donationType = _getDonationTypes()
2020-04-03 08:54:53 +00:00
donateName = None
for paymentService in donationType:
if paymentService in donateUrl:
2020-04-03 08:54:53 +00:00
donateName = paymentService
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
2019-12-17 23:35:59 +00:00
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
2020-04-03 08:54:53 +00:00
if not propertyValue['name'].lower() != donateName:
2019-12-17 23:35:59 +00:00
continue
2020-04-03 08:54:53 +00:00
propertyFound = propertyValue
2019-12-17 23:35:59 +00:00
break
if propertyFound:
actorJson['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 = \
'<a href="' + donateUrl + \
'" rel="me nofollow noopener noreferrer" target="_blank">' + \
donateUrl + '</a>'
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
2020-04-03 08:54:53 +00:00
if propertyValue['name'].lower() != donateName:
continue
2020-04-03 08:54:53 +00:00
if propertyValue['type'] != 'PropertyValue':
continue
2020-04-03 08:54:53 +00:00
propertyValue['value'] = donateValue
return
2020-04-03 08:54:53 +00:00
newDonate = {
"name": donateName,
"type": "PropertyValue",
"value": donateValue
}
actorJson['attachment'].append(newDonate)
2021-08-12 20:40:23 +00:00
2021-08-12 20:56:00 +00:00
def setWebsite(actorJson: {}, websiteUrl: str, translate: {}) -> None:
2021-08-12 20:40:23 +00:00
"""Sets a web address
"""
2021-08-12 21:16:43 +00:00
websiteUrl = websiteUrl.strip()
2021-08-12 20:40:23 +00:00
notUrl = False
if '.' not in websiteUrl:
notUrl = True
if '://' not in websiteUrl:
notUrl = True
if ' ' in websiteUrl:
notUrl = True
if '<' in websiteUrl:
notUrl = True
if not actorJson.get('attachment'):
actorJson['attachment'] = []
matchStrings = _getWebsiteStrings()
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
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
2021-08-12 21:16:43 +00:00
if propertyValue['name'].lower() not in matchStrings:
2021-08-12 20:40:23 +00:00
continue
propertyFound = propertyValue
break
if propertyFound:
actorJson['attachment'].remove(propertyFound)
if notUrl:
return
newEntry = {
"name": 'Website',
"type": "PropertyValue",
"value": websiteUrl
}
actorJson['attachment'].append(newEntry)