Skip to content

Commit d847849

Browse files
authored
Merge pull request #36 from ut-code/class-data-format
旧シ楽バス用の講義情報.jsonを新シ楽バス用へ変換するスクリプトを作りました。
2 parents 554bc3b + eba4ff4 commit d847849

File tree

11 files changed

+128945
-0
lines changed

11 files changed

+128945
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ bun.lockb
1818

1919
# production
2020
/build
21+
/class_data/build
2122

2223
# misc
2324
.DS_Store

class_data/data/new/2024A.json

Lines changed: 68460 additions & 0 deletions
Large diffs are not rendered by default.

class_data/data/old/2024A.json

Lines changed: 60240 additions & 0 deletions
Large diffs are not rendered by default.

class_data/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { PreviousClassData } from "./previousClassDaya";
2+
import fs from "fs";
3+
import { argv } from "node:process";
4+
import { NewClassData } from "./newClass";
5+
6+
/**
7+
* 変換処理本体
8+
*/
9+
function main() {
10+
/************ コマンドライン引数のチェック ************/
11+
if (argv.length != 4) {
12+
throw "パラメーター数が異常です。\n例: npm run start -- 2024 A\n";
13+
}
14+
15+
if (argv[2].length < 4) {
16+
throw "年度は西暦で表現してください。\n例: npm run start -- 2024 A\n";
17+
}
18+
19+
if (argv[3] != "S" && argv[3] != "A") {
20+
throw "セメスターはSまたはAで表現してください。\n例: npm run start -- 2024 A\n";
21+
}
22+
const semester = argv[3];
23+
const year = Number(argv[2]);
24+
if (Number.isNaN(year)) {
25+
throw "年度は数字で表現してください。\n例: npm run start -- 2024 A\n";
26+
}
27+
/****************************************************/
28+
29+
// 入力ファイルのパス
30+
const inputFilepath = `./data/old/${year}${semester}.json`;
31+
32+
// 出力ファイルのパス
33+
const outputFilepath = `./data/new/${year}${semester}.json`;
34+
35+
// ファイルの読み取り
36+
if (!fs.existsSync(inputFilepath)) {
37+
throw "ファイルが見つかりませんでした。\n" + inputFilepath + "\n";
38+
}
39+
const inputData: string = fs.readFileSync(inputFilepath, "utf8");
40+
41+
// PreviousClassTypeの配列にパース
42+
const prevClassData: PreviousClassData[] = JSON.parse(inputData);
43+
44+
// データを変換
45+
const converted = prevClassData.map((c) => new NewClassData(c));
46+
47+
// ファイル出力
48+
fs.writeFileSync(outputFilepath, JSON.stringify(converted, null, 2));
49+
}
50+
51+
main();

class_data/newClass.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { PreviousClassData } from "./previousClassDaya";
2+
import { dayMappingToEn, DayPeriod } from "./type";
3+
4+
/**
5+
* 新シ楽バス仕様の講義情報クラス
6+
* 構造はほとんど同様なのでPreviousClassTypeを継承する
7+
*/
8+
export class NewClassData extends PreviousClassData {
9+
/**
10+
* 旧シ楽バスのデータをもとにインスタンス化する
11+
* @param prevClass 旧シ楽バスのデータ
12+
*/
13+
constructor(prevClass: PreviousClassData) {
14+
super();
15+
/********* プロパティのコピー *********/
16+
Object.assign(this, prevClass);
17+
18+
/********* 新仕様特有のプロパティ *********/
19+
if (!prevClass.periods) throw "入力データのperiodsがundefinedです";
20+
this.dayPeriod = this.toDayPeriod(prevClass.periods);
21+
this.periods = undefined;
22+
23+
if (!prevClass.lecturerJp) throw "入力データのlecturerJpがundefinedです";
24+
this.lecturer = prevClass.lecturerJp;
25+
this.lecturerJp = undefined;
26+
}
27+
28+
/**
29+
* 現行シ楽バスの曜限表現を新シラバス仕様に変換する
30+
* @param dayPeriodStrs 旧シラバスの曜限表現
31+
* @returns 新シラバスの曜限表現
32+
*/
33+
private toDayPeriod(dayPeriodStrs: string[]): DayPeriod[] | "集中" {
34+
// 集中講義ならそのまま"集中"を返す
35+
if (dayPeriodStrs.indexOf("集中") != -1) return "集中";
36+
37+
// 授業は複数コマ開講されることがあるため、
38+
// 各曜限を新シラバス仕様に変換
39+
return dayPeriodStrs.map((dayStr) => {
40+
// dayStrは必ず2文字
41+
const day: string = dayStr[0];
42+
const period = Number(dayStr[1]);
43+
44+
// 新フォーマットを返す
45+
return {
46+
day: dayMappingToEn[day],
47+
period: period,
48+
};
49+
});
50+
}
51+
52+
/********* 新仕様特有のプロパティ *********/
53+
54+
/**
55+
* 開講曜限
56+
*/
57+
dayPeriod: DayPeriod[] | "集中";
58+
59+
/**
60+
* 教授名(Ja)
61+
*/
62+
lecturer: string;
63+
}

class_data/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "class_data",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "data_converter.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "tsc -p . && node ./build/"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC"
13+
}

class_data/previousClassDaya.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 現行のシ楽バス(v1)のスクレイピング結果(json)の型
3+
* 情報のフィールドを並べているだけ
4+
*/
5+
export class PreviousClassData {
6+
code?: string;
7+
type?: "基礎" | "総合" | "要求" | "主題" | "展開";
8+
category?: string;
9+
semester?: "S" | "S1" | "S2" | "A" | "A1" | "A2";
10+
periods?: string[];
11+
classroom?: string;
12+
titleJp?: string;
13+
lecturerJp?: string;
14+
titleEn?: string;
15+
lecturerEn?: string;
16+
ccCode?: string;
17+
credits?: number;
18+
detail?: string;
19+
schedule?: string;
20+
methods?: string;
21+
evaluation?: string;
22+
notes?: string;
23+
class?: string;
24+
guidance?: string;
25+
guidanceDate?: string;
26+
guidancePeriod?: string;
27+
guidancePlace?: string;
28+
time?: number;
29+
timeCompensation?: string;
30+
targetClass?: string[][];
31+
importance?: ("l1" | "l2" | "l3" | "s1" | "s2" | "s3")[][];
32+
shortenedCategory?:
33+
| "基礎"
34+
| "要求"
35+
| "主題"
36+
| "展開"
37+
| "総合L"
38+
| "総合A"
39+
| "総合B"
40+
| "総合C"
41+
| "総合D"
42+
| "総合E"
43+
| "総合F";
44+
shortenedEvaluation?: string;
45+
shortenedClassroom?: string;
46+
}

class_data/readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## 概要
2+
3+
現行のシ楽バスのためのスクレイピング結果を、シ楽バスnextで利用できる型に変換するスクリプトです。
4+
5+
## 利用方法
6+
7+
1. class_data ディレクトリに移動します。
8+
1. 変換元のファイル名を`yyyyX.json`(`yyyy`:年度、`X`:セメスター)に変更し(例:2024A.json)、data/old/ に配置します。
9+
1. `npm run start -- yyyy X` (`yyyy`:年度、`X`:セメスター)
10+
1. data/new/ に同名のjsonファイルが生成されます。

class_data/tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2016",
4+
"module": "commonjs",
5+
"resolveJsonModule": true,
6+
"esModuleInterop": true,
7+
"forceConsistentCasingInFileNames": true,
8+
"strict": true,
9+
"skipLibCheck": true,
10+
"outDir": "build",
11+
"rootDir": "./"
12+
}
13+
}

class_data/type.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 新シ楽バスで利用する曜限(集中以外)の型
3+
*/
4+
export type DayPeriod = {
5+
day: Day;
6+
period: number;
7+
};
8+
9+
/**
10+
* 曜日の列挙
11+
*/
12+
export type Day = "mon" | "tue" | "wed" | "thu" | "fri" | "sat";
13+
14+
/**
15+
* 曜日の、日本語から英語への変換
16+
*/
17+
export const dayMappingToEn: { [key in string]: Day } = {
18+
: "mon",
19+
: "tue",
20+
: "wed",
21+
: "thu",
22+
: "fri",
23+
: "sat",
24+
};

0 commit comments

Comments
 (0)