diff --git a/dist/GameSource.js b/dist/GameSource.js index 18fc61e..d375b3c 100644 --- a/dist/GameSource.js +++ b/dist/GameSource.js @@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -require('dotenv').config(); +require("dotenv").config(); const axios_1 = __importDefault(require("axios")); const node_html_parser_1 = require("node-html-parser"); const moment_1 = __importDefault(require("moment")); // require @@ -17,34 +17,62 @@ class GameSource { const gameBoxs = parsedResult.querySelectorAll(".game-box"); const games = []; for (let gameBox of gameBoxs) { - const teamsPlaying = gameBox.querySelectorAll(".team-name").map((team) => team.text); + const teamsPlaying = gameBox + .querySelectorAll(".team-name") + .map((team) => team.text); const regex = /[\r\n\s]+/g; - const gameHeader = gameBox.querySelector(".game-header").text.replace(regex, " ").trim(); + const gameHeader = gameBox + .querySelector(".game-header") + .text.replace(regex, " ") + .trim(); const headerSplit = gameHeader.split(","); // In data, if there is no time, it means it's the last game for the calender const lastGameForCalender = headerSplit.length < 4; if (lastGameForCalender) break; - const gameDate = headerSplit[2].trim(); - const gameTime = headerSplit[3].trim(); + const gameDate = this.findDate(headerSplit); + const gameTime = this.findTime(headerSplit); const start = (0, moment_1.default)(gameDate + gameTime, "DD/MM/YYYYHH:mm").toISOString(); - const end = (0, moment_1.default)(gameDate + gameTime, "DD/MM/YYYYHH:mm").add(2, "hours").toISOString(); + const end = (0, moment_1.default)(gameDate + gameTime, "DD/MM/YYYYHH:mm") + .add(2, "hours") + .toISOString(); games.push({ - summary: 'Maccabi Haifa F.C.', - location: headerSplit[4].trim(), + summary: "Maccabi Haifa F.C.", + location: headerSplit[headerSplit.length - 1].trim(), description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`, start: { dateTime: start, - timeZone: 'Asia/Jerusalem' + timeZone: "Asia/Jerusalem", }, end: { dateTime: end, - timeZone: 'Asia/Jerusalem' - } + timeZone: "Asia/Jerusalem", + }, }); } return games; } + findTime(headerSplit) { + let time = ''; + headerSplit.forEach((item) => { + if (/\d{2}:\d{2}/.test(item)) { + time = item; + return; + } + }); + return time.trim(); + } + findDate(headerSplit) { + // if it's a date format, return it like: 19/08/2023 + let date = ''; + headerSplit.forEach((item) => { + if (/\d{2}\/\d{2}\/\d{4}/.test(item)) { + date = item; + return; + } + }); + return date.trim(); + } getOpponentIndexByStadium(stadium) { if (stadium === "Sammy Ofer Stadium") { return 1; diff --git a/dist/index.js b/dist/index.js index 840538f..c139836 100644 --- a/dist/index.js +++ b/dist/index.js @@ -28,6 +28,8 @@ class App { } else { console.log("Event already exists"); + console.log("Event:"); + console.log("Happening at:"); } } if (newGamesAdded.length > 0) { diff --git a/package-lock.json b/package-lock.json index 161f46f..ba2d4aa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,12 +6,10 @@ "": { "dependencies": { "axios": "^1.3.4", - "cron": "^2.3.0", "dotenv": "^16.0.3", "googleapis": "^122.0.0", "moment": "^2.29.4", "moment-timezone": "^0.5.43", - "node-cron": "^3.0.2", "node-html-parser": "^6.1.5", "uuid": "^9.0.0" }, @@ -191,14 +189,6 @@ "node": ">= 0.8" } }, - "node_modules/cron": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/cron/-/cron-2.3.0.tgz", - "integrity": "sha512-ZN5HP8zDY41sJolMsbc+GksRATcbvkPKF5wR/qc8FrV4NBVi9ORQa1HmYa5GydaysUB80X9XpRlRkooa5uEtTA==", - "dependencies": { - "luxon": "^3.2.1" - } - }, "node_modules/css-select": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/css-select/-/css-select-5.1.0.tgz", @@ -578,14 +568,6 @@ "node": ">=10" } }, - "node_modules/luxon": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.3.0.tgz", - "integrity": "sha512-An0UCfG/rSiqtAIiBPO0Y9/zAnHUZxAMiCpTd5h2smgsj7GGmcenvrvww2cqNA8/4A5ZrD1gJpHN2mIHZQF+Mg==", - "engines": { - "node": ">=12" - } - }, "node_modules/mime-db": { "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", @@ -629,25 +611,6 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, - "node_modules/node-cron": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", - "integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", - "dependencies": { - "uuid": "8.3.2" - }, - "engines": { - "node": ">=6.0.0" - } - }, - "node_modules/node-cron/node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "bin": { - "uuid": "dist/bin/uuid" - } - }, "node_modules/node-fetch": { "version": "2.6.12", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz", diff --git a/package.json b/package.json index 2d9fd9c..598e856 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,10 @@ { "dependencies": { "axios": "^1.3.4", - "cron": "^2.3.0", "dotenv": "^16.0.3", "googleapis": "^122.0.0", "moment": "^2.29.4", "moment-timezone": "^0.5.43", - "node-cron": "^3.0.2", "node-html-parser": "^6.1.5", "uuid": "^9.0.0" }, diff --git a/src/GameSource.ts b/src/GameSource.ts index ecfddc3..11384a8 100644 --- a/src/GameSource.ts +++ b/src/GameSource.ts @@ -1,57 +1,87 @@ -require('dotenv').config(); +require("dotenv").config(); import axios from "axios"; import { GoogleCalendarEvent } from "./types"; -import { parse } from 'node-html-parser'; -import moment from 'moment'; // require +import { parse } from "node-html-parser"; +import moment from "moment"; // require // This calss will be the game source. // search for upcomming games export default class GameSource { - async getGamesFromHaifa(): Promise { const sourceUrl = `https://mhaifafc.com/games?lang=en`; const result = await axios.get(sourceUrl); - const parsedResult = parse(result.data.toString()) + const parsedResult = parse(result.data.toString()); const gameBoxs = parsedResult.querySelectorAll(".game-box"); const games: GoogleCalendarEvent[] = []; for (let gameBox of gameBoxs) { - - const teamsPlaying = gameBox.querySelectorAll(".team-name").map((team: any) => team.text); + const teamsPlaying = gameBox + .querySelectorAll(".team-name") + .map((team: any) => team.text); const regex = /[\r\n\s]+/g; - const gameHeader = gameBox.querySelector(".game-header").text.replace(regex, " ").trim(); + const gameHeader = gameBox + .querySelector(".game-header") + .text.replace(regex, " ") + .trim(); const headerSplit = gameHeader.split(","); + // In data, if there is no time, it means it's the last game for the calender const lastGameForCalender = headerSplit.length < 4; + if (lastGameForCalender) break; - - const gameDate = headerSplit[2].trim(); - const gameTime = headerSplit[3].trim(); - - const start = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm").toISOString(); - const end = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm").add(2, "hours").toISOString(); + const gameDate = this.findDate(headerSplit); + const gameTime = this.findTime(headerSplit); - - games.push({ - summary: 'Maccabi Haifa F.C.', - location: headerSplit[4].trim(), + const start = moment( + gameDate + gameTime, + "DD/MM/YYYYHH:mm" + ).toISOString(); + const end = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm") + .add(2, "hours") + .toISOString(); + + games.push({ + summary: "Maccabi Haifa F.C.", + location: headerSplit[headerSplit.length - 1].trim(), description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`, start: { dateTime: start, - timeZone: 'Asia/Jerusalem' - }, + timeZone: "Asia/Jerusalem", + }, end: { dateTime: end, - timeZone: 'Asia/Jerusalem' - } - }) + timeZone: "Asia/Jerusalem", + }, + }); } return games; } + private findTime(headerSplit: string[]) { + let time = ''; + headerSplit.forEach((item) => { + if (/\d{2}:\d{2}/.test(item)) { + time = item; + return + } + }); + return time.trim(); + } + private findDate(headerSplit: string[]) { + // if it's a date format, return it like: 19/08/2023 + let date = ''; + headerSplit.forEach((item) => { + if(/\d{2}\/\d{2}\/\d{4}/.test(item)) { + date = item; + return; + } + }) + return date.trim(); + } + getOpponentIndexByStadium(stadium: string) { if (stadium === "Sammy Ofer Stadium") { return 1; @@ -59,4 +89,4 @@ export default class GameSource { return 0; } } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index 1688496..484c2ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ import GameSource from "./GameSource"; import GoogleCalendar from "./GoogleCalendar"; import env from "dotenv"; -import cron from "node-cron"; env.config(); @@ -19,7 +18,6 @@ class App { console.log("START CRON JOB"); const newGamesAdded = []; await this.googleCalendar.init(); - // Schedule the job to run daily at a specific time (e.g., 1:00 AM) try { const games = await app.gameSource.getGamesFromHaifa();