-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfetchResult.py
44 lines (40 loc) · 1.68 KB
/
fetchResult.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
import csv, requests, os, json
from tqdm import tqdm
from constant import COURSERESULT_CSV, COURSERESULT_YEARSEM
import shutil
def main():
for sem in COURSERESULT_YEARSEM:
i = 0
row_count = sum(1 for line in open("./data/" + COURSERESULT_CSV(sem), 'r'))
with open("./data/" + COURSERESULT_CSV(sem), 'r') as f:
reader = tqdm(csv.reader(f), total=row_count)
for row in reader:
courseid = str(row[0])
try:
res = requests.get("https://es.nccu.edu.tw/course/zh-TW/:sem=" + sem + "%20" + str(courseid) + "%20/").json()
result = dict({
"yearsem": sem,
"time": res[0]["subTime"],
"courseId": courseid,
"studentLimit": str(row[3]),
"studentCount": str(row[4]),
"lastEnroll": str(row[5])
})
dataPath= "./result/" + res[0]["teaNam"] + "/" + res[0]["subNam"]
if not os.path.exists(dataPath):
os.makedirs(dataPath)
if not os.path.exists(dataPath + "/courseResult"):
os.makedirs(dataPath + "/courseResult")
if not os.path.exists(dataPath + "/courseResult/" + sem + ".json"):
with open(dataPath + "/courseResult/" + sem + ".json", "w") as file:
json.dump(list(), file)
with open(dataPath + "/courseResult/" + sem + ".json", 'r') as file:
originalData = json.loads(file.read())
originalData.append(result)
with open(dataPath + "/courseResult/" + sem + ".json", "w") as file:
json.dump(originalData, file)
except BaseException as err:
print(courseid + ": ", err)
i += 1
if __name__ == "__main__":
main()