-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeamSnappier.py
589 lines (393 loc) · 17.7 KB
/
TeamSnappier.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
import requests
import json
import configparser
import csv
class TeamSnappier:
def __init__(self):
config = configparser.ConfigParser()
config.read('config.ini')
self.access_token = config['api']['access_token']
self.headers = {
"Authorization": f"Bearer {self.access_token}"
}
def find_me(self):
API_HREF = "https://api.teamsnap.com/v3/me"
response = requests.get(API_HREF, headers=self.headers)
if response.status_code == 200:
print("find_me() was successful!\n")
parsed_json = response.json()
list_of_myself = []
myself = {}
for myself_item in parsed_json["collection"]["items"]:
myself_data = myself_item["data"]
for item in myself_data:
myself[item["name"]] = item["value"]
list_of_myself.append(myself)
return list_of_myself
user_id = parsed_json["collection"]["items"][0]["data"][00]["value"]
return user_id
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
return None
# ... [Include other methods here in the same manner]
# ...
def get_url(self,url):
# Obtain API specification for an endpoint given URL
API_HREF = url
response = requests.get(API_HREF, headers=self.headers)
if response.status_code == 200:
print("get_url() was successful!\n")
parsed_json = response.json()
my_list = []
my_list.append(parsed_json)
return my_list
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def list_assignments(self, teamid):
params = {
'team_id': teamid
}
API_HREF = f"https://api.teamsnap.com/v3/assignments/search"
response = requests.get(API_HREF, headers=self.headers, params=params)
if response.status_code == 200:
print("list_teams() was successful!\n")
parsed_json = response.json()
list_of_objects = []
for object_item in parsed_json["collection"]["items"]:
object_data = object_item["data"]
object = {}
for item in object_data:
object[item["name"]] = item["value"]
list_of_objects.append(object)
return list_of_objects
def list_opponents(self, teamid):
params = {
'team_id': teamid
}
API_HREF = f"https://api.teamsnap.com/v3/opponents/search"
response = requests.get(API_HREF, headers=self.headers, params=params)
if response.status_code == 200:
print("list_teams() was successful!\n")
parsed_json = response.json()
list_of_objects = []
for object_item in parsed_json["collection"]["items"]:
object_data = object_item["data"]
object = {}
for item in object_data:
object[item["name"]] = item["value"]
list_of_objects.append(object)
return list_of_objects
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def list_members(self, teamid):
params = {
'team_id': teamid
}
API_HREF = f"https://api.teamsnap.com/v3/members/search"
response = requests.get(API_HREF, headers=self.headers, params=params)
if response.status_code == 200:
print("list_teams() was successful!\n")
parsed_json = response.json()
list_of_objects = []
for object_item in parsed_json["collection"]["items"]:
object_data = object_item["data"]
object = {}
for item in object_data:
object[item["name"]] = item["value"]
list_of_objects.append(object)
return list_of_objects
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def list_statistics(self, teamid):
params = {
'team_id': teamid
}
API_HREF = f"https://api.teamsnap.com/v3/statistic_aggregates/search"
response = requests.get(API_HREF, headers=self.headers, params=params)
if response.status_code == 200:
print("list_teams() was successful!\n")
parsed_json = response.json()
list_of_objects = []
for object_item in parsed_json["collection"]["items"]:
object_data = object_item["data"]
object = {}
for item in object_data:
object[item["name"]] = item["value"]
list_of_objects.append(object)
return list_of_objects
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def list_divisions(self,divisionid):
params = {
'id': divisionid
}
API_HREF = f"https://api.teamsnap.com/v3/divisions/search"
response = requests.get(API_HREF, headers=self.headers, params=params)
if response.status_code == 200:
print("list_teams() was successful!\n")
parsed_json = response.json()
list_of_objects = []
for object_item in parsed_json["collection"]["items"]:
object_data = object_item["data"]
object = {}
for item in object_data:
object[item["name"]] = item["value"]
list_of_objects.append(object)
return list_of_objects
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def list_members(self,team_id):
params = {
'team_id': team_id
}
API_HREF = f"https://api.teamsnap.com/v3/members/search" # Replace with your endpoint URL
response = requests.get(API_HREF, headers=self.headers, params=params)
if response.status_code == 200:
print("list_members() was successful!")
parsed_json = response.json()
print("n")
myList = []
for item in parsed_json["collection"]["items"]:
data = item["data"]
team = {}
for subitem in data:
team[subitem["name"]] = subitem["value"]
myList.append(team)
return myList
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def list_division_locations(self,divisionid):
params = {
'division_id': divisionid
}
API_HREF = f"https://api.teamsnap.com/v3/division_locations/search"
response = requests.get(API_HREF, headers=self.headers, params=params)
if response.status_code == 200:
print("list_teams() was successful!\n")
parsed_json = response.json()
list_of_objects = []
for object_item in parsed_json["collection"]["items"]:
object_data = object_item["data"]
object = {}
for item in object_data:
object[item["name"]] = item["value"]
list_of_objects.append(object)
return list_of_objects
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def list_teams(self,userid):
params = {
'user_id': userid
}
API_HREF = f"https://api.teamsnap.com/v3/teams/search"
response = requests.get(API_HREF, headers=self.headers, params=params)
if response.status_code == 200:
print("list_teams() was successful!\n")
parsed_json = response.json()
list_of_teams = []
for team_item in parsed_json["collection"]["items"]:
team_data = team_item["data"]
team = {}
for item in team_data:
team[item["name"]] = item["value"]
list_of_teams.append(team)
return list_of_teams
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def list_events(self,userid=None,teamid = None):
params = {
'user_id': userid,
'team_id': teamid
}
API_HREF = f"https://api.teamsnap.com/v3/events/search" # Replace with your endpoint URL
response = requests.get(API_HREF, headers=self.headers, params=params)
if response.status_code == 200:
print("list_events() was successful!")
parsed_json = response.json()
list_of_events = []
for item in parsed_json["collection"]["items"]:
data = item["data"]
event = {}
for subitem in data:
event[subitem["name"]] = subitem["value"]
list_of_events.append(event)
return list_of_events
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def search_user(self,userid):
params = {
'id': userid
}
API_HREF = f"https://apiv3.teamsnap.com/users/search" # Replace with your endpoint URL
response = requests.get(API_HREF, headers=headers, params=params)
if response.status_code == 200:
print("search_user() was successful!\n")
parsed_json = response.json()
print(f"id: is {parsed_json['collection']['items'][0]['data'][0]['value']}")
print(f"email: {parsed_json['collection']['items'][0]['data'][5]['value']}")
print(f"First Name: {parsed_json['collection']['items'][0]['data'][8]['value']}")
print(f"Last Name: {parsed_json['collection']['items'][0]['data'][10]['value']}\n")
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
#TODO Fix creating multiple events Iiteration
def create_events(self,events_csv):
list_of_events = self.csv_to_templates(events_csv)
for event in list_of_events:
API_HREF = f"https://api.teamsnap.com/v3/events" # Replace with your endpoint URL
response = requests.post(API_HREF, headers=self.headers, json=event)
if response.status_code in (200, 201, 204):
print(f"{response.status_code} request successful!")
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def create_opponents(self, events_csv):
list_of_opponents = self.csv_to_templates(events_csv)
for opponent in list_of_opponents:
API_HREF = f"https://api.teamsnap.com/v3/opponents" # Replace with your endpoint URL
response = requests.post(API_HREF, headers=self.headers, json=opponent)
if response.status_code in (200, 201, 204):
print(f"{response.status_code} request successful!")
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def create_team_member(self,csv_file):
list_of_members = self.csv_to_templates(csv_file)
for member in list_of_members:
API_HREF = f"https://api.teamsnap.com/v3/members" # Replace with your endpoint URL
response = requests.post(API_HREF, headers=self.headers, json=member)
if response.status_code in (200, 201, 204):
print(f"{response.status_code} request successful!")
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def create_assignments(self,assignments_csv):
list_of_assignments = self.csv_to_templates(assignments_csv)
for assignment in list_of_assignments:
API_HREF = f"https://api.teamsnap.com/v3/assignments" # Replace with your endpoint URL
response = requests.post(API_HREF, headers=self.headers, json=assignment)
if response.status_code in (200, 201, 204):
print(f"{response.status_code} request successful!")
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def delete_opponents_by_ids(self, opponent_list):
for opponent in opponent_list:
API_HREF = f"https://api.teamsnap.com/v3/opponents/{opponent}" # Replace with your endpoint URL
response = requests.delete(API_HREF, headers=self.headers)
if response.status_code in (200, 201, 204):
print(f"{response.status_code} request successful!")
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def delete_events_by_id(self, event_list):
for event in event_list:
API_HREF = f"https://api.teamsnap.com/v3/events/{event}" # Replace with your endpoint URL
response = requests.delete(API_HREF, headers=self.headers)
if response.status_code in (200, 201, 204):
print(f"{response.status_code} request successful!")
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
def delete_events_by_dict(self,events_dict):
for event in events_dict:
API_HREF = f"https://api.teamsnap.com/v3/events/{event['id']}" # Replace with your endpoint URL
response = requests.delete(API_HREF, headers=self.headers)
if response.status_code in (200, 201, 204):
print(f"{response.status_code} request successful!")
else:
print(f"Request failed with status code: {response.status_code}")
print(response.text)
@staticmethod
def print_list(list,variables=[]):
print(f"************************")
if variables:
for item in list:
for variable in variables:
print(f"{variable}: {item[variable]}")
print("---------------------------------------------")
else:
for dict in list:
for k,v in dict.items():
print(f"{k}: {v}")
print("---------------------------------------------")
@staticmethod
def json_to_csv(json_data, csv_filename):
# Ensure input is a list of dictionaries
if not isinstance(json_data, list) or not all(isinstance(item, dict) for item in json_data):
raise ValueError("Input JSON should be a list of dictionaries")
# Extract column headers from the keys of the dictionaries
headers = list(json_data[0].keys())
# Write to CSV file
with open(csv_filename, 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=headers)
writer.writeheader()
for row in json_data:
writer.writerow(row)
@staticmethod
def csv_to_templates(filename):
"""
Convert a CSV file into a list of template dicts, skipping rows where the first cell is "Mandatory" or "Optional".
Args:
- filename (str): Path to the CSV file.
Returns:
- list of dicts: The converted JSON format.
"""
with open(filename, 'r') as file:
reader = csv.reader(file)
# Check if the file is empty
try:
headers = next(reader)
except StopIteration:
return {
"template": {
"data": []
}
}
number_of_columns = len(headers)
# Initialize the data list
data_list = []
for values in reader:
# Skip rows where the first cell is "Mandatory" or "Optional"
if values[0] in ("Mandatory", "Optional"):
continue
row_data = []
for i in range(number_of_columns):
value = values[i]
name = headers[i]
row_data.append({
"name": name,
"value": value
})
data_list.append({"template": {"data": row_data}}) # Append row_data as a dict
return data_list
@staticmethod
def print_members(memberList):
print(f"Printing Members:")
print(f"************************")
for member in memberList:
print(f"First Name: {member['first_name']}")
print(f"Last Name: {member['last_name']}")
print(f"Email address: {member['email_addresses']}\n")
@staticmethod
def write_to_json_file(data, filename="output.json"):
"""
Write a Python dictionary to a JSON file.
Parameters:
- data (dict): The dictionary to write to the file.
- filename (str): The name of the file to which the data should be written. Defaults to "output.json".
Returns:
None
"""
with open(filename, 'w') as file:
json.dump(data, file, indent=4)