114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
require("dotenv").config();
|
|
import axios from "axios";
|
|
import { GoogleCalendarEvent } from "./types";
|
|
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<GoogleCalendarEvent[]> {
|
|
const sourceUrl = `https://mhaifafc.com/games?lang=en`;
|
|
console.log('Trying to get games from Haifa...');
|
|
try {
|
|
const result = await axios.get(sourceUrl,
|
|
{
|
|
headers: {
|
|
'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',
|
|
}
|
|
});
|
|
|
|
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 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();
|
|
|
|
if (location === 'נדחה') continue;
|
|
if (lastGameForCalender) break;
|
|
|
|
const gameDate = this.findDate(headerSplit);
|
|
const gameTime = this.findTime(headerSplit);
|
|
|
|
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: `${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 [];
|
|
return games;
|
|
}
|
|
catch (error) {
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
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;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
}
|