-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchival_script.py
More file actions
411 lines (308 loc) · 15.7 KB
/
Copy patharchival_script.py
File metadata and controls
411 lines (308 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import os
import json
import requests
import base64
import logging
import subprocess
from datetime import datetime, timezone, timedelta
from dateutil import parser
api_key = os.environ.get("WA_API_KEY")
log_file_path = 'wa_contact_archive.log'
logging.basicConfig(level=logging.INFO, filename= log_file_path, filemode='a', format='%(asctime)s - %(levelname)s - %(message)s')
def get_access_token(api_key):
"""Obtains and returns an access token for the Wild Apricot API."""
auth_url = 'https://oauth.wildapricot.org/auth/token'
encoded_key = base64.b64encode(f'APIKEY:{api_key}'.encode()).decode()
auth_headers = {'Authorization': f'Basic {encoded_key}', 'Content-Type': 'application/x-www-form-urlencoded'}
auth_data = {'grant_type': 'client_credentials', 'scope': 'auto'}
auth_response = requests.post(auth_url, headers=auth_headers, data=auth_data)
return auth_response.json().get('access_token')
def get_account_id(headers):
"""Retrieves the account ID."""
api_base_url = 'https://api.wildapricot.org/v2.2'
response = requests.get(f"{api_base_url}/accounts", headers=headers)
if response.status_code != 200:
logging.error(f"Error: Unable to retrieve account details. Status code: {response.status_code}")
return None
return response.json()[0]['Id']
def get_contact_info(contact_id, access_token):
"""Retrieves the email address and first name of a contact given a contact ID."""
api_base_url = 'https://api.wildapricot.org/v2.1'
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
# Make an API request to retrieve the account details
account_response = requests.get(f'{api_base_url}/accounts', headers=headers)
if account_response.status_code != 200:
logging.error(f'Error: Unable to retrieve account details. Status code: {account_response.status_code}')
return
account_id = account_response.json()[0]['Id']
# Make an API request to retrieve the contact details
contact_response = requests.get(f'{api_base_url}/accounts/{account_id}/contacts/{contact_id}', headers=headers)
if contact_response.status_code != 200:
logging.error(f'Error: Unable to retrieve contact details. Status code: {contact_response.status_code}')
return
contact_details = contact_response.json()
# Get the email address, first name, and membership status from the contact details
email = contact_details.get('Email', 'Unknown')
first_name = contact_details.get('FirstName', 'Unknown')
return email, first_name
def set_contact_to_archived(contact_id, access_token):
api_base_url = 'https://api.wildapricot.org/v2.1'
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
# Make an API request to retrieve the account details
account_response = requests.get(f'{api_base_url}/accounts', headers=headers)
if account_response.status_code != 200:
logging.error(f'Error: Unable to retrieve account details. Status code: {account_response.status_code}')
return
account_id = account_response.json()[0]['Id']
contact_response = requests.get(f'{api_base_url}/accounts/{account_id}/contacts/{contact_id}', headers=headers)
if contact_response.status_code != 200:
logging.error(f'Error: Unable to retrieve contact details. Status code: {contact_response.status_code}')
return
contact_data = contact_response.json()
for field in contact_data['FieldValues']:
if field['SystemCode'] == 'IsArchived':
field['Value'] = True
if field['SystemCode'] == 'Notes':
current_notes = field['Value']
# Add new line to the notes with the current date and "RMM Archival Bot"
new_note = f"\n\r\nMember archived on {datetime.now().strftime('%m/%d/%Y')} by RMM Archival Bot"
field['Value'] = current_notes + new_note
# Send the updated data back to the API
update_response = requests.put(f'{api_base_url}/accounts/{account_id}/contacts/{contact_id}', headers=headers, data=json.dumps(contact_data))
if update_response.status_code != 200:
logging.error(f'Error: Unable to update contact. Status code: {update_response.status_code}')
return
return "Contact archived successfully"
formatted_data = []
# Basic contact information
formatted_data.append(f"First Name: {contact_data.get('FirstName', 'Unknown')}")
formatted_data.append(f"Last Name: {contact_data.get('LastName', 'Unknown')}")
formatted_data.append(f"Email: {contact_data.get('Email', 'Unknown')}")
formatted_data.append(f"Membership Level: {contact_data.get('MembershipLevel', {}).get('Name', 'Unknown')}")
# Detailed field values
formatted_data.append("\nDetailed Fields:")
for field in contact_data.get('FieldValues', []):
field_name = field.get('FieldName', 'Unknown Field')
field_value = field.get('Value', 'Unknown Value')
formatted_data.append(f" {field_name}: {field_value}")
# Return the formatted data
return '\n'.join(formatted_data)
def num_members(access_token):
"""Finds a contact by Discord username."""
api_base_url = 'https://api.wildapricot.org/v2.2'
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json', 'Accept': 'application/json'}
account_id = get_account_id(headers)
if not account_id:
return None
filter_query = f"$filter='Member' eq 'True'"
contacts_url = f"{api_base_url}/accounts/{account_id}/contacts?$async=false&{filter_query}"
contacts_response = requests.get(contacts_url, headers=headers)
if contacts_response.status_code != 200:
logging.error(f"Error: Unable to retrieve contacts. Status code: {contacts_response.status_code}")
return None
else:
return len(contacts_response.json().get("Contacts", []))
def num_contacts(access_token):
"""returns the number of contacts that are not archived"""
api_base_url = 'https://api.wildapricot.org/v2.2'
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json', 'Accept': 'application/json'}
account_id = get_account_id(headers)
if not account_id:
return None
url = f"{api_base_url}/accounts/{account_id}/contacts"
top = 100
skip = 0
total = 0
while True:
params = {
"$async": "false",
"$filter": "isArchived eq false",
"$top": top,
"$skip": skip,
}
r = requests.get(url, headers=headers, params=params)
if r.status_code != 200:
logging.error(f"Unable to retrieve contacts. Status code: {r.status_code}, body: {r.text}")
return None
contacts = r.json().get("Contacts", [])
total += len(contacts)
# done when the API returns fewer than a full page
if len(contacts) < top:
break
skip += top
return total
def contacts_w_registrations(access_token):
"""Finds a contact by Discord username."""
api_base_url = 'https://api.wildapricot.org/v2.2'
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json', 'Accept': 'application/json'}
account_id = get_account_id(headers)
if not account_id:
return None
url = f"{api_base_url}/accounts/{account_id}/contacts"
top = 100
skip = 0
contacts_w_registration = []
while True:
params = {
"$async": "false",
"$filter": "Member eq false and isArchived eq false",
"$top": top,
"$skip": skip,
}
r = requests.get(url, headers=headers, params=params)
if r.status_code != 200:
logging.error(f"Unable to retrieve contacts. Status code: {r.status_code}, body: {r.text}")
return None
contacts = r.json().get("Contacts", [])
for contact in contacts:
contact_id = contact.get("Id")
if not contact_id:
continue
if has_upcoming_event_registrations(contact_id, access_token):
contacts_w_registration.append(contact_id)
if len(contacts) < top:
break
skip += top
return len(contacts_w_registration)
def contacts_w_balance(access_token):
"""Finds a contact by Discord username."""
api_base_url = 'https://api.wildapricot.org/v2.2'
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json', 'Accept': 'application/json'}
account_id = get_account_id(headers)
if not account_id:
return None
filter_query = f"$filter='Balance' ne '0.0'"
contacts_url = f"{api_base_url}/accounts/{account_id}/contacts?$async=false&{filter_query}"
contacts_response = requests.get(contacts_url, headers=headers)
if contacts_response.status_code != 200:
logging.error(f"Error: Unable to retrieve contacts. Status code: {contacts_response.status_code}")
return None
else:
return len(contacts_response.json().get("Contacts", []))
def return_archival_candidates(access_token):
"""Finds a contact by Discord username."""
api_base_url = 'https://api.wildapricot.org/v2.2'
headers = {'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json', 'Accept': 'application/json'}
account_id = get_account_id(headers)
if not account_id:
return None
filter_query = f"$filter='Member' ne 'True' and 'IsArchived' ne 'True'"
contacts_url = f"{api_base_url}/accounts/{account_id}/contacts?$async=false&{filter_query}"
contacts_response = requests.get(contacts_url, headers=headers)
contacts = contacts_response.json().get("Contacts", [])
contacts.sort(key=get_last_login_date, reverse=False)
archival_candidates = []
print(len(contacts))
for contact in contacts:
upcoming_event = has_upcoming_event_registrations(contact['Id'], access_token)
if not upcoming_event:
balance = 0.0
ignore_archive_bot = False
for field in contact['FieldValues']:
if field['FieldName'] == 'Balance':
balance = field['Value']
elif field['FieldName'] == 'Internal Use Admin Info':
# Check if 'Ignore Archive Bot' exists in the list of dictionaries
ignore_archive_bot = any(item.get('Label') == 'Ignore Archive Bot' for item in field['Value'])
if balance == 0.0 and not ignore_archive_bot:
archival_candidates.append(contact['Id'])
if contacts_response.status_code != 200:
logging.error(f"Error: Unable to retrieve contacts. Status code: {contacts_response.status_code}")
return None
else:
print(f"Can Archive {len(archival_candidates)}")
return archival_candidates
def has_upcoming_event_registrations(contact_id, access_token):
api_base_url = 'https://api.wildapricot.org/v2.1'
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
'Accept': 'application/json',
}
# Retrieve the account details
account_response = requests.get(f'{api_base_url}/accounts', headers=headers)
if account_response.status_code != 200:
logging.error(f'Error: Unable to retrieve account details. Status code: {account_response.status_code}')
return False
account_id = account_response.json()[0]['Id']
# Retrieve event registrations for the contact
registrations_url = f"{api_base_url}/accounts/{account_id}/eventregistrations?contactId={contact_id}"
registrations_response = requests.get(registrations_url, headers=headers)
if registrations_response.status_code != 200:
logging.error(f'Error: Unable to retrieve event registrations. Status code: {registrations_response.status_code}')
return False
registrations = registrations_response.json()
# Check if any of the event start dates are in the future
for registration in registrations:
event_start_date = registration.get('Event', {}).get('StartDate')
if event_start_date:
event_start_date = parser.parse(event_start_date)
current_time = datetime.now(event_start_date.tzinfo)
#shifts back by 4 days to allow time for class followup emails to be sent before archival
archival_threshold = archival_threshold = current_time - timedelta(days=4)
if event_start_date > archival_threshold:
return True
return False
def get_last_login_date(contact):
"""Extracts and returns the last login date from the contact's FieldValues."""
for field in contact.get('FieldValues', []):
if field['SystemCode'] == 'LastLoginDate':
last_login_str = field.get('Value')
if last_login_str:
# Parse the datetime string and make it offset-aware
last_login_date = parser.parse(last_login_str)
if last_login_date.tzinfo is None:
last_login_date = last_login_date.replace(tzinfo=timezone.utc)
return last_login_date
return datetime.min.replace(tzinfo=timezone.utc)
def cleanup_log_file():
subprocess.run(['git', 'add', log_file_path])
subprocess.run(['git', 'commit', '-m', 'Processed events from Wild Apricot to discord'])
subprocess.run(['git', 'push'])
access_token = get_access_token(api_key)
logging.info("Starting archival script")
num_contacts = num_contacts(access_token)
print(num_contacts)
contact_target = 300
removal_target = num_contacts - contact_target
if num_contacts > contact_target:
logging.info(f"Currently at {num_contacts} contacts. Target is {contact_target}, attempting to remove {removal_target} contacts.")
else:
logging.info(f"Currently at {num_contacts} contacts. Target is {contact_target}, no action required. Exiting")
cleanup_log_file()
exit()
number_of_members = num_members(access_token)
print(f"number of members is {number_of_members}")
num_non_members_with_a_balance = contacts_w_balance(access_token)
print(f"number of non members with a balance is {num_non_members_with_a_balance}")
num_non_members_future_registration = contacts_w_registrations(access_token)
print(f"number of non members with future registrations is {num_non_members_future_registration}")
minimum_contacts = number_of_members + num_non_members_with_a_balance + num_non_members_future_registration
contact_margin = contact_target - minimum_contacts - 10 #10 is a buffer
if contact_margin < 0:
logging.info(f"Warning: Currently at {minimum_contacts} contacts. Target is {contact_target}, we have more contacts than the target. Need to consider upgrading our plan.")
cleanup_log_file()
exit()
else:
logging.info(f"Currently at {minimum_contacts} minimum contacts. Target is {contact_target}, we have {contact_margin} contacts margin.\nContinuing to remove {removal_target} contacts.")
logging.info(f"Minimum contact makeup:\nNumber of members: {number_of_members}\nNumber of non-members with a balance: {num_non_members_with_a_balance}\nNumber of non-members with future registrations: {num_non_members_future_registration}")
archival_candidates = return_archival_candidates(access_token)
logging.info(f"{len(archival_candidates)} total candidates available for archive")
num = 0
for contact in archival_candidates:
num += 1
if num > removal_target:
logging.info("Exiting after removing target contacts")
cleanup_log_file()
exit()
logging.info(f"Archiving contact {contact}")
set_contact_to_archived(contact, access_token)
cleanup_log_file()