-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
88 lines (73 loc) · 2.67 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
import base64
import html
import os
import sys
import argparse
import re
# Handle path & folder.
def create_arg_parser():
# Creates and returns the ArgumentParser object.
parser = argparse.ArgumentParser()
parser.add_argument("inputDir", help="Path to the input directory.")
parser.add_argument(
"outputDir",
nargs="?",
help="Path to the output directory.",
)
args = parser.parse_args()
return parser
def decode_file(fn, folder):
# Decodes the given file and saves it as a HTML file in the given folder.
# Check if file exists.
if not os.path.exists(fn):
print("[Error] File not found! Exit.")
return
# Open File and Read It.
with open(fn, "r") as file:
encodedQaAs = re.findall(r"(?<=content\":\s\").+?(?=\")", file.read())
# Check if file is valid.
if not encodedQaAs:
with open(fn, "r") as file:
encodedQaAs = re.findall(r"(?<=content\":\").+?(?=\")", file.read())
if not encodedQaAs:
print("[Error] File is not supported! Exit.")
return
else:
print("[INFO] File supported! Start decoding.")
else:
print("[INFO] File supported! Start decoding.")
# Decode QaA then store it in a list.
print("[INFO] Decoding questions and answers.")
decodedQaAs = []
for encodedQaA in encodedQaAs:
decodedQaAs.append(html.unescape((base64.b64decode(encodedQaA)).decode("utf-8")))
# Decode HTML and apply color format for correct answer.
print("[INFO] Applying color format.")
split = []
questionNumber = 0
for decodedQaA in decodedQaAs:
if re.search(r"<li class=\"correctAnswer\">", decodedQaA) is None:
split.append(re.sub(r"<li class = 'correctAnswer'>", "<li style=\"color:red\">", decodedQaA))
else:
split.append(re.sub(r"<li class=\"correctAnswer\">", "<li style=\"color:red\">", decodedQaA))
# Save.
try:
os.mkdir(folder)
except FileExistsError:
pass
os.chdir(folder)
with open("decode.htm", "w", encoding="utf-8") as file2:
for i in split:
file2.write(f"Câu {questionNumber + 1}:\n")
file2.write(i + "\n")
questionNumber += 1
print(f"[INFO] Done! File is stored in {os.path.join(folder, 'decode.htm')}")
if __name__ == "__main__":
arg_parser = create_arg_parser()
parsed_args = arg_parser.parse_args(sys.argv[1:])
fn = parsed_args.inputDir
if parsed_args.outputDir == None:
folder = os.path.abspath(os.path.dirname(parsed_args.inputDir))
else:
folder = parsed_args.outputDir
decode_file(fn, folder)