haifa-reminder/dist/GameSource.js

106 lines
4.2 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();
2023-08-06 08:25:52 +00:00
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 {
2024-07-31 18:53:04 +00:00
async getGamesFromHaifa(logger) {
2024-02-04 11:00:14 +00:00
const sourceUrl = `https://mhaifafc.com/games?lang=en`;
2024-02-04 10:56:26 +00:00
console.log('Trying to get games from Haifa...');
try {
const result = await axios_1.default.get(sourceUrl, {
headers: {
2024-02-04 11:10:12 +00:00
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
}
});
2024-02-04 10:56:26 +00:00
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;
const location = headerSplit[headerSplit.length - 1].trim();
2024-07-31 18:53:04 +00:00
const gameDate = this.findDate(headerSplit);
const gameTime = this.findTime(headerSplit);
2024-02-04 10:56:26 +00:00
if (location === 'נדחה')
continue;
2024-07-31 18:53:04 +00:00
if (lastGameForCalender && gameDate !== '')
2024-02-04 10:56:26 +00:00
break;
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: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
location: headerSplit[headerSplit.length - 1].trim(),
description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
start: {
dateTime: start,
timeZone: "Asia/Jerusalem",
},
end: {
dateTime: end,
timeZone: "Asia/Jerusalem",
},
});
}
return games;
}
catch (error) {
console.error(error);
2023-08-06 08:25:52 +00:00
}
}
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;
}
});
2024-07-31 18:53:04 +00:00
// console.log("Cheking date: ", date, "By split: ", headerSplit)
return date.trim();
}
2023-08-06 08:25:52 +00:00
getOpponentIndexByStadium(stadium) {
if (stadium === "Sammy Ofer Stadium") {
return 1;
}
else {
return 0;
}
}
}
exports.default = GameSource;