-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_navigation.py
88 lines (60 loc) · 2.48 KB
/
build_navigation.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 json
from array import array
import time
import utils
result = None
cnt = None
start_time = time.time()
tabs = []
for idx in range(0, 100):
tabs.append(idx * ' ')
def walk(node, tab_cnt):
global result, tabs, cnt
node_id = node["id"] if "id" in node else ""
title = node["title"] if "title" in node else ""
ka_url = node["ka_url"] if "ka_url" in node else ""
kind = node["kind"] if "kind" in node else ""
pictograph = ''
if kind == "Video":
pictograph = '🎥 '
cnt["video"]["items"] += 1
cnt["video"]["ids"][node_id] = True
result.frombytes((tabs[tab_cnt] + '<li id="' + node_id + '"><a href="' + ka_url + '">' + pictograph + title + "</a>").encode())
if "children" in node and len(node["children"]) > 0:
result.frombytes(("\n" + tabs[tab_cnt + 1] + "<ul>\n").encode())
for child in node["children"]:
walk(child, tab_cnt + 2)
result.frombytes((tabs[tab_cnt + 1] + "</ul>\n" + tabs[tab_cnt] + "</li>\n").encode())
elif "id" in node or "title" in node or "ka_url" in node:
result.frombytes("</li>\n".encode())
def run(input_file, output_file):
print(input_file, "->", output_file)
global result, cnt
result = array('b')
cnt = {"video": {"items": 0, "ids": {}}}
with open(input_file, "r") as f:
topic_tree = json.load(f)
result.frombytes("<ul>".encode())
walk(topic_tree, 0)
result.frombytes("</ul>".encode())
with open(output_file, "w") as out:
out.write(result.tobytes().decode())
print("🎥 items count:", cnt["video"]["items"])
print("🎥 ids count:", len(cnt["video"]["ids"]))
def all_subjects():
utils.silentremove("Topics and Exercises.html")
run("api/v1/topictree/Topics and Exercises.json", "Topics and Exercises.html")
utils.silentremove("Topics only.html")
run("api/v1/topictree/Topics only.json", "Topics only.html")
utils.silentremove("topictree.html")
run("api/v1/topictree/topictree.json", "topictree.html")
def math():
utils.silentremove("Math Topics and Exercises.html")
run("api/v1/topictree/Math Topics and Exercises.json", "Math Topics and Exercises.html")
utils.silentremove("Math Topics only.html")
run("api/v1/topictree/Math Topics only.json", "Math Topics only.html")
utils.silentremove("Math topictree.html")
run("api/v1/topictree/Math topictree.json", "Math topictree.html")
# all_subjects()
math()
print("--- %s seconds ---" % (time.time() - start_time))