Repository navigation
Expand file tree
/
Copy pathASAP_Enrollments_to_CSV.py
More file actions
77 lines (75 loc) · 3.58 KB
/
Copy pathASAP_Enrollments_to_CSV.py
File metadata and controls
77 lines (75 loc) · 3.58 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
import pandas as pd
import requests, json, logging, sys
from pathlib import Path
from logging.handlers import SysLogHandler
#load configs
confighome = Path.home() / ".ASAPCanvas" / "ASAPCanvas.json"
try:
with open(confighome) as f:
configs = json.load(f)
except EnvironmentError:
print('Config file not found. Check to make sure there is a .ASAPCanvas folder in your home directory with a ASAPCanvas.json file in that folder.')
raise
# Logging
if configs['logserveraddress'] is None:
logfilename = Path.home() / ".ASAPCanvas" / configs['logfilename']
thelogger = logging.getLogger('MyLogger')
thelogger.basicConfig(filename=str(logfilename), level=logging.INFO)
else:
thelogger = logging.getLogger('MyLogger')
thelogger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = (configs['logserveraddress'],514))
thelogger.addHandler(handler)
thelogger.info('Loaded config file and logfile started')
csvfilename = sys.argv[1]
#-----ASAP Info
userid = configs['ASAPuserid']
orgid = configs['ASAPorgid']
password = configs['ASAPpassword']
apikey = configs['ASAPAPIKey']
url = configs['ASAPurl']
headers = {'Authorization' : 'user='+userid+'&organizationId='+orgid+'&password='+password+'&apiKey='+apikey}
thelogger.info('Getting ASAP Key')
r = requests.get(url,headers = headers)
if r.status_code == 404:
thelogger.info('Failed to get ASAP Key')
elif r.status_code == 200:
thelogger.info('Got ASAP Key')
accesstoken = r.json()
thelogger.info('Key is ' + accesstoken)
url2 = configs['ASAPapiurl']
header = {'asap_accesstoken' : accesstoken}
thelogger.info('Getting data from ASAP')
r2 = requests.get(url2,headers = header)
#print(r2)
results = pd.concat([pd.json_normalize(r2.json()), pd.json_normalize(r2.json(),record_path="Students", max_level=2)], axis=1).drop(columns=['Students'])
# was drop('Students',1)
#Drop columns we don't need
'''
results.drop(results.columns.difference(['CreatedDate',
'EventEnrollmentID',
'ScheduledEvent.Course.CourseName',
'ScheduledEvent.Course.IsOnline',
'EnrollmentStatusCd',
'ScheduledEvent.EventCd',
'StudentID',
'CustomerID',
'Person.Email',
'Person.FirstName',
'Person.LastName']),axis=1,inplace=True)
'''
# EventEnrollmentID
#Drop columns we don't need, keep the ones we want
# results.drop(results.columns.difference(['CreatedDate',
# 'EventEnrollmentID',
# 'InvoiceItems.InvoiceItemID',
# 'ScheduledEvent.Course.CourseName',
# 'EnrollmentStatusCd',
# 'ScheduledEvent.EventCd',
# 'StudentID',
# 'CustomerID',
# 'Person.Email',
# 'Person.FirstName',
# 'Person.LastName']),axis=1,inplace=True)
print(results)
results.to_csv(csvfilename)