This repository was archived by the owner on Sep 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate.py
More file actions
355 lines (287 loc) · 11.4 KB
/
generate.py
File metadata and controls
355 lines (287 loc) · 11.4 KB
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
import argparse
import asyncio
import hashlib
import json
import multiprocessing as mp
import os
import re
import shutil
import sys
from collections import defaultdict
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from io import BytesIO
import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup
from pyppeteer import errors, launch
# Mutex
process_lock = mp.Lock()
# URLs
program_url = "https://resources.bth.se/ProgrammeOverviews/Sv/"
teacher_url = "https://www.bth.se/?s=%s&searchtype=employee"
# Paths
ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
DATA_PATH = os.path.join(ROOT_PATH, "app", "data")
PUBLIC_PATH = os.path.join(ROOT_PATH, "app", "public", "data")
TEACHER_CSV = os.path.join(DATA_PATH, "teachers.csv")
TEACHER_JSON = os.path.join(DATA_PATH, "teachers.json")
YEARS_CSV = os.path.join(DATA_PATH, "years.csv")
YEARS_JSON = os.path.join(DATA_PATH, "years.json")
INDEX_PATH = os.path.join(DATA_PATH, "index.json")
NAMES_PATH = os.path.join(DATA_PATH, "names.json")
# Regex
excel_regex = re.compile(r"\.xlsx$")
year_regex = re.compile(r"\d{4}")
program_regex = re.compile(r"\w{5}\d{2}[vh]\.json")
# CLI
parser = argparse.ArgumentParser(description="Generate data for the app")
parser.add_argument("-u", "--update", action="store_true", help="Updates the data")
parser.add_argument("-d", "--delete", action="store_true", help="Deletes the data")
parser.add_argument("-n", "--number", help="Download data for n programs", type=int)
parser.add_argument(
"--skip-teachers", action="store_true", help="Skips downloading teacher data"
)
args = vars(parser.parse_args())
# Global
teacher_codes = set()
async def fetch(url: str, retries: int = 5) -> str:
"""Asynchronously fetches the HTML content of a webpage with retry logic"""
for attempt in range(retries):
try:
browser = await launch()
page = await browser.newPage()
await page.goto(url)
html = await page.content()
await browser.close()
return html
except errors.NetworkError as e:
print(f"NetworkError on attempt {attempt + 1}: {e}")
if attempt == retries - 1:
raise
await asyncio.sleep(2)
except Exception as e:
print(f"Error on attempt {attempt + 1}: {e}")
if attempt == retries - 1:
raise
await asyncio.sleep(2)
def get_program_code(url: str) -> str:
"""Parses the url to get the program code"""
code = url.replace(" ", "").split(".")[-2].split("-")[-1]
year = year_regex.findall(url)
semester = "h" if "Höst" in url else "v"
return f"{code[:5]}{year[-1][-2:]}{semester}"
def download_program(url: str) -> None:
"""Downloads the program data"""
try:
code = get_program_code(url)
data = requests.get(url)
df = pd.read_excel(BytesIO(data.content))
df = df.rename(
columns={
"Kurskod": "code",
"Kurs": "name",
"Poäng": "points",
"Termin": "semester",
"Startvecka": "start_week",
"Slutvecka": "end_week",
"Läsperiod": "periods",
"Typ": "type",
"Inriktning": "academic_focus",
"Förkunskapskrav": "prerequisites",
"Kursansvarig": "teacher",
"Länk till kursansvarig": "teacher_url",
"Ersättande kurs": "replacement",
"Nästa kurstillfalle": "next_instance",
"Länk till kursplan": "syllabus_url",
"Länk till utbildningsplan": "education_plan_url",
}
)
df["code"] = df["code"].str.upper()
df["points"] = (
df["points"]
.str.extract(r"(\d+[\.\,]?\d*)")[0]
.str.replace(",", ".")
.astype(float)
)
# Reformat times
df["start_week"] = df["start_week"].astype(str)
df["end_week"] = df["end_week"].astype(str)
df[["start_year", "start_week"]] = df["start_week"].str.extract(
r"(\d{4})(\d{2})"
)
df[["end_year", "end_week"]] = df["end_week"].str.extract(r"(\d{4})(\d{2})")
df[["start_year", "start_week", "end_year", "end_week"]] = df[
["start_year", "start_week", "end_year", "end_week"]
].astype(int)
df["course_duration"] = (
(df["end_year"] - df["start_year"]) * 52
+ (df["end_week"] - df["start_week"])
+ 1
)
df["is_double"] = df["course_duration"] > 15
def fix_period(row):
is_double = row["is_double"]
if row["start_week"] < 10:
return [3, 4] if is_double else [3]
elif row["start_week"] < 30:
return [4, 1] if is_double else [4]
elif row["start_week"] < 40:
return [1, 2] if is_double else [1]
else:
return [2, 3] if is_double else [2]
df["periods"] = df.apply(fix_period, axis=1)
# Color
def generate_color(course_code: str):
return f"#{hashlib.sha256(course_code[:2].encode()).hexdigest()[:6]}"
df["color"] = df["code"].apply(generate_color)
teacher_codes.update(df["teacher"].unique())
# Update length
program_years = df["end_year"].max() - df["start_year"].min()
with process_lock:
with open(YEARS_CSV, "a", encoding="utf-8") as f:
f.write(f"{code};{program_years}\n")
# Remove
df = df.drop_duplicates(subset=["code"], keep="first")
df = df.drop(columns=["teacher_url"])
groups = {}
for _, row in df.iterrows():
year = str(row["start_year"])
if year not in groups:
groups[year] = {}
for period in row["periods"]:
if period not in groups[year]:
groups[year][period] = []
groups[year][period].append(row["code"])
def replace_nan_with_none(obj):
if isinstance(obj, dict):
return {k: replace_nan_with_none(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [replace_nan_with_none(elem) for elem in obj]
elif isinstance(obj, float) and np.isnan(obj):
return None
return obj
data = df.set_index("code", drop=False).to_dict(orient="index")
data = replace_nan_with_none(data)
data["_groups"] = replace_nan_with_none(groups)
file_path = os.path.join(PUBLIC_PATH, f"{code}.json")
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, allow_nan=False)
print(f"Downloaded data for '{code}'")
except Exception as e:
print(f"Error downloading data for '{code}': {e}")
def download_teacher(code: str) -> None:
"""Fetches information about a teacher"""
search_url = teacher_url % code
html = asyncio.run(fetch(search_url))
soup = BeautifulSoup(html, "html.parser")
result = soup.find(attrs={"class": "Search-result"})
ul = result.find("div", attrs={"class": "SearchItem-text"})
if ul is None:
print(f"Could not find data for '{code}'")
return
ul = ul.find_all("li")
name = " ".join(reversed(result.find("h3").text.strip().split(", ")))
email = ""
room = ""
phone = ""
unit = ""
location = ""
for li in ul:
text = li.text.strip().lower()
if "e-post" in text:
email = text.split(": ")[1]
elif "rum" in text:
room = text.split(": ")[1]
elif "telefon" in text:
phone = text.split(": ")[1]
elif "enhet" in text:
unit = text.split(": ")[1]
elif "jänsteställe" in text:
location = text.split(": ")[1]
with process_lock:
with open(TEACHER_CSV, "a", encoding="utf-8") as f:
f.write(f"{code};{name};{email};{phone};{room};{unit};{location}\n")
print(f"Downloaded data for '{code}'")
async def main() -> int:
"""Main function"""
should_update = args.get("update")
should_delete = args.get("delete")
should_skip_teachers = args.get("skip_teachers")
# Check if the data exists
if should_delete:
shutil.rmtree(DATA_PATH, ignore_errors=True)
shutil.rmtree(PUBLIC_PATH, ignore_errors=True)
if not should_update and (os.path.exists(DATA_PATH) or os.path.exists(PUBLIC_PATH)):
print("Data already exists. Use the -u flag to update.")
return 0
os.makedirs(DATA_PATH, exist_ok=True)
os.makedirs(PUBLIC_PATH, exist_ok=True)
# Get a list of urls
html = await fetch(program_url)
soup = BeautifulSoup(html, "html.parser")
urls = [
program_url + re.sub(r"^\.\/", "", a["href"])
for a in soup.find_all("a", href=excel_regex)
]
n_programs = max(1, min(int(args.get("number") or len(urls)), len(urls)))
if args.get("number"):
urls = urls[:n_programs]
print(f"Downloading data for {n_programs} program(s)...")
# Download program data
with open(YEARS_CSV, "w", encoding="utf-8") as f:
f.write("program;years\n")
with ThreadPoolExecutor() as executor:
await asyncio.gather(
*[asyncio.to_thread(executor.submit, download_program, url) for url in urls]
)
with open(YEARS_CSV, "r", encoding="utf-8") as f:
df = pd.read_csv(f, delimiter=";")
df = df.drop_duplicates(subset=["program"], keep="first")
years_data = df.set_index(df.columns[0]).to_dict()[df.columns[1]]
with open(YEARS_JSON, "w", encoding="utf-8") as f:
json.dump(years_data, f, indent=4)
os.remove(YEARS_CSV)
if not should_skip_teachers:
# Download teacher data
with open(TEACHER_CSV, "w", encoding="utf-8") as f:
f.write("code;name;email;phone;room;unit;location\n")
with ProcessPoolExecutor() as executor:
await asyncio.gather(
*[
asyncio.to_thread(executor.submit, download_teacher, code)
for code in teacher_codes
]
)
with open(TEACHER_CSV, "r", encoding="utf-8") as f:
df = pd.read_csv(f, delimiter=";")
df.loc[df["name"] == "Nan Huang", "code"] = (
"nan" # Fun edge case, since the code is "nan", pandas believes it's NaN
)
df = df.drop_duplicates(subset=["code"], keep="first")
df.set_index("code", drop=False).to_json(
TEACHER_JSON, orient="index", indent=4
)
# Generate index
indexes = defaultdict(list)
for file in os.listdir(PUBLIC_PATH):
if program_regex.match(file):
code = file[:5].upper()
semester = file.split(".")[0][5:].lower()
indexes[code].append(semester)
with open(INDEX_PATH, "w", encoding="utf-8") as f:
json.dump(indexes, f, indent=4)
# Get names
names = {}
for h2 in soup.find_all("h2"):
match = re.match(r"(\w{5}) - (.+)", h2.text)
if match:
code, name = match.groups()
names[code] = name
with open(NAMES_PATH, "w", encoding="utf-8") as f:
json.dump(names, f, indent=4)
return 0
if __name__ == "__main__":
exit_code = asyncio.run(main())
os.remove(TEACHER_CSV) if os.path.exists(TEACHER_CSV) else None
sys.exit(exit_code)