UNPKG

parse-comcigan

Version:

컴시간 알리미에서 시간표를 가져오는 라이브러리입니다.

140 lines 6.09 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import ComciganError from "./error.js"; import axios from "axios"; import iconv from "iconv-lite"; import dayjs from "dayjs"; axios.defaults.validateStatus = () => true; function splitData(dat) { const d = String(dat).match(/\d{1,3}(?=(\d{3})+(?!\d))|\d{1,3}$/g); if (!d) return -1; return d.reverse().map(Number); } export default class Comcigan { constructor(code) { this.code = code; this._data = []; } /** * 학교를 검색합니다. * * @param query 검색어 * @returns 검색 결과 */ static search(query) { return __awaiter(this, void 0, void 0, function* () { const euckr = Array.from(iconv.encode(query, "euc-kr")) .map(byte => '%' + byte.toString(16).toUpperCase().padStart(2, '0')) .join(''); const r = yield axios.get("http://comci.net:4082/36179?17384l" + euckr); const obj = JSON.parse(r.data.substring(0, r.data.lastIndexOf("}") + 1)); return obj.학교검색 .filter(v => v[3] !== 0) .map(v => ({ "code": v[3], "region": v[1], "name": v[2] })); }); } _getdata(index) { return __awaiter(this, void 0, void 0, function* () { const idx = index !== null && index !== void 0 ? index : 1; if (this._data[idx]) return this._data[idx]; const route = btoa(`73629_${this.code}_0_${idx}`); const r = yield axios.get(`http://comci.net:4082/36179?${route}`); if (r.data.startsWith("{}")) throw new ComciganError(`학교 정보를 가져오는데 실패했어요. 학교 코드가 올바른지 확인해주세요.`); const obj = JSON.parse(r.data.substring(0, r.data.lastIndexOf("}") + 1)); this._data[idx] = obj; }); } /** * 학교의 정보를 가져옵니다. * * @returns 학교의 정보 */ schoolInfo() { return __awaiter(this, void 0, void 0, function* () { yield this._getdata(); const data = this._data[1]; return { "changedAt": dayjs(data.자료244, "YYYY-MM-DD HH:mm:ss").toDate(), "classes": data.학급수.slice(1), "times": data.일과시간.map(v => ({ "number": Number(v.slice(0, 1)), "start": v.slice(2, 7), })), "code": this.code }; }); } /** * 시간표를 조회합니다. * * @param options 시간표 옵션 * @returns 시간표 */ timetable(options) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const { grade, classNum, week } = options; if (!grade || !classNum) throw new ComciganError("학년 (grade), 반 (classNum)을 제공해주세요."); yield this._getdata(week); const data = this._data[week !== null && week !== void 0 ? week : 1]; const toReturn = []; // 자료481: 현재 데이터 // 자료147: 원본 데이터 // 자료446: 선생님 // 자료492: 과목 for (let day = 1; day <= 5; day++) { const dayItems = []; for (let time = 1; time <= 8; time++) { const now = splitData(data.자료481[grade][classNum][day][time]); if (now === -1) continue; const subject = data.자료492[now[1]]; const teacher = data.자료446[now[0]]; const classRoom = data.자료245[grade][classNum][day][time]; const classRoomAvailable = classRoom !== undefined && classRoom !== ""; const origin = splitData((_d = (_c = (_b = (_a = data.자료147) === null || _a === void 0 ? void 0 : _a[grade]) === null || _b === void 0 ? void 0 : _b[classNum]) === null || _c === void 0 ? void 0 : _c[day]) === null || _d === void 0 ? void 0 : _d[time]); if (origin !== -1) { const newSubject = data.자료492[origin[1]]; const newTeacher = data.자료446[origin[0]]; dayItems.push({ "subject": newSubject, "teacher": newTeacher, "classRoom": classRoomAvailable ? classRoom.split("_")[1] : undefined, "original": newSubject === subject && newTeacher === teacher ? undefined : { "subject": subject, "teacher": teacher, } }); continue; } dayItems.push({ subject, teacher, "classRoom": classRoomAvailable ? classRoom : undefined }); } toReturn[day - 1] = { "total": dayItems.length, "items": dayItems }; } return toReturn; }); } } //# sourceMappingURL=class.js.map