diff --git a/cron-runner.js b/cron-runner.js index 9e0d5fb..d1e22a5 100644 --- a/cron-runner.js +++ b/cron-runner.js @@ -1,6 +1,6 @@ -const cron = require('node-cron'); -const { exec } = require('child_process'); -const { scheduleRules, toMinutes } = require('./scheduleRules'); +import cron from 'node-cron'; +import { exec } from 'child_process'; +import { scheduleRules, toMinutes } from './scheduleRules.js'; function runScraper() { console.log(`Running scraper at ${new Date().toLocaleString()}...`); diff --git a/package-lock.json b/package-lock.json index ccc0664..e3c3004 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,6 @@ "concurrently": "^9.2.0", "express": "^5.1.0", "node-cron": "^4.2.1", - "node-fetch": "^3.3.2", "puppeteer": "^24.10.0", "xlsx": "^0.18.5" } @@ -1019,29 +1018,6 @@ "pend": "~1.2.0" } }, - "node_modules/fetch-blob": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", - "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/jimmywarting" - }, - { - "type": "paypal", - "url": "https://paypal.me/jimmywarting" - } - ], - "license": "MIT", - "dependencies": { - "node-domexception": "^1.0.0", - "web-streams-polyfill": "^3.0.3" - }, - "engines": { - "node": "^12.20 || >= 14.13" - } - }, "node_modules/finalhandler": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-2.1.0.tgz", @@ -1059,18 +1035,6 @@ "node": ">= 0.8" } }, - "node_modules/formdata-polyfill": { - "version": "4.0.10", - "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", - "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", - "license": "MIT", - "dependencies": { - "fetch-blob": "^3.1.2" - }, - "engines": { - "node": ">=12.20.0" - } - }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -1506,53 +1470,6 @@ "node": ">=6.0.0" } }, - "node_modules/node-domexception": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", - "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", - "deprecated": "Use your platform's native DOMException instead", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/jimmywarting" - }, - { - "type": "github", - "url": "https://paypal.me/jimmywarting" - } - ], - "license": "MIT", - "engines": { - "node": ">=10.5.0" - } - }, - "node_modules/node-fetch": { - "version": "3.3.2", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", - "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", - "license": "MIT", - "dependencies": { - "data-uri-to-buffer": "^4.0.0", - "fetch-blob": "^3.1.4", - "formdata-polyfill": "^4.0.10" - }, - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/node-fetch" - } - }, - "node_modules/node-fetch/node_modules/data-uri-to-buffer": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", - "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", - "license": "MIT", - "engines": { - "node": ">= 12" - } - }, "node_modules/nth-check": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-2.1.1.tgz", @@ -2364,15 +2281,6 @@ "node": ">= 0.8" } }, - "node_modules/web-streams-polyfill": { - "version": "3.3.3", - "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", - "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", - "license": "MIT", - "engines": { - "node": ">= 8" - } - }, "node_modules/whatwg-encoding": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-3.1.1.tgz", diff --git a/package.json b/package.json index 3422943..56af699 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "license": "ISC", "author": "Jakub Žitník", - "type": "commonjs", + "type": "module", "main": "server.js", "scripts": { "start": "concurrently \"node server.js\" \"node cron-runner.js\"" @@ -14,7 +14,6 @@ "concurrently": "^9.2.0", "express": "^5.1.0", "node-cron": "^4.2.1", - "node-fetch": "^3.3.2", "puppeteer": "^24.10.0", "xlsx": "^0.18.5" } diff --git a/scheduleRules.js b/scheduleRules.js index b8219c8..eb247f0 100644 --- a/scheduleRules.js +++ b/scheduleRules.js @@ -1,5 +1,5 @@ // Rules: start and end in 24h format, interval in minutes -const scheduleRules = [ +export const scheduleRules = [ { start: "0:00", end: "3:00", interval: 180 }, { start: "3:00", end: "4:00", interval: 60 }, { start: "5:00", end: "6:00", interval: 30 }, @@ -9,12 +9,12 @@ const scheduleRules = [ { start: "19:00", end: "0:00", interval: 180 } ]; -function toMinutes(timeStr) { +export function toMinutes(timeStr) { const [h, m] = timeStr.split(":").map(Number); return h * 60 + (m || 0); } -function getCurrentInterval(date = new Date()) { +export function getCurrentInterval(date = new Date()) { const nowMinutes = date.getHours() * 60 + date.getMinutes(); for (const rule of scheduleRules) { @@ -35,9 +35,3 @@ function getCurrentInterval(date = new Date()) { return null; } - -module.exports = { - scheduleRules, - getCurrentInterval, - toMinutes -}; diff --git a/scrape/parse.js b/scrape/parse.js index 3a9b46b..691e550 100644 --- a/scrape/parse.js +++ b/scrape/parse.js @@ -1,9 +1,9 @@ -const XLSX = require('xlsx'); -const fs = require("fs"); -const parseAbsence = require('./utils/parseAbsence'); -const parseTeachers = require('./utils/parseTeachers'); +import XLSX from "xlsx"; +import fs from "fs"; +import parseAbsence from "./utils/parseAbsence.js"; +import parseTeachers from "./utils/parseTeachers.js"; -async function parseThisShit(downloadedFilePath) { +export default async function parseThisShit(downloadedFilePath) { const workbook = XLSX.readFile(downloadedFilePath); const sheetNames = workbook.SheetNames; @@ -138,6 +138,4 @@ async function parseThisShit(downloadedFilePath) { fs.writeFileSync('db/current.json', JSON.stringify(data)); } -module.exports = parseThisShit; - // parseThisShit("downloads/table.xlsx"); diff --git a/scrape/scraper.js b/scrape/scraper.js index 6ba6843..2259568 100644 --- a/scrape/scraper.js +++ b/scrape/scraper.js @@ -1,7 +1,7 @@ -const puppeteer = require('puppeteer'); -const path = require('path'); -const fs = require('fs'); -const parseThisShit = require('./parse'); +import puppeteer from 'puppeteer'; +import path from 'path'; +import fs from 'fs'; +import parseThisShit from './parse.js'; const EMAIL = process.env.EMAIL; const PASSWORD = process.env.PASSWORD; diff --git a/scrape/utils/parseAbsence.js b/scrape/utils/parseAbsence.js index 43a5f32..3cc80de 100644 --- a/scrape/utils/parseAbsence.js +++ b/scrape/utils/parseAbsence.js @@ -1,4 +1,4 @@ -function parseAbsence(str, teacherMap) { +export default function parseAbsence(str, teacherMap) { str = str.trim().replace(/\s+/g, " "); const result = { @@ -40,5 +40,3 @@ function parseAbsence(str, teacherMap) { return result; } - -module.exports = parseAbsence; diff --git a/scrape/utils/parseTeachers.js b/scrape/utils/parseTeachers.js index a061815..130021f 100644 --- a/scrape/utils/parseTeachers.js +++ b/scrape/utils/parseTeachers.js @@ -1,9 +1,8 @@ -const cheerio = require("cheerio"); -const fetch = require("node-fetch"); +import * as cheerio from "cheerio"; globalThis.File = class File {}; -async function parseTeachers() { +export default async function parseTeachers() { const url = "https://spsejecna.cz/ucitel"; const response = await fetch(url); const data = await response.text(); // fetch needs .text() to get HTML @@ -24,5 +23,3 @@ async function parseTeachers() { return map; } - -module.exports = parseTeachers; diff --git a/server.js b/server.js index f743761..f2a1ea1 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,8 @@ -const express = require("express"); -const path = require("path"); +import express from "express"; +import path from "path"; const app = express(); -const fs = require("fs/promises"); -const { getCurrentInterval } = require("./scheduleRules"); +import fs from "fs/promises"; +import { getCurrentInterval } from "./scheduleRules.js"; const PORT = process.env.PORT || 3000;