-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathed.py
153 lines (109 loc) · 4 KB
/
ed.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
from edapi import EdAPI
from datetime import datetime, timedelta
import discord
def get_threads(amt: int, course_id: int) -> list:
ed = EdAPI()
ed.login()
threads = ed.list_threads(course_id=course_id, limit=amt)
ret_threads = []
for thread in threads:
if 'is_private' in thread and thread["is_private"]:
continue
ret_threads.append(dict(thread))
return ret_threads
def get_thread(id: int) -> dict:
ed = EdAPI()
ed.login()
thread = ed.get_thread(id)
if 'is_private' in thread and thread["is_private"]:
raise Exception("Thread is private")
return dict(thread)
def get_course_thread(course_id: int, thread_number: int) -> dict:
ed = EdAPI()
ed.login()
thread = ed.get_course_thread(course_id, thread_number)
if 'is_private' in thread and thread["is_private"]:
raise Exception("Thread is private")
return dict(thread)
def get_title(thread: dict) -> str:
return thread["title"]
def get_document(thread: dict) -> str:
return thread["document"]
def get_author(thread: dict) -> str | None:
try:
author = thread["user"]["name"]
return author
except TypeError:
return None
def get_category(thread: dict) -> str:
return thread["category"]
def break_string_to_thousands(string: str) -> list[str] | None:
x = len(string) // 1000
str_list = []
for i in range(x + 1):
if i == x:
str_list.append(string)
else:
str_list.append(string[:1000])
string = string[1000:]
if len(str_list[-1]) == 0:
del str_list[-1]
return str_list
def get_link(thread: dict) -> str:
return (
f'https://edstem.org/us/courses/{thread["course_id"]}/discussion/{thread["id"]}'
)
def get_reply_link(reply: dict) -> str:
return f'https://edstem.org/us/courses/{reply["course_id"]}/discussion/{reply["thread_id"]}?comment={get_id(reply)}'
def get_id(thread: dict) -> str:
return thread["id"]
def get_course_id(thread: dict) -> str:
return thread["course_id"]
def get_is_anonymous(thread: dict) -> bool:
return bool(thread["is_anonymous"])
def get_is_pinned(thread: dict) -> bool:
return bool(thread["is_pinned"])
def get_date(thread: dict) -> datetime:
datestring = thread["created_at"]
datestring = datestring[:-3] + datestring[-2:]
dt = datetime.strptime(datestring, "%Y-%m-%dT%H:%M:%S.%f%z")
return dt
def get_date_string(thread: dict) -> str:
datestring = thread["created_at"]
datestring = datestring[:-3] + datestring[-2:]
dt = datetime.strptime(datestring, "%Y-%m-%dT%H:%M:%S.%f%z")
offset_hours = 19
adjusted_dt = dt.replace(tzinfo=None) - timedelta(hours=offset_hours)
formatted_datetime = adjusted_dt.strftime("%m/%d/%Y at %-I:%M:%S%p") + " PST"
return formatted_datetime
def make_embed(thread: dict, color) -> discord.Embed:
try:
author = get_author(thread) + ', '
except KeyError:
author = 'Anonymous, ' if get_is_anonymous(thread) else ''
except TypeError:
author = ''
title = f"{get_title(thread)}: {author}in {get_category(thread)}"
document = get_document(thread)
if len(document) > 4000:
document = document[:4000] + "..."
link = get_link(thread)
embed = discord.Embed(title=title, url=link, description=document, color=color)
embed.set_footer(
text=f"{get_date_string(thread)} | A bot by yousef :D | {get_course_id(thread)} | {get_id(thread)}"
)
return embed
def filter_threads(threads: list[dict], category: str, pinned_ok: bool) -> list[dict]:
filtered_threads = []
for thread in threads:
if not pinned_ok and thread["is_pinned"]:
continue
if bool(thread["is_private"]):
continue
if category == 'All' or thread["category"] == category:
filtered_threads.append(thread)
return filtered_threads
if __name__ == "__main__":
thread = get_course_thread(57105, 45)
for key in thread:
print(key, thread[key])