-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_threads.py
749 lines (629 loc) · 25.6 KB
/
post_threads.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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
"""
This example script will NOT work if you run it directly; it is meant to be used as a reference
in case you'd like to implement something similar.
This script is used for CS70 at UC Berkeley to automatically post
homework, discussion, and note threads. All of these threads are tracked in a locked index post,
which is continually updated by this script as well.
Homework templates are also posted with each homework, by uploading a zip file to Overleaf
and prompting the user to finalize the new Overleaf project.
This Overleaf view link is included in a separate post for students.
Usage:
post_threads.py hw <num>
post_threads.py dis <num> [--summer]
post_threads.py note <num>
post_threads.py init-index
Requirements:
config file with:
course_id: id of course to target
index_thread_num: id of index thread for homeworks/discussions
environment variables:
ED_API_KEY: ed api key
"""
import json
import os
import datetime
from dataclasses import dataclass
from typing import Optional, Union
import re
from ruamel.yaml import YAML
from bs4 import BeautifulSoup, Tag
from edapi import EdAPI
from edapi.constants import ThreadType
from edapi.utils import new_document, parse_content
from PIL import Image
import ed_templates
OVERLEAF_UPLOAD_URL = "https://www.overleaf.com/docs?snip_uri="
ANSI_BLUE = lambda text: f"\u001b[34m{text}\u001b[0m"
@dataclass
class Config:
"""Configuration for posting Ed threads."""
course_id: int
index_thread_num: Optional[int]
overleaf_url: Optional[str]
lectures_path: Optional[str]
def copy(self) -> "Config":
"""Make a copy of the current configuration."""
return Config(course_id=self.course_id, index_thread_num=self.index_thread_num)
@staticmethod
def from_json(obj: dict, id_field: str) -> "Config":
"""Convert a JSON object (dict) into a Config dataclass."""
assert id_field in obj, "config JSON must contain 'course_id' value"
index_thread_num = obj.get("index_thread_num", None)
if index_thread_num is not None:
index_thread_num = int(index_thread_num)
overleaf_url = obj.get("overleaf_link", None)
lectures_path = obj.get("lectures_path", None)
return Config(
course_id=int(obj[id_field]),
index_thread_num=index_thread_num,
overleaf_url=overleaf_url,
lectures_path=lectures_path,
)
def as_json(self) -> dict:
"""Convert this dataclass into a dict to write to a JSON file."""
return {
"course_id": self.course_id,
"index_thread_num": self.index_thread_num,
"overleaf_url": self.overleaf_url,
}
# ========== File path helpers ==========
HOMEWORK_IMAGE_EXTENSION = ".png"
def get_hw_folder(hw_num: str):
"""Compute the base path for homework files."""
return f"./hw-screenshots/hw{hw_num}/"
def get_hw_template_zip(hw_num: str):
"""Compute the path to the homework template zip file."""
return f"../rendered/hw{hw_num}/raw/hw{hw_num}-template.zip"
# ========== Ed post helpers ==========
def parse_template(template: list[Union[str, ed_templates.Link, ed_templates.H2]]) -> tuple[BeautifulSoup, Tag]:
"""
Parse a template into a BeautifulSoup document.
"""
post_soup, document = new_document()
for par in template:
paragraph = post_soup.new_tag("paragraph")
if isinstance(par, list):
for item in par:
if isinstance(item, str):
paragraph.append(item)
elif isinstance(item, ed_templates.Link):
link_tag = post_soup.new_tag("link", href=item.href)
link_tag.string = item.text
paragraph.append(link_tag)
document.append(paragraph)
elif isinstance(par, ed_templates.H2):
if paragraph.string:
document.append(paragraph)
h2_tag = post_soup.new_tag("heading", level=2)
h2_tag.string = par.text
document.append(h2_tag)
else:
paragraph.string = par
document.append(paragraph)
return post_soup, document
def post_exam(ed: EdAPI, config: Config, exam_name: str):
"""
Post past midterm/final exam question threads.
"""
exam_names_long = dict(
mt1="Midterm 1",
mt2="Midterm 2",
final="Final",
)
assert exam_name in exam_names_long, f"Invalid exam name: {exam_name}"
exam_name_fmt = exam_names_long[exam_name]
summary = []
yaml = YAML()
with open(f"./{exam_name}.yml", "r") as f:
exam_data = yaml.load(f)
for entry in reversed(exam_data):
sem = entry["sem"]
# create post body
template = ed_templates.SINGLE_EXAM_TEMPLATE(sem, exam_name_fmt)
post_soup, document = parse_template(template)
links_paragraph = post_soup.new_tag("paragraph")
links_paragraph.string = "Links: "
field_to_text = {
"exam_link": "Blank",
"sol_link": "Solutions",
"video_link": "Video Walkthrough",
"common_mistakes": "Common Mistakes Doc",
}
has_field = False
for field, text in field_to_text.items():
link = entry.get(field, None)
if link is not None:
if has_field:
links_paragraph.append(", ")
link_tag = post_soup.new_tag("link", href=link)
link_tag.string = text
links_paragraph.append(link_tag)
has_field = True
document.append(links_paragraph)
clarifications_hdr = post_soup.new_tag("heading", level=2)
clarifications_hdr.string = "Clarifications/Notes/Errata"
clarifications_paragraph = post_soup.new_tag("list")
for bullet in (entry.get("clarifications", "") or "").split("\n"):
if not bullet:
continue
bullet_tag = post_soup.new_tag("list-item")
bullet_tag.string = bullet
clarifications_paragraph.append(bullet_tag)
if clarifications_paragraph.contents:
document.append(clarifications_hdr)
document.append(clarifications_paragraph)
result = ed.post_thread(
config.course_id,
{
"type": ThreadType.POST,
"title": f"{sem} {exam_name_fmt} Thread",
"category": "Exam",
"subcategory": exam_name_fmt,
"subsubcategory": "",
"content": str(document),
"is_pinned": False,
"is_private": False,
"is_anonymous": False,
"is_megathread": True,
"anonymous_comments": True,
},
)
print(
f"[{sem} {exam_name}] Posted thread for {exam_name_fmt}: #{result['number']}"
)
summary.append(f"{sem} {exam_name_fmt} (#{result['number']})")
template = ed_templates.EXAM_MEGATHREAD_TEMPLATE(exam_name_fmt)
post_soup, document = parse_template(template)
question_list = post_soup.new_tag("list")
question_list.attrs["style"] = "bullet"
for question_content in reversed(summary):
question_item = post_soup.new_tag("list-item")
question_paragraph = post_soup.new_tag("paragraph")
question_paragraph.append(question_content)
question_item.append(question_paragraph)
question_list.append(question_item)
document.append(question_list)
template_result = ed.post_thread(
config.course_id,
{
"type": ThreadType.POST,
"title": f"Past {exam_name_fmt} Megathread",
"category": "Exam",
"subcategory": exam_name_fmt,
"subsubcategory": "",
"content": str(document),
"is_pinned": True,
"is_private": False,
"is_anonymous": False,
"is_megathread": True,
"anonymous_comments": True,
},
)
print(f"Posted megathread for {exam_name_fmt}: #{template_result['number']}")
summary.append(f"{exam_name_fmt} Megathread (#{template_result['number']})")
def post_hw(ed: EdAPI, config: Config, hw_num: str):
"""
Post homework question threads and update the homework/discussion index.
"""
assert config.index_thread_num is not None, "Index thread number must be provided."
summary = []
hw_num_fmt = f"{int(hw_num):02d}"
base_path = get_hw_folder(hw_num)
# retrieve all images from the rendered directory
hw_imgs = [
f
for f in os.listdir(base_path)
if os.path.isfile(os.path.join(base_path, f))
and f.endswith(HOMEWORK_IMAGE_EXTENSION)
]
# sort images by problem number (names are of the format "hwXX-imgXX.png")
hw_imgs.sort(key=lambda x: int(x.split(".")[0].split("-")[1][3:]), reverse=True)
num_imgs = len(hw_imgs)
for hw_idx, hw_img in enumerate(hw_imgs, 1):
with open(base_path + hw_img, "rb") as f:
curr_img = f.read()
# upload image to ed
img_url = ed.upload_file(hw_img, curr_img, "image/png")
print(f"[{hw_idx}/{num_imgs}] Uploaded {hw_img}: {img_url}")
# get image dimensions for display
img = Image.open(base_path + hw_img)
img_width, img_height = img.size
# hw_img has format hw<hw #>-img<problem #>.png
problem_num = hw_img.split(".")[0][-1]
# create post body
problem_soup, document = new_document()
problem_figure = problem_soup.new_tag("figure")
problem_image = problem_soup.new_tag(
"image", src=img_url, width=img_width, height=img_height
)
problem_figure.append(problem_image)
document.append(problem_figure)
result = ed.post_thread(
config.course_id,
{
"type": ThreadType.POST,
"title": f"Homework {hw_num_fmt} Question {problem_num} Thread",
"category": "Homework",
"subcategory": f"HW{hw_num_fmt}",
"subsubcategory": "",
"content": str(document),
"is_pinned": False,
"is_private": False,
"is_anonymous": False,
"is_megathread": True,
"anonymous_comments": True,
},
)
print(
f"[{hw_idx}/{num_imgs}] Posted thread for HW{hw_num_fmt} Q{problem_num}:"
f" #{result['number']}"
)
summary.append(f"Question {problem_num} (#{result['number']})")
summary.reverse()
# LaTeX Template
# with open(get_hw_template_zip(hw_num), "rb") as f:
# template_zip = f.read()
# template_url = ed.upload_file(
# f"hw{hw_num}-template.zip", template_zip, "multipart/form-data"
# )
# template_creation_url = OVERLEAF_UPLOAD_URL + template_url
# print(f"\nGo to:\n\t{ANSI_BLUE(template_creation_url)}")
# Release post
# student_link = input("Enter shareable Overleaf link: ")
student_link = config.overleaf_url
print("Shareable Overleaf link:", student_link)
# create post body
next_friday = (datetime.datetime.now() + datetime.timedelta(
days=(4 - datetime.datetime.now().weekday()) % 7
)).strftime(r"%m/%d")
template = ed_templates.HW_TEMPLATE(hw_num_fmt, next_friday)
post_soup, document = parse_template(template)
question_list = post_soup.new_tag("list")
question_list.attrs["style"] = "bullet"
for question_content in summary:
question_item = post_soup.new_tag("list-item")
question_paragraph = post_soup.new_tag("paragraph")
question_paragraph.append(question_content)
question_item.append(question_paragraph)
question_list.append(question_item)
document.append(question_list)
link_paragraph = post_soup.new_tag("paragraph")
link_paragraph.string = "LaTeX Template link: "
link_link = post_soup.new_tag("link", href=student_link)
link_link.string = student_link
link_paragraph.append(link_link)
document.append(link_paragraph)
# zip_paragraph = post_soup.new_tag("paragraph")
# zip_paragraph.string = "Source files:"
# document.append(zip_paragraph)
# zip_link = post_soup.new_tag(
# "file", url=template_url, filename=f"hw{hw_num}-template.zip"
# )
# zip_paragraph.append(zip_link)
# document.append(zip_link)
template_result = ed.post_thread(
config.course_id,
{
"type": ThreadType.POST,
"title": f"HW {hw_num_fmt} Released",
"category": "Homework",
"subcategory": f"HW{hw_num_fmt}",
"subsubcategory": "",
"content": str(document),
"is_pinned": True,
"is_private": False,
"is_anonymous": False,
"is_megathread": True,
"anonymous_comments": True,
},
)
print(f"Posted template for HW{hw_num_fmt}: #{template_result['number']}")
print(f"https://edstem.org/us/courses/{template_result['course_id']}/discussion/{template_result['id']}")
summary.append(f"LaTeX Template (#{template_result['number']})")
# Update summary
course_id = config.course_id
hw_dis_post_num = config.index_thread_num
hw_dis_post = ed.get_course_thread(course_id, hw_dis_post_num)
hw_dis_post_id = hw_dis_post["id"]
last_content = hw_dis_post["content"]
soup, document = parse_content(last_content)
hw_list = document.find(string=lambda s: s.startswith("Homework Thread")).parent.parent.find("list")
hw_item = soup.new_tag("list-item")
hw_item_paragraph = soup.new_tag("paragraph")
hw_item_paragraph.string = (
f"Homework {str(int(hw_num_fmt))}: #{template_result['number']}"
)
hw_item.append(hw_item_paragraph)
hw_list.append(hw_item)
ed.edit_thread(hw_dis_post_id, {"content": str(document)})
print("Updated Index Thread")
# unpin old threads
for row in hw_list.find_all("list-item"):
res = re.search(r"#\d+", row.text)
if res is not None:
old_thread_num = int(res.group()[1:])
if old_thread_num == template_result["number"]:
continue
old_thread = ed.get_course_thread(config.course_id, old_thread_num)
if old_thread and old_thread.get("is_pinned", False):
old_thread_id = old_thread["id"]
ed.edit_thread(old_thread_id, {"is_pinned": False})
print(f"Unpinned old HW thread: #{old_thread_num}")
def post_dis(ed: EdAPI, config: Config, dis_num: str, is_summer: bool):
"""
Post discussion thread and update the homework/discussion index.
"""
assert config.index_thread_num is not None, "Index thread number must be provided."
dis_num_fmt = int(dis_num)
# create post body
template = ed_templates.DIS_TEMPLATE(dis_num_fmt)
discussion_soup, document = parse_template(template)
# post thread
dis_post_result = ed.post_thread(
config.course_id,
{
"type": ThreadType.POST,
"title": f"Discussion {dis_num_fmt} Thread",
"category": "Discussion",
"subcategory": "",
"subsubcategory": "",
"content": str(document),
"is_pinned": True,
"is_private": False,
"is_anonymous": False,
"is_megathread": True,
"anonymous_comments": True,
},
)
print(f"Posted discussion {dis_num_fmt}: #{dis_post_result['number']}")
# update summary
hw_dis_post_num = config.index_thread_num
hw_dis_post = ed.get_course_thread(config.course_id, hw_dis_post_num)
hw_dis_post_id = hw_dis_post["id"]
last_content = hw_dis_post["content"]
soup, document = parse_content(last_content)
dis_list = document.find(string="Discussion Threads:", recursive=True).parent.parent.find("list")
dis_item = soup.new_tag("list-item")
dis_item_paragraph = soup.new_tag("paragraph")
dis_item_paragraph.string = (
f"Discussion {dis_num_fmt}: #{dis_post_result['number']}"
)
dis_item.append(dis_item_paragraph)
dis_list.append(dis_item)
ed.edit_thread(hw_dis_post_id, {"content": str(document)})
print("Updated Index Thread")
# unpin old threads
for row in dis_list.find_all("list-item"):
res = re.search(r"#\d+", row.text)
if res is not None:
old_thread_num = int(res.group()[1:])
if old_thread_num == dis_post_result["number"]:
continue
old_thread = ed.get_course_thread(config.course_id, old_thread_num)
if old_thread and old_thread.get("is_pinned", False):
old_thread_id = old_thread["id"]
ed.edit_thread(old_thread_id, {"is_pinned": False})
print(f"Unpinned old discussion thread: #{old_thread_num}")
def post_lec(ed: EdAPI, config: Config, week_num: str):
"""
Post discussion thread and update the homework/discussion index.
"""
assert config.index_thread_num is not None, "Index thread number must be provided."
week_num_fmt = int(week_num)
# create post body
# read weeks.yml and get the lec topics for this week
lecs_path = config.lectures_path
topics = []
yaml = YAML()
with open(lecs_path, "r") as f:
lecs = yaml.load(f)
for lec in lecs:
if int(lec["week"]) == week_num_fmt:
# skip malformed lectures, holidays, and midterms
if "title" not in lec:
continue
if "holiday" in lec:
continue
title = lec["title"]
if "midterm" in title.lower():
continue
if "no lecture" in title.lower():
continue
topics.append(title)
topics_fmt = ", ".join(topics)
template = ed_templates.LEC_TEMPLATE(topics_fmt)
lec_soup, document = parse_template(template)
# post thread
lec_post_result = ed.post_thread(
config.course_id,
{
"type": ThreadType.POST,
"title": f"Week {week_num_fmt} Lecture Thread",
"category": "Lecture",
"subcategory": "",
"subsubcategory": "",
"content": str(document),
"is_pinned": True,
"is_private": False,
"is_anonymous": False,
"is_megathread": True,
"anonymous_comments": True,
},
)
print(f"Posted week {week_num_fmt} lec thread: #{lec_post_result['number']}")
# update summary
hw_dis_post_num = config.index_thread_num
hw_dis_post = ed.get_course_thread(config.course_id, hw_dis_post_num)
hw_dis_post_id = hw_dis_post["id"]
last_content = hw_dis_post["content"]
soup, document = parse_content(last_content)
lec_list = document.find(string="Weekly Lecture Threads:", recursive=True).parent.parent.find("list")
lec_item = soup.new_tag("list-item")
lec_item_paragraph = soup.new_tag("paragraph")
lec_item_paragraph.string = (
f"Week {week_num} ({topics_fmt}): #{lec_post_result['number']}"
)
lec_item.append(lec_item_paragraph)
lec_list.append(lec_item)
ed.edit_thread(hw_dis_post_id, {"content": str(document)})
print("Updated Index Thread")
# unpin old threads
for row in lec_list.find_all("list-item"):
res = re.search(r"#\d+", row.text)
if res is not None:
old_thread_num = int(res.group()[1:])
if old_thread_num == lec_post_result["number"]:
continue
old_thread = ed.get_course_thread(config.course_id, old_thread_num)
if old_thread and old_thread.get("is_pinned", False):
old_thread_id = old_thread["id"]
ed.edit_thread(old_thread_id, {"is_pinned": False})
print(f"Unpinned old lecture thread: #{old_thread_num}")
def post_note(ed: EdAPI, config: Config, note_num: str):
"""
Post note thread and update the homework/discussion index.
"""
assert config.index_thread_num is not None, "Index thread number must be provided."
# create post body
note_soup, document = new_document()
note_link_paragraph = note_soup.new_tag("paragraph")
note_link_paragraph.string = f"Note {note_num}: "
link_text = f"https://www.eecs70.org/assets/pdf/notes/n{note_num}.pdf"
note_link = note_soup.new_tag("link", href=link_text)
note_link.string = link_text
note_link_paragraph.append(note_link)
document.append(note_link_paragraph)
# second paragraph
note_paragraph = note_soup.new_tag("paragraph")
note_paragraph.string = (
f"Please ask any questions you have about note {note_num} here."
)
document.append(note_paragraph)
# post thread
note_post_result = ed.post_thread(
config.course_id,
{
"type": ThreadType.POST,
"title": f"Note {note_num} Thread",
"category": "Notes",
"subcategory": "",
"subsubcategory": "",
"content": str(document),
"is_pinned": False,
"is_private": False,
"is_anonymous": False,
"is_megathread": True,
"anonymous_comments": True,
},
)
print(f"Posted note {note_num} thread: #{note_post_result['number']}")
# update summary
hw_dis_post_num = config.index_thread_num
hw_dis_post = ed.get_course_thread(config.course_id, hw_dis_post_num)
hw_dis_post_id = hw_dis_post["id"]
last_content = hw_dis_post["content"]
soup, document = parse_content(last_content)
# hw list is first, dis list is second, note list is third
note_list = document.find_all("list", recursive=False)[2]
note_item = soup.new_tag("list-item")
note_item_paragraph = soup.new_tag("paragraph")
note_item_paragraph.string = f"Note {note_num} (#{note_post_result['number']})"
note_item.append(note_item_paragraph)
note_list.append(note_item)
ed.edit_thread(hw_dis_post_id, {"content": str(document)})
print("Updated Index Thread")
def init_index(ed: EdAPI, config: Config):
"""Initialize the index post, updating the config file with the post number."""
course_id = config.course_id
index_soup, document = new_document()
# Homework section
homework_title = index_soup.new_tag("heading", level=2)
homework_title.string = "Homeworks"
homework_list = index_soup.new_tag("list", style="bullet")
document.extend([homework_title, homework_list])
# Discussion section
discussion_title = index_soup.new_tag("heading", level=2)
discussion_title.string = "Discussions"
discussion_list = index_soup.new_tag("list", style="bullet")
document.extend([discussion_title, discussion_list])
# Note section
note_title = index_soup.new_tag("heading", level=2)
note_title.string = "Notes"
note_list = index_soup.new_tag("list", style="bullet")
document.extend([note_title, note_list])
# create and lock the index thread
index_thread = ed.post_thread(
course_id,
{
"type": ThreadType.POST,
"title": "Homework/Discussion/Note Thread",
"category": "Logistics",
"subcategory": "",
"subsubcategory": "",
"content": str(document),
"is_pinned": True,
"is_private": False,
"is_anonymous": False,
"is_megathread": True,
"anonymous_comments": True,
},
)
ed.lock_thread(index_thread["id"])
index_thread_num = index_thread["number"]
print(f"Created index thread: #{index_thread_num}")
# update config json file
new_config = Config(course_id=config.course_id, index_thread_num=index_thread_num)
with open("./config.json", "w", encoding="utf-8") as config_file:
json.dump(new_config.as_json(), config_file, indent=2)
config_file.write("\n") # add newline at end of file
print("Updated config file")
def main(args):
"""
Main method, delegating to other functions to post threads.
"""
# read configuration
with open("./config.json", "r", encoding="utf-8") as config_file:
config_json = json.load(config_file)
config = Config.from_json(config_json, args.id_field)
ed = EdAPI()
ed.login()
if args.type == "hw":
post_hw(ed, config, args.num)
elif args.type == "dis":
post_dis(ed, config, args.num, args.summer)
elif args.type == "note":
post_note(ed, config, args.num)
elif args.type == "lec":
post_lec(ed, config, args.num)
elif args.type == "exam":
post_exam(ed, config, args.exam)
elif args.type == "init-index":
init_index(ed, config)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--id_field",
help="If multiple courses, specify which course forum to post to",
default="draft_course_id",
)
subparsers = parser.add_subparsers(dest="type")
subparsers.required = True
hw_parser = subparsers.add_parser("hw")
dis_parser = subparsers.add_parser("dis")
exam_parser = subparsers.add_parser("exam")
lec_parser = subparsers.add_parser("lec")
note_parser = subparsers.add_parser("note")
init_parser = subparsers.add_parser("init-index")
# add "num" argument to hw/dis/note parsers
for p in (hw_parser, dis_parser, note_parser, lec_parser):
p.add_argument("num", help="HW/discussion/note/week number")
exam_parser.add_argument("exam", help="Exam name (mt1, mt2, final)")
# optional summer flag for discussions
dis_parser.add_argument(
"--summer",
action="store_true",
help="Flag for summer discussions; creates 4 discussion threads instead of 2",
)
arguments = parser.parse_args()
main(arguments)