-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.py
More file actions
312 lines (285 loc) · 13.9 KB
/
Main.py
File metadata and controls
312 lines (285 loc) · 13.9 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
import getpass
from edsby import Edsby
from time import sleep
from datetime import datetime, timedelta
from configparser import ConfigParser
import sys
import os
directory = os.path.dirname(os.path.realpath(__file__))
# ANSI Color Definitions
HEADER = '\033[95m'
OK_BLUE = '\033[94m'
OK_GREEN = '\033[92m'
WARNING = '\033[93m'
WHITE = '\033[37m'
CYAN = '\033[36m'
RED = '\033[31m'
MAGENTA = '\033[35m'
YELLOW = '\033[33m'
FAIL = '\033[91m'
END_C = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
GRAY = '\033[30m'
# Prints lines slower, for added effect
def printd(string, end='\n'):
sleep(.05)
print(string, end=end)
# Converts dates to local time
def convertDate(dateIn):
if len(dateIn) > 10:
newDate = datetime.strptime(dateIn, '%Y-%m-%d %H:%M:%S') - timedelta(hours=4)
elif len(dateIn) <= 8:
newDate = datetime.strptime(dateIn, '%Y%m%d')
else:
newDate = datetime.strptime(dateIn, '%Y-%m-%d')
return newDate
# Method for 'loading' text that disappears
def loading(message):
printd(YELLOW + "\n " + message + WHITE, '\r')
def loaded(message):
clean = " "
for ii in range(len(message)):
clean += " "
printd(clean, '\r')
# Main CLI loop
while True:
# Read config file
exists = os.path.isfile(directory + "/testConfig.ini")
config = ConfigParser()
if exists:
config.read(directory + "/testConfig.ini")
rHOST = config.get('auth', 'host')
rUSERNAME = config.get('auth', 'username')
rPASSWORD = config.get('auth', 'password')
# Confirm auth method
if exists:
HOST = input("\nPress enter to sign in with credentials from the config file ("+rUSERNAME+"@"+rHOST+").\nOtherwise, enter the host prefix for your edsby instance (*.edsby.com): ")
else:
HOST = input("\nConfig file not detected. Enter the host prefix for your edsby instance (*.edsby.com): ")
if HOST != "":
HOST += ".edsby.com"
USERNAME = input("Username: ")
PASSWORD = getpass.getpass()
save = "n"
if not exists or USERNAME != rUSERNAME:
save = input("Would you like to save this to the config file? This will overwrite the current config. (y/n)")
if save == "y":
file = open(directory+"/testConfig.ini", "w")
if not exists:
config.add_section("auth")
config.set("auth", "host", HOST)
config.set("auth", "username", USERNAME)
config.set("auth", "password", PASSWORD)
config.write(file)
file.close()
exists = True
else:
HOST = rHOST
USERNAME = rUSERNAME
PASSWORD = rPASSWORD
printd(BOLD + HEADER + "–----------------------------------------------------------------------------------------------------------------------")
edsby = Edsby(host=HOST)
# Attempt login
printd(HEADER + "Logging " + USERNAME + " into " + HOST + "..." + END_C)
for i in range(1, 10):
result = False
try:
result = edsby.login(username=USERNAME, password=PASSWORD)
except:
pass
if result:
printd(OK_GREEN + "Logged in (" + str(i) + " attempts)\n" + END_C)
break
sleep(2)
# Logged in CLI loop
while True:
action = ""
printd(UNDERLINE + WHITE + "Choose an action:" + END_C)
printd(WHITE + "\t0: Logout")
printd("\t1: Get class averages")
printd("\t2: Get class assignments")
printd("\t3: Get schedule")
while True:
action = input("Action: ")
if action in ["0", "1", "2", "3"]:
break
else:
printd("Invalid action, try again.")
# Action 0 - exit
if action == "0":
loading("Logging out...")
edsby.logout()
sleep(.5)
loaded("Logging out...")
printd("Logged out.")
break
# Action 1 - class averages
if action == "1":
loading("Loading class averages...")
courses = edsby.getAllClassAverages()
loaded("Loading class averages...")
printd(WHITE + UNDERLINE + "Grades summary:" + END_C)
for entry in courses:
printd(CYAN + '\t' + courses[entry]['human_name'] + ": " + WHITE + str(courses[entry]['average']) + "%")
printd("")
# Action 2 - class assignments
if action == "2":
action2 = ""
loading("Loading class list...")
courses = edsby.getAllClasses()
loaded("Loading class list...")
numberedCourses = [0]
i = 1
for course in courses:
numberedCourses.append(course)
i += 1
printd(WHITE + UNDERLINE + "Choose an option to get class assignments for:" + END_C)
printd("\t" + WHITE + "0:" + CYAN + " All classes")
i = 1
for entry in courses:
courses[entry]['index'] = str(i)
printd("\t" + WHITE + str(i) + ": " + CYAN + courses[entry]['human_name'])
i += 1
while True:
action2 = input(WHITE + "Option: ")
try:
if int(action2) in range(0, i):
break
else:
printd(WHITE + "Invalid option, try again.")
except ValueError:
printd(WHITE + "Invalid option, try again.")
ASSIGNMENTS = {}
if action2 == "0":
loading("Loading assignments from all classes...")
else:
loading("Loading assignments from " + str(courses[numberedCourses[int(action2)]]['human_name']) + "...")
for course in courses:
if action2 != "0" and course != numberedCourses[int(action2)]:
continue
assignmentsByDate = {}
courseNID = course
courseRID = courses[course]['rid']
average = edsby.getClassAverage(courseNID)
courses[course]['average'] = average
assignmentSummary = edsby.getClassAssignmentList(courseNID, courseRID)
for entry in assignmentSummary['assignments']:
assignment = assignmentSummary['assignments'][entry]
assignmentsByDate[assignment['date'] + str(assignment['nid'])] = assignment
score = assignment['score']
outof = assignment['columns']
date = assignment['date'][:10]
try:
if assignment['scheme'] == "gs_outof":
try:
percent = float(score) / float(outof)*100.0
assignmentsByDate[assignment['date'] + str(assignment['nid'])]['info'] = str(
WHITE + "\t" + date + " " + CYAN + assignment['name'] + ": " + WHITE + str(
score) + "/" + str(outof) + " (" + str(format(round(percent, 2)), ) + "%)")
except:
percent = "--"
assignmentsByDate[assignment['date'] + str(assignment['nid'])]['info'] = str(
WHITE + "\t" + date + " " + CYAN + assignment['name'] + ": " + WHITE + str(
score) + "/" + str(outof) + " (--%)")
elif assignment['scheme'] == "gs_kica":
if len(assignment['weighting']) != 0:
percent = 0
weightSum = 0
for category in assignment['score']:
percent += ((assignment['weighting'][category]) * (float(assignment['score'][category])/float(assignment['columns'][category])))
weightSum += assignment['weighting'][category]
percent /= weightSum
percent *= 100
else:
percent = "--"
assignmentsByDate[assignment['date'] + str(assignment['nid'])]['info'] = (WHITE + "\t" + date + CYAN + " " + assignment['name'] + ": ")
for s in score:
assignmentsByDate[assignment['date'] + str(assignment['nid'])]['info'] += (WHITE + s.upper() + ":" + str(score[s]) + "/" + str(outof[s]) + " ")
assignmentsByDate[assignment['date'] + str(assignment['nid'])]['info'] += ("(" + str(round(percent, 2)) + "%)")
elif assignment['scheme'] == "gs_4levelplusminus" or assignment['scheme'] == "gs_4level":
assignmentsByDate[assignment['date'] + str(assignment['nid'])]['info'] = (WHITE + "\t" + date + " " + CYAN + assignment['name'] + ": " + WHITE + assignment['score'])
elif assignment['scheme'] == "gs_yesno":
percent = "100" if assignment['score'] == 'yes' else "0"
assignmentsByDate[assignment['date'] + str(assignment['nid'])]['info'] = (WHITE + "\t" + date + " " + CYAN + assignment['name'] + ": " + WHITE + assignment['score'].upper() + " (" + percent + "%)")
elif assignment['scheme'] == "gs_percent":
percent = assignment['score']
assignmentsByDate[assignment['date'] + str(assignment['nid'])]['info'] = (WHITE + "\t" + date + " " + CYAN + assignment['name'] + ": " + WHITE + percent + "%")
except Exception as e:
printd(e)
printd(assignment)
if str(average) == "":
percentage = "--"
else:
percentage = str(average)
ASSIGNMENTS[course] = assignmentsByDate
if action2 == "0":
loaded("Loading assignments from all classes...")
for course in courses:
printd(MAGENTA + UNDERLINE + '\nAssignment summary for ' + courses[course]['human_name'] +':' + END_C)
for assg in ASSIGNMENTS[course]:
printd(ASSIGNMENTS[course][assg]['info'])
printd(WHITE + 'Your average in ' + courses[course]['human_name'] + ' is currently ' + str(courses[course]['average']) + '%.')
printd("")
else:
loaded("Loading assignments from " + str(courses[numberedCourses[int(action2)]]['human_name']) + "...")
n = int(action2)
printd(MAGENTA + UNDERLINE + '\nAssignment summary for ' + courses[numberedCourses[n]]['human_name'] +':' + END_C)
for entry in sorted(ASSIGNMENTS[numberedCourses[n]]):
try:
printd(ASSIGNMENTS[numberedCourses[n]][entry]['info'])
except KeyError:
print(assignmentSummary)
printd(WHITE + 'Your average in '+courses[numberedCourses[n]]['human_name']+' is currently '+str(average)+'%.\n')
# Action 3 - schedule
elif action == "3":
NONE = (WHITE + "██" + WHITE)
PRESENT = (OK_GREEN + "██" + WHITE)
LATE = (YELLOW + "██" + WHITE)
ABSENT = (RED + "██" + WHITE)
BLANK = " "
attendance = {-1:NONE, 0:PRESENT, 1:LATE, 2:ABSENT, 3:ABSENT}
printd(WHITE + "\nPress enter to get today's schedule, or type a date (YYYYMMDD).")
dateGet = ""
dateStr = ""
while True:
action3 = input(WHITE + "Date: ")
if action3 == "":
dateGet = datetime.today()
dateStr = dateGet.strftime('%Y%m%d')
break
elif not (action3.isdigit() and action3.startswith("20") and len(action3)==8):
printd("Invalid date, try again.")
else:
dateGet = datetime.strptime(action3, '%Y%m%d')
dateStr = action3
break
loading("Loading schedule for " + dateGet.strftime('%A, %B %e, %Y') + "...")
try:
schedule = edsby.getSchedule(dateStr)
loaded("Loading schedule for " + dateGet.strftime('%A, %B %e, %Y') + "...")
except KeyError:
loaded("Loading schedule for " + dateGet.strftime('%A, %B %e, %Y') + "...")
printd(UNDERLINE + "Schedule for " + dateGet.strftime('%A, %B %e, %Y') + ":" + END_C)
printd(WHITE + "\tNothing scheduled.\n")
continue
scheduleOut = {}
for r in schedule:
newDate = convertDate(schedule[r]['sdate'])
if newDate.day != dateGet.day:
continue
scheduleOut[str(newDate) + str((len(schedule[r]['class']) != 0)) + str(schedule[r]['nid'])] = schedule[r]
printd(UNDERLINE + "Schedule for " + dateGet.strftime('%A, %B %e, %Y') + ":" + END_C)
if len(scheduleOut) == 0:
printd(WHITE + "\tNothing scheduled.\n")
for s in sorted(scheduleOut):
printd("\t", '')
a = BLANK if 'objtype_13' not in scheduleOut[s] else attendance[int(scheduleOut[s]['objtype_13']['attendance'])]
d = convertDate(scheduleOut[s]['sdate'])
name = (CYAN + scheduleOut[s]['name'] + WHITE) if len(scheduleOut[s]['class']) == 0 else (YELLOW + scheduleOut[s]['class'] + WHITE)
if d.strftime('%-I:%M %p') == '12:00 AM':
printd("{0} {1:11}{2}".format(a, "", name))
else:
printd("{0} {1:11}{2}".format(a, d.strftime('%-I:%M %p'), name))
printd("")
printd('\n')