-
Notifications
You must be signed in to change notification settings - Fork 2
/
piazza-to-latex.py
80 lines (68 loc) · 2.85 KB
/
piazza-to-latex.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
from piazza_api import Piazza
import time
import re
import subprocess, os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def cleanhtml(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
def clean(str):
return cleanhtml(str.replace('+', '+').replace('`', '`').replace('\\', '\\\\').replace('@', '@').replace('&', '&').replace('"', "''").replace(''', "'").replace('>', "\\textgreater{}").replace('<', "\\textless{}").replace('&', '\\&').replace('#', '\\#').replace('_', '\_').replace('$', '\$').replace('^', '\^{}'))
p = Piazza()
p.user_login()
class_id = raw_input("Enter class ID: ")
course_piazza = p.network(class_id)
posts = course_piazza.iter_all_posts()
text = ""
print "Working..."
for post in reversed(list(posts)):
subject = clean(post['history'][0]['subject'])
text += "\\section*{" + str(post['nr']) + ": " + subject + "}\n"
if len(post['children']) > 0:
text += "\\subsection*{Question}\n"
text += clean(post['history'][0]['content']) + "\n"
for child in post['children']:
if 'history' in child:
if child['type'] == 's_answer':
text += "\\subsection*{Student Answer}\n"
text += clean(child['history'][0]['content']) + "\n"
elif child['type'] == 'i_answer':
text += "\\subsection*{Instructor Answer}\n"
text += clean(child['history'][0]['content']) + "\n"
elif child['type'] == 'followup':
text += "\\subsection*{Followup}\n"
text += clean(child['subject']) + "\n"
for inner_child in child['children']:
if inner_child['type'] == 'feedback':
text += "\\subsubsection*{Feedback}\n"
text += clean(inner_child['subject']) + "\n"
elif 'no_answer' in post and post['no_answer'] == 1:
text += "\\subsection*{Question}\n"
text += clean(post['history'][0]['content']) + "\n"
text += "\\subsection*{**No Answer**}\n"
elif clean(post['history'][0]['content']) != "":
text += "\\subsection*{Note}\n"
text += clean(post['history'][0]['content']) + "\n"
f = open("piazza-export-" + class_id + ".tex", "w+")
f.write("\\documentclass[10pt]{article}\n")
f.write("\\usepackage[utf8]{inputenc}\n")
f.write("\\usepackage[margin=1in]{geometry}\n")
f.write("\\DeclareUnicodeCharacter{00A0}{ }")
f.write("\\title{Piazza Export}\n")
f.write("\\author{Class ID: " + class_id + "}\n")
f.write("\\date{\\today}\n")
f.write("\\begin{document}\n")
f.write("\\maketitle\n")
f.write(text)
f.write("\\end{document}")
f.close()
os.system("pdflatex piazza-export-" + class_id + ".tex")
if sys.platform.startswith('darwin'):
subprocess.call(('open', "piazza-export-" + class_id + ".pdf"))
elif os.name == 'nt':
os.startfile("piazza-export-" + class_id + ".pdf")
elif os.name == 'posix':
subprocess.call(('xdg-open', "piazza-export-" + class_id + ".pdf"))