| 
									
										
										
										
											2020-04-03 08:54:53 +00:00
										 |  |  | __filename__ = "donate.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							|  |  |  | __version__ = "1.1.0" | 
					
						
							|  |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							|  |  |  | __email__ = "bob@freedombone.net" | 
					
						
							|  |  |  | __status__ = "Production" | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def getDonationTypes() -> str: | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  | def getDonationUrl(actorJson: {}) -> str: | 
					
						
							|  |  |  |     """Returns a link used for donations
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if not actorJson.get('attachment'): | 
					
						
							|  |  |  |         return '' | 
					
						
							| 
									
										
										
										
											2020-04-03 08:54:53 +00:00
										 |  |  |     donationType = getDonationTypes() | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  |     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': | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  |             continue | 
					
						
							|  |  |  |         if '<a href="' not in propertyValue['value']: | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 08:54:53 +00:00
										 |  |  |         donateUrl = propertyValue['value'].split('<a href="')[1] | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  |         if '"' in donateUrl: | 
					
						
							|  |  |  |             return donateUrl.split('"')[0] | 
					
						
							|  |  |  |     return '' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 08:54:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def setDonationUrl(actorJson: {}, donateUrl: str) -> None: | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  |     """Sets a link used for donations
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     if not actorJson.get('attachment'): | 
					
						
							| 
									
										
										
										
											2020-04-03 08:54:53 +00:00
										 |  |  |         actorJson['attachment'] = [] | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 08:54:53 +00:00
										 |  |  |     donationType = getDonationTypes() | 
					
						
							|  |  |  |     donateName = None | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  |     for paymentService in donationType: | 
					
						
							|  |  |  |         if paymentService in donateUrl: | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											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-04-03 08:54:53 +00:00
										 |  |  |     donateValue = \ | 
					
						
							|  |  |  |         '<a href="' + donateUrl + \ | 
					
						
							|  |  |  |         '" rel="me nofollow noopener noreferrer" target="_blank">' + \ | 
					
						
							|  |  |  |         donateUrl + '</a>' | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +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 propertyValue['name'].lower() != donateName: | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 08:54:53 +00:00
										 |  |  |         if propertyValue['type'] != 'PropertyValue': | 
					
						
							| 
									
										
										
										
											2019-11-07 12:14:17 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 08:54:53 +00:00
										 |  |  |         propertyValue['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 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     actorJson['attachment'].append(newDonate) |