haifa-reminder/dist/GameSource.js

58 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-08-06 08:25:52 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
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
// This calss will be the game source.
// search for upcomming games
class GameSource {
async getGamesFromHaifa() {
const sourceUrl = `https://mhaifafc.com/games?lang=en`;
const result = await axios_1.default.get(sourceUrl);
const parsedResult = (0, node_html_parser_1.parse)(result.data.toString());
const gameBoxs = parsedResult.querySelectorAll(".game-box");
const games = [];
for (let gameBox of gameBoxs) {
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 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 = (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();
games.push({
summary: 'Maccabi Haifa F.C.',
location: headerSplit[4].trim(),
description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
start: {
dateTime: start,
timeZone: 'Asia/Jerusalem'
},
end: {
dateTime: end,
timeZone: 'Asia/Jerusalem'
}
});
}
return games;
}
getOpponentIndexByStadium(stadium) {
if (stadium === "Sammy Ofer Stadium") {
return 1;
}
else {
return 0;
}
}
}
exports.default = GameSource;