-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_all.py
86 lines (73 loc) · 2.81 KB
/
message_all.py
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
import schoolopy
import yaml
import time
BLOCKED = []
SUBJECT = 'subject'
BODY = 'body'
# Load staff names from file
with open('staff.yml') as file:
# STAFF is a list of lists containing first and last names. Decoding as tuples might require extra effort
# E.x. [['First', 'Last']]
STAFF = yaml.load(file, Loader=yaml.FullLoader)
# Load schoology api keys from file
with open('keys.yml') as file:
keys = yaml.load(file, Loader=yaml.FullLoader)
sc = schoolopy.Schoology(schoolopy.Auth(keys['public'], keys['secret']))
def schoology_req(endpoint, data=None):
if data is not None:
res = sc.schoology_auth.oauth.post(endpoint, headers=sc.schoology_auth._request_header(), auth=sc.schoology_auth.oauth.auth, json=data)
else:
res = sc.schoology_auth.oauth.get(endpoint, headers=sc.schoology_auth._request_header(), auth=sc.schoology_auth.oauth.auth)
return res
def get_paged_data(
request_function,
endpoint: str,
data_key: str,
next_key: str = 'links',
max_pages: int = -1,
*request_args,
**request_kwargs
):
"""
Schoology requests which deal with large amounts of data are paged.
This function automatically sends the several paged requests and combines the data
"""
data = []
page = 0
next_url = ''
while next_url is not None and (page < max_pages or max_pages == -1):
json = request_function(next_url if next_url else endpoint, *request_args, **request_kwargs).json()
try:
next_url = json[next_key]['next']
except KeyError:
next_url = None
data += json[data_key]
page += 1
return data
# Add all users in the GMHS building to a list, excluding many staff members.
user_ids = []
users = get_paged_data(schoology_req, 'https://api.schoology.com/v1/users?building_id=10704963&limit=200', 'user')
for user in users:
if [user['name_first'], user['name_last']] not in STAFF:
user_ids.append((user['uid'], user['name_first'], user['name_last']))
# Add all people that have already received messages to a "BLOCKED" list
sent = get_paged_data(schoology_req, 'https://api.schoology.com/v1/messages/sent?limit=200', 'message')
for message in sent:
BLOCKED.append(message['recipient_ids'])
# Change to True to actually send the messages
if False:
num = 0
for user_id, name, last in user_ids:
if str(user_id) in BLOCKED:
print('SKIPPING: ' + str(user_id) + ' | ' + name)
continue
print(f'{num + 1} - Sending to: {user_id} ({name}) ...', end='')
schoology_req('https://api.schoology.com/v1/messages', data={
'subject': SUBJECT,
'message': BODY,
'recipient_ids': str(user_id)
})
print('Success')
time.sleep(1)
num += 1
print('Total messages sent: ' + str(num))