forked from apozharski/gradescope-api
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
136 lines (95 loc) · 3.25 KB
/
main.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
import dotenv
from threading import Thread
from pyscope import pyscope as gs
from ics import Calendar, Event
from getpass import getpass
import os
import hashlib
from pyscope.account import GSAccount, GSCourse
dotenv.load_dotenv()
def login(email, pwd) -> GSAccount:
conn = gs.GSConnection()
print("Logging in...")
conn.login(email, pwd)
# Get Account
success = conn.get_account()
if not success:
print("Failed to get account")
return None
return conn.account
def get_courses(acct: GSAccount) -> list[GSCourse]:
return list(acct.student_courses.values())
def get_semesters(courses: list[GSCourse]) -> list[str]:
semesters = set()
for course in courses:
semesters.add(course.year)
return list(semesters)
def create_hw_events(course: GSCourse, assignment: dict) -> list[Event]:
name = assignment["name"]
# print(f"\tProcessing Assignment: {name}")
assigned = assignment["assigned"]
due = assignment["due"]
date_printfmt = "%A, %d. %B %Y %I:%M%p"
desc = f"""Course: {course.name}
Course Shortname: {course.shortname}
Link to Course: {course.get_url()}
Assignment: {name}
Assigned: {assigned.strftime(date_printfmt) if assigned is not None else "None"}
Due: {due.strftime(date_printfmt) if due is not None else "None"}"""
out = []
def hw_event(name, course_name, type, time):
event = Event()
event.name = f"{name} - {course_name} {type}"
event.begin = time
event.end = time
event.description = desc
hash = f"{name}{course_name}{time.strftime('%Y%m%d')}"
hash = hashlib.sha1(hash.encode("utf-8")).hexdigest()
event.uid = f"{hash}@sagar.sh"
return event
if assigned is not None:
out.append(hw_event(name, course.shortname, "Assigned", assigned))
if due is not None:
out.append(hw_event(name, course.shortname, "Due", due))
return out
def get_course_events(course: GSCourse, all_events: list[Event]):
course_name = course.shortname
print(f"Started Processing Course: {course_name}")
assignments = course.get_assignments()
# all_events = []
for assign in assignments:
events = create_hw_events(course, assign)
all_events.extend(events)
print(f"Finished Processing Course: {course_name}")
return
def do_the_thing(email, pwd, sem=None):
acct: GSAccount = login(email, pwd)
courses = get_courses(acct)
cal = Calendar()
threads = []
events = []
for course in courses:
if sem is not None:
if course.year != sem:
continue
thread = Thread(target=get_course_events, args=(course, events))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
for event in events:
cal.events.add(event)
return cal.serialize_iter()
if __name__ == "__main__":
email = os.environ.get("GS_EMAIL")
pwd = os.environ.get("GS_PWD")
sem = os.environ.get("GS_SEM")
# Login
if email is None:
email = input("Email: ")
if pwd is None:
pwd = getpass("Password: ")
if sem is None:
sem = input("Semester: ")
with open("gradescope.ics", "w") as f:
f.writelines(do_the_thing(email, pwd, sem))