UNPKG

@heknon/node-webtop

Version:

A Backend for an unofficial WebTop (SmartSchool - Israel) API.

226 lines (225 loc) 10.3 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const query_string_1 = require("query-string"); const axios_cookiejar_support_1 = __importDefault(require("axios-cookiejar-support")); const tough_cookie_1 = require("tough-cookie"); const apiConfig_1 = require("./apiConfig"); const cheerio_1 = require("cheerio"); const TimeTable_1 = require("./TimeTable/TimeTable"); const Grades_1 = require("./Grades/Grades"); const Inbox_1 = require("./Inbox/Inbox"); const Message_1 = require("./Inbox/Message"); const TestsLeft_1 = require("./TestsLeft/TestsLeft"); const TimeTableChanges_1 = require("./TimeTableChanges/TimeTableChanges"); class Client { constructor(id, password) { this.id = id; this.password = password; this._axiosInstance = null; this._cookieJar = null; this.platform = null; } /** * get the security necessary for logging in * @param {CheerioStatic} $ parsed html * @returns {WebtopSecurityData} */ _getSecurityData($) { const keys = $("#captchaWrapper").children('input[type="hidden"]')[0]; return { securityId: keys.attribs.id, securityValue: keys.attribs.value, }; } ; /** * gets platform in use * @param {CheerioStatic} $ parsed html * @returns platform axios is using */ _getPlatform($) { const platform = $('#platform').val(); return platform; } login() { return __awaiter(this, void 0, void 0, function* () { this._cookieJar = new tough_cookie_1.CookieJar(); this._axiosInstance = axios_1.default.create({ jar: this._cookieJar, withCredentials: true, headers: { 'User-Agent': 'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36' }, }); ; axios_cookiejar_support_1.default(this._axiosInstance); this._axiosInstance.defaults.jar = this._cookieJar; const html = (yield this._axiosInstance.get(apiConfig_1.baseUrl)).data; const $ = cheerio_1.load(html); const { securityId, securityValue } = this._getSecurityData($); const data = { 'action': 'login', 'rememberMe': 1, 'captcha': '', 'secondsToLogin': 23, 'username': this.id, 'password': this.password, [securityId]: securityValue }; this.platform = this._getPlatform($); const loginReq = yield this._axiosInstance.post(apiConfig_1.baseApiUrl(this.platform), query_string_1.stringify(data)); if (loginReq.data.error !== undefined) throw new Error(loginReq.data.error); return loginReq; }); } logout() { return __awaiter(this, void 0, void 0, function* () { const res = yield this._axiosInstance.post(apiConfig_1.baseApiUrl(this.platform), query_string_1.stringify({ action: 'logout' })); if (res.status === 200) { this.platform = ''; this._axiosInstance = null; this._cookieJar = null; return res; } throw new Error('Failed to logout'); }); } /** * get all the grades of all 3 years of school * @param onlyFilledGrades if only to show grades that have been filled by a teacher. if false grades that haven't been filled will be set to -1 */ getGrades(onlyFilledGrades = true) { return __awaiter(this, void 0, void 0, function* () { return Grades_1.constructGrades(this._axiosInstance, onlyFilledGrades); }); } /** * get the grades of a semester * @param semester the semester. 1 or 2. * @param studentYear the study year * @param onlyFilledGrades if only to show grades that have been filled by a teacher. if false grades that haven't been filled will be set to -1 */ getSemesterGrades(studentYear = 0, semester = 0, onlyFilledGrades = true) { return __awaiter(this, void 0, void 0, function* () { return Grades_1.constructGradeSemester(this._axiosInstance, studentYear - 1, semester, onlyFilledGrades); }); } /** * get the grades of a year * @param studentYear the study year * @param onlyFilledGrades if only to show grades that have been filled by a teacher. if false grades that haven't been filled will be set to -1 */ getYearGrades(studentYear = 0, onlyFilledGrades = true) { return __awaiter(this, void 0, void 0, function* () { return Grades_1.constructGradeYear(this._axiosInstance, studentYear - 1, onlyFilledGrades); }); } getTodayLessons() { return __awaiter(this, void 0, void 0, function* () { return (yield this._axiosInstance.get(apiConfig_1.defaultApiRoutes.todayTimetable)).data; }); } getTodayEvents() { return __awaiter(this, void 0, void 0, function* () { return (yield this._axiosInstance.get(apiConfig_1.defaultApiRoutes.todayEvents)).data; }); } getHomework() { return __awaiter(this, void 0, void 0, function* () { return (yield this._axiosInstance.get(apiConfig_1.defaultApiRoutes.homework)).data; }); } /** * @param removeEmptyHours Remove hours which there are no lessons on from the response */ getTimeTable(removeEmptyHours = false) { return __awaiter(this, void 0, void 0, function* () { const timetable = yield this._axiosInstance.get(apiConfig_1.routes.timetable); return TimeTable_1.constructTimeTable(timetable.data, removeEmptyHours); }); } getTestLeftToDo() { return __awaiter(this, void 0, void 0, function* () { const testsLeft = yield this._axiosInstance.get(apiConfig_1.routes.changesAndExams); return TestsLeft_1.constructTestsLeft(testsLeft.data); }); } getTimeTableChanges() { return __awaiter(this, void 0, void 0, function* () { const changes = yield this._axiosInstance.get(apiConfig_1.routes.changesAndExams); return TimeTableChanges_1.constructTimeTableChanges(changes.data); }); } getMainPage() { return __awaiter(this, void 0, void 0, function* () { return (yield this._axiosInstance.get(apiConfig_1.baseUrl)).data; }); } getUsers(query) { return __awaiter(this, void 0, void 0, function* () { return (yield this._axiosInstance.post(apiConfig_1.defaultApiRoutes.searchUsers, query_string_1.stringify({ value: query }))).data; }); } getInbox(page) { return __awaiter(this, void 0, void 0, function* () { return Inbox_1.constructInbox((yield this._axiosInstance.get(apiConfig_1.routes.inbox(page))).data); }); } getMessage(id) { return __awaiter(this, void 0, void 0, function* () { return Message_1.constructMessage((yield this._axiosInstance.get(apiConfig_1.routes.message(id))).data, id, this._axiosInstance); }); } changePassword(newPassword) { return __awaiter(this, void 0, void 0, function* () { return (yield this._axiosInstance.post(apiConfig_1.defaultApiRoutes.changePassword, query_string_1.stringify({ newPassword }))).data; }); } changeUsername(currentPassword, newUsername) { return __awaiter(this, void 0, void 0, function* () { return (yield this._axiosInstance.post(apiConfig_1.defaultApiRoutes.changeUsername, query_string_1.stringify({ currentPassword, newUsername }))).data; }); } changePersonalDetails(personalDetails) { return __awaiter(this, void 0, void 0, function* () { const data = personalDetails; data.emailUponUpdate = data.emailUponUpdate ? 1 : 0; data.showMyCellphone = data.showMyCellphone ? 1 : 0; data.showMyEmail = data.showMyEmail ? 1 : 0; return (yield this._axiosInstance.post(apiConfig_1.defaultApiRoutes.changeUsername, query_string_1.stringify(data))).data; }); } changePushNotifications(pushNotifications) { return __awaiter(this, void 0, void 0, function* () { return (yield this._axiosInstance.post(apiConfig_1.defaultApiRoutes.changePushNotifications, query_string_1.stringify(pushNotifications))).data; }); } getAllGradeUsers() { return __awaiter(this, void 0, void 0, function* () { const gradeUsers = []; for (let index = 1488; index < 1515; index++) { const users = yield this.getUsers(String.fromCharCode(index)); for (const user of users) { if (!gradeUsers.includes(user)) gradeUsers.push(user); } } return gradeUsers; }); } } exports.default = Client;