-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
80 lines (64 loc) · 2.75 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
import os
import requests
from dotenv import load_dotenv
from datetime import timezone, timedelta, datetime as dt
from gradescope.methods import get_courses, get_assignments
if __name__ == '__main__':
load_dotenv()
embeds = []
pastdue = str()
todue_list, todue_fields = [], []
due_today_count = 0
now = dt.now(timezone.utc)
for course in get_courses():
assignments = get_assignments(course['id'])
filtered = sorted(list(filter(
lambda x:
x['is_submitted'] == False and
x['course_term'] == 'Fall 2024' and
isinstance(x['due_time'], dt),
assignments)), key=lambda x: x['due_time'])
if not filtered: continue
todue_str = str()
field_title = course['abbrv']
for assgn in filtered[:8]: # max 8 assignments for each field (more may result in exception)
due_dt = assgn['due_time']
due = '<t:{}:R>'.format(int(due_dt.timestamp()))
late_due = '<t:{}:R>'.format(int(late_due.timestamp())) if (late_due := assgn.get('late_due_time')) else '`N/A`'
if now < due_dt:
todue_str += '\n[{}]({})\nDue: {} \nLate Due: {}\n'.format(assgn['title'], assgn['url'], due, late_due)
todue_list.append({'title': assgn['title'], 'url': assgn['url'], 'due': due_dt, 'course': assgn['course_abbrv']})
if now + timedelta(days=1) > due_dt:
due_today_count += 1
else:
pastdue += '- [{}]({}) - {} {}\n'.format(
assgn['title'], assgn['url'], assgn['course_abbrv'], due)
if todue_str:
todue_fields.append({'name': field_title, 'value': todue_str, 'inline': True})
todue_list = sorted(todue_list, key=lambda x: x['due'])
todue_desc = '## Summary\n- Total: **{}**\n- Due Today: **{}**\n'.format(len(todue_list), due_today_count)
if todue_list and (nextdue := todue_list[0]):
todue_desc += '- Next due: **[{}]({}) ({}) <t:{}:R>**'.format(
nextdue['title'], nextdue['url'], nextdue['course'], int(nextdue['due'].timestamp()))
embeds.append({
'title': '⚠️ Missing Submission',
'type': 'rich',
'color': 0xFF3131,
'description': pastdue,
})
embeds.append({
'title': '🔔 Daily Homework Reminder',
'type': 'rich',
'color': 0x90EE90,
'fields': todue_fields,
'description': todue_desc,
})
req = requests.post(
url=os.getenv('WEBHOOK_URL'),
json={
'username': 'Gradescope',
'avatar_url': '',
'embeds': embeds
})
if req.status_code != 204: # normal status code
req.raise_for_status()