-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.py
323 lines (270 loc) · 9.88 KB
/
output.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
import json
import random
import string
def main():
data = open("data/cleaned.json", "r")
data_json = json.load(data)
data.close()
parse_dosen_matkul(data_json)
generate_dosen_sql()
generate_matkul_sql()
parse_prodi(data_json)
parse_prodi_to_sql(data_json)
parse_fakultas(data_json)
generate_dosen_matkul_sql()
def clean_str(str: str):
return str.replace(".", "")\
.replace("'", "")\
.replace(")", "")\
.replace("(", "")\
.replace(",", "")\
.replace("&", "")\
.replace("-", "")
def generate_slug(input: str):
removed_dot = clean_str(input).lower()
splitted = removed_dot.split(" ")
result = "-".join(splitted)
result += "-" + generate_random()
return result
def generate_random():
charset = string.ascii_lowercase
ans = ""
size = len(charset)
for i in range(8):
ans += charset[random.randint(0, size - 1)]
return ans
def generate_dosen_sql():
f = open("data/id_prodi_map.json", "r")
id_prodi_map = json.load(f)
f.close()
dosen_id_name = {}
f = open("data/dosen_matkul_map.json", "r")
list_all_dosen = json.load(f)
f.close()
SQL_STATEMENT = 'insert into public.professors (id, "name", institution_id, slug, major_id) values\n'
ctr = 1
for dosen in list_all_dosen:
slug_dosen = generate_slug(dosen)
try:
major_id = id_prodi_map[list_all_dosen[dosen]['kode_prodi']]
except:
continue
dosen_name = dosen.replace("'", "")
dosen_id_name[dosen_name] = ctr
SQL_STATEMENT += f"({ctr}, '{dosen_name}', 1, '{slug_dosen}', {major_id}),\n"
ctr += 1
SQL_STATEMENT = SQL_STATEMENT[:-2] + ';'
f = open("sql/dosen.sql", "w")
f.write(SQL_STATEMENT)
f.close()
to_write = json.dumps(dosen_id_name)
f = open("data/dosen_id_name.json", "w")
f.write(to_write)
f.close()
def generate_matkul_sql():
f = open("data/id_prodi_map.json", "r")
id_prodi_map = json.load(f)
f.close()
matkul_id_name = {}
f = open("data/matkul_dosen_map.json", "r")
list_all_matkul = json.load(f)
f.close()
SQL_STATEMENT = 'insert into public.courses (id, course_id, course_name, institution_id, slug, major_id, sks) values\n'
ctr = 1
# kode_matkul
for matkul in list_all_matkul:
slug_matkul = generate_slug(matkul).replace("--", "-")
matkul_name = list_all_matkul[matkul]['nama_matkul']
sks = list_all_matkul[matkul]['sks']
try:
major_id = id_prodi_map[list_all_matkul[matkul]['kode_prodi']]
except:
continue
matkul_name = matkul_name.replace("'", "")
matkul_id_name[matkul_name] = ctr
SQL_STATEMENT += f"({ctr}, '{matkul}', '{matkul_name}', 1, '{slug_matkul}', {major_id}, {sks}),\n"
ctr += 1
SQL_STATEMENT = SQL_STATEMENT[:-2] + ';'
f = open("sql/matkul.sql", "w")
f.write(SQL_STATEMENT)
f.close()
to_write = json.dumps(matkul_id_name)
f = open("data/matkul_id_name.json", "w")
f.write(to_write)
f.close()
def generate_dosen_matkul_sql():
f = open("data/matkul_id_name.json", "r")
matkul_id_name = json.load(f)
f.close()
f = open("data/dosen_id_name.json", "r")
dosen_id_name = json.load(f)
f.close()
f = open("data/dosen_matkul_map.json", "r")
list_all_dosen = json.load(f)
f.close()
SQL_STATEMENT = 'insert into public.professor_course (id, professor_id, course_id) values\n'
ctr = 1
for dosen in list_all_dosen:
for matkul in list_all_dosen[dosen]['list_matkul']:
try:
dosen_id = dosen_id_name[dosen]
matkul_id = matkul_id_name[matkul]
except:
continue
SQL_STATEMENT += f"({ctr}, {dosen_id}, {matkul_id}),\n"
ctr += 1
SQL_STATEMENT = SQL_STATEMENT[:-2] + ";"
f = open("sql/matkul_dosen.sql", "w")
f.write(SQL_STATEMENT)
f.close()
def parse_dosen_matkul(data):
list_all_dosen = {}
list_all_matkul = {}
for index, fakultas in enumerate(data):
for index, prodi in enumerate(data[fakultas]):
list_data = data[fakultas][prodi]
for dosen_matkul in list_data:
nama_matkul = dosen_matkul['nama_matkul']
kode_matkul = dosen_matkul['kode_matkul']
nama_prodi = dosen_matkul['nama_prodi']
kode_prodi = dosen_matkul['kode_prodi']
sks = dosen_matkul['sks']
if kode_prodi.startswith("1"):
nama_prodi = f"Sarjana - {nama_prodi}"
elif kode_prodi.startswith("2"):
nama_prodi = f"Magister - {nama_prodi}"
elif kode_prodi.startswith("3"):
nama_prodi = f"Doktor - {nama_prodi}"
# Dosen
for dosen in dosen_matkul['list_dosen']:
if dosen not in list_all_dosen.keys():
list_all_dosen[dosen] = {}
list_all_dosen[dosen]['list_matkul'] = set()
list_all_dosen[dosen]['kode_prodi'] = kode_prodi
list_all_dosen[dosen]['nama_prodi'] = nama_prodi
list_all_dosen[dosen]['list_matkul'].add(nama_matkul)
else:
list_all_dosen[dosen]['list_matkul'].add(nama_matkul)
# Matkul
if kode_matkul not in list_all_matkul.keys():
list_all_matkul[kode_matkul] = {}
list_all_matkul[kode_matkul]['sks'] = sks
list_all_matkul[kode_matkul]["nama_matkul"] = nama_matkul
list_all_matkul[kode_matkul]['kode_matkul'] = kode_matkul
list_all_matkul[kode_matkul]['kode_prodi'] = kode_prodi
list_all_matkul[kode_matkul]['nama_prodi'] = nama_prodi
list_all_matkul[kode_matkul]["list_dosen"] = set()
list_all_matkul[kode_matkul]["list_dosen"].add(dosen)
else:
list_all_matkul[kode_matkul]["list_dosen"].add(dosen)
# Dosen
for dosen in list_all_dosen:
list_all_dosen[dosen]['list_matkul'] = list(
list_all_dosen[dosen]['list_matkul'])
to_write = json.dumps(list_all_dosen)
f = open("data/dosen_matkul_map.json", "w")
f.write(to_write)
f.close()
for matkul in list_all_matkul:
list_all_matkul[matkul]['list_dosen'] = list(
list_all_matkul[matkul]['list_dosen'])
# Matkul
to_write = json.dumps(list_all_matkul)
f = open("data/matkul_dosen_map.json", "w")
f.write(to_write)
f.close()
def parse_fakultas(data):
f = open("data/fakultas_shorthand.json", "r")
fakutlas_shorthand_map_json = json.load(f)
f.close()
f = open("data/fakultas.json", "r")
fakultas_map = json.load(f)
f.close()
to_write = f'insert into public.faculties (id, institution_id, "name", code) values '
ctr = 1
for fakultas in data:
try:
kode_fakultas = fakutlas_shorthand_map_json[fakultas]
for kode in kode_fakultas:
to_write += f"({ctr}, 1, '{fakultas_map[kode]}', {int(kode)}),\n"
ctr += 1
except KeyError:
pass
sql_statement = to_write[:-2] + ';'
f = open("sql/fakultas.sql", "w")
f.write(sql_statement)
f.close()
def parse_prodi(data):
prodi_namaprodi = {}
for index, fakultas in enumerate(data):
for index, prodi in enumerate(data[fakultas]):
list_data = data[fakultas][prodi]
if not list_data:
continue
nama_prodi = list_data[0]['nama_prodi']
kode_prodi = list_data[0]['kode_prodi']
a = ""
if kode_prodi.startswith("1"):
nama_prodi = f"Sarjana - {nama_prodi}"
elif kode_prodi.startswith("2"):
nama_prodi = f"Magister - {nama_prodi}"
elif kode_prodi.startswith("3"):
nama_prodi = f"Doktor - {nama_prodi}"
prodi_namaprodi[kode_prodi] = nama_prodi
to_write = json.dumps(prodi_namaprodi)
f = open("data/prodi.json", "w")
f.write(to_write)
f.close()
FAKULTAS_MAPPING = {
"FMIPA": 1,
"SITH": [2, 3],
"SF": 4,
"FTTM": 5,
"FITB": 6,
"FTI": 7,
"STEI": 8,
"FTMD": 9,
"FTSL": 10,
"SAPPK": 11,
"FSRD": 12,
"SBM": 13
}
def parse_prodi_to_sql(data):
sql_statement = f'insert into public.majors (id, faculty_id, "name", code) values '
ctr = 1
id_prodi_map = {}
for fakultas in data:
for prodi in data[fakultas]:
list_data = data[fakultas][prodi]
if not list_data:
continue
nama_prodi = list_data[0]['nama_prodi']
kode_prodi = list_data[0]['kode_prodi']
try:
faculty_id = FAKULTAS_MAPPING[fakultas]
except:
continue
if fakultas == "SITH":
if "Rekayasa" in nama_prodi:
faculty_id = faculty_id[1]
else:
faculty_id = faculty_id[0]
if kode_prodi.startswith("1"):
nama_prodi = f"Sarjana - {nama_prodi}"
elif kode_prodi.startswith("2"):
nama_prodi = f"Magister - {nama_prodi}"
elif kode_prodi.startswith("3"):
nama_prodi = f"Doktor - {nama_prodi}"
sql_statement += f"({ctr}, {faculty_id}, '{nama_prodi}', {kode_prodi}),\n"
id_prodi_map[kode_prodi] = ctr
ctr += 1
id_prodi = json.dumps(id_prodi_map)
f = open("data/id_prodi_map.json", "w")
f.write(id_prodi)
f.close()
sql_statement = sql_statement[:-2] + ';'
f = open("sql/prodi.sql", "w")
f.write(sql_statement)
f.close()
if __name__ == "__main__":
main()