-
Notifications
You must be signed in to change notification settings - Fork 0
/
masterrota.py
322 lines (279 loc) · 9.49 KB
/
masterrota.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
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
#!/usr/bin/python3
import json
import sys
from datetime import datetime, timedelta
import dateutil.parser
import drive
import emails
import requests
import tablib
from lxml import html
from terminaltables import AsciiTable
CA_DATE_FORMAT = '%d-%m-%Y'
CA_AJAX_DATE_FORMAT = '%Y-%m-%d'
SHEETS_ROOT_URL = 'https://docs.google.com/spreadsheets/d/'
LEAD_ROLES = ['leader', 'preacher', ]
EXCLUDE_ROLES = ['reserve', ]
def is_leader_role(role):
role = role.lower()
for lrole in LEAD_ROLES:
if lrole in role:
return True
return False
def login(churchname, username, password, siteid=None):
"""
Login to churchsuite
"""
site_switcher_url = 'https://{churchname}.churchsuite.co.uk/ajax/site'
login_url = "https://login.churchsuite.com/"
print('Logging in via {}'.format(login_url))
session = requests.Session()
login_response = session.post(
login_url,
data={
'username': username,
'password': password,
'system': 'admin',
},
cookies={
'churchapp_login_account': churchname
}
)
if login_response.url == login_url:
print('Error - login failed!')
sys.exit(1)
print('Login OK')
if siteid is not None:
print('Switching site to: {}'.format(siteid))
session.put(
site_switcher_url.format(churchname=churchname),
data={
'site_id': siteid,
},
)
return session
def fetch_overview(churchname, username, password, year=None, siteid=None):
report_url = "https://{churchname}.churchsuite.co.uk/modules/rotas/reports/rotas_overview.php?date_start={fromdate}&date_end={todate}&order_by=default&submit_btn=Generate" # noqa
ajax_report_url = "https://{churchname}.churchsuite.co.uk/ajax/rotas/rajax?date_start={fromdate}&date_end={todate}&order_by=default&break_page_on_week=off&show_empty_dates=off&show_members_table=off&page=1&submit_btn=Generate&pg=rotas_overview&view=dates" # noqa
if year is None:
now = datetime.now()
fromdate = datetime.now()
todate = (now + timedelta(days=365))
else:
fromdate = datetime(year, 1, 1)
todate = datetime(year+1, 1, 1)
print('Fetching rotas from {} to {}'.format(
fromdate.date(), todate.date()
))
session = login(churchname, username, password, siteid)
url = ajax_report_url.format(
churchname=churchname,
fromdate=fromdate.strftime(CA_AJAX_DATE_FORMAT),
todate=todate.strftime(CA_AJAX_DATE_FORMAT),
)
print('Running report: {}'.format(url))
response = session.get(url)
return response.text
def grab_text(el, selector):
return el.cssselect(selector)[0].text_content()
def parse_data(text):
print('Parsing...')
master = []
team_names = []
tree = html.fromstring(text)
# First pass to read data
dates = tree.cssselect('.rota-section h2.report_break')
if not dates:
print('Error: No dates found')
return
for el in dates:
date_rotas = {}
datetext = el.text_content()
thedate = dateutil.parser.parse(datetext)
# We only care about sundays at the moment
if thedate.weekday() != 6:
continue
rotas = el.getparent().cssselect('div.rota-date')
for rota in rotas:
team = grab_text(rota, '.date-rota-name')
if team not in team_names:
team_names.append(team)
date_rotas[team] = []
members = rota.cssselect('ul.date-members li.profile-initial')
for member in members:
name = grab_text(member, '.profile-name')
role = grab_text(member, '.roles')
date_rotas[team].append({
'name': name,
'role': role,
})
master.append((thedate, date_rotas))
# now organise
team_names.sort()
dataset = tablib.Dataset()
dataset.headers = ['Date', ] + team_names
for thedate, rotas in master:
row = [thedate.date(), ]
for team_name in team_names:
names = []
rota = rotas.get(team_name)
if rota:
rota.sort(key=lambda x: x['role'])
included = []
for member in rota:
if is_leader_role(member['role']):
included.append(member)
if not included:
# No leader match - just use all of them
included = rota
for member in included:
name = member['name']
if member['role'].lower() in EXCLUDE_ROLES:
continue
elif member['role'] and not \
is_leader_role(member['role']):
name += ' ({})'.format(member['role'])
names.append(name)
row.append(', '.join(names))
dataset.append(row)
return dataset
def write_to_sheet(rows, sheetid, range_name, clear=True):
body = {
'values': rows
}
service = drive.get_service()
if clear:
service.spreadsheets().values().clear(
spreadsheetId=sheetid,
body={},
range=range_name
).execute()
service.spreadsheets().values().update(
spreadsheetId=sheetid,
body=body,
valueInputOption='USER_ENTERED',
range=range_name
).execute()
print('Changes written to: {}{}'.format(SHEETS_ROOT_URL, sheetid))
def write_overview(dataset, sheetid):
# Overview
print('Updating Overview Sheet...')
timestamp = get_timestamp()
values = [
['', "Last update: {}".format(timestamp), ],
dataset.headers,
]
for row in dataset:
cols = [str(x) for x in row]
values.append(cols)
write_to_sheet(values, sheetid, "Overview")
def display_rows(rows):
table = AsciiTable(rows)
print(table.table)
def write_next(dataset, sheetid, churchname,
site_name, notify=None, smtp=None):
# Next sunday
print('Updating Next Sunday Sheet...')
timestamp = get_timestamp()
sunday = dataset.dict[0]
nicedate = sunday['Date'].strftime('%A %d %b %Y').replace(" 0", " ")
header_rows = [
('Next Sunday', "Last update: {}".format(timestamp), ),
(nicedate, ''),
]
rows = []
for header in dataset.headers[1:]:
value = str(sunday[header])
if value:
rows.append((header, value))
write_to_sheet(header_rows + rows, sheetid, "Next Sunday")
display_rows(header_rows + rows)
# Emails
if notify and smtp:
next_dataset = tablib.Dataset(
*rows,
headers=('Rota', 'People'))
html = """<style>th, td {{ border-bottom: 1px solid #ccc; padding: 3px; }}</style>
<p><em>This is an automated email generated from all rotas on <a href="{churchsuiteurl}">{churchsuiteurl}</a></em></p>
<p>
<b>{nicedate}</b>
</p>
{table}
<p><em>
You can <a href="{churchsuiteurl}">update these rotas in ChurchSuite</a>
</em></p>
<p><em>
Further detail on all rotas is available in <a href="{sheeturl}">this google sheet</a>
</em></p>
<p>
Please note:
<br />
- Only non-leader roles are included after names
<br />
- People with the following roles are excluded from this report: {excluded}
<br />
</p>
""".format(table=next_dataset.html,
churchsuiteurl='https://{}.churchsuite.co.uk/modules/rotas/'.format(
churchname),
sheeturl=SHEETS_ROOT_URL + sheetid,
nicedate=nicedate,
excluded=', '.join(EXCLUDE_ROLES))
message = emails.html(
html=html,
subject='[{}] Sunday Roles {}'.format(site_name, nicedate),
mail_from=smtp.get('user'),
headers={
'X-Mailer': 'ChurchSuite Master Rota',
'X-Auto-Response-Suppress': (
'DR, NDR, RN, NRN, OOF, AutoReply'),
})
for address in notify:
response = message.send(
to=address,
smtp={
"host": smtp.get('host'),
"ssl": smtp.get('ssl', False),
"port": smtp.get('port', 25),
"user": smtp.get('user', None),
"password": smtp.get('password', None),
}
)
print('Notifying {} ({})'.format(address, response.status_code))
def get_timestamp():
return datetime.now().strftime('%Y-%m-%d %H:%M')
if __name__ == "__main__":
try:
configfile = sys.argv[1]
except IndexError:
print('Please provide a JSON config file')
sys.exit(1)
if '--notify' in sys.argv:
notify = True
else:
notify = False
with open(configfile, 'r') as f:
config = json.load(f)
if '--test' in sys.argv:
overview = open('example.html').read()
else:
overview = fetch_overview(
config['churchname'],
config['username'],
config['password'],
siteid=config.get('site_id', None),
)
dataset = parse_data(overview)
if not (dataset and len(dataset)):
print('Error: no data was parsed!!')
sys.exit(1)
write_overview(dataset, config['google_sheet_id'])
write_next(
dataset,
config['google_sheet_id'],
config['churchname'],
config['site_name'],
notify=notify and config.get('notify'),
smtp=config.get('smtp'),
)
print('Done')