Compare commits

..

2 commits

Author SHA1 Message Date
3d8306b225 changing source of games 2024-09-21 13:25:42 +03:00
d3460f8871 logs added 2024-07-31 21:53:04 +03:00
4 changed files with 162 additions and 182 deletions

142
dist/GameSource.js vendored
View file

@ -5,100 +5,90 @@ var __importDefault = (this && this.__importDefault) || function (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
const moment_1 = __importDefault(require("moment"));
class GameSource {
async getGamesFromHaifa() {
const sourceUrl = `https://mhaifafc.com/games?lang=en`;
console.log('Trying to get games from Haifa...');
async getGamesFromHaifa(logger) {
console.log("Trying to get games from Haifa...");
try {
const result = await axios_1.default.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',
}
// Get the current date and time in the required format
const currentDate = (0, moment_1.default)().format("DD/MM/YYYY HH:mm");
// Construct the filters object with the current date
const filters = {
date: {
startDate: currentDate,
endDate: "",
},
league: "",
session: "",
gamesDirection: "1",
};
// Encode the filters for the URL
const filtersParam = encodeURIComponent(JSON.stringify(filters));
// Construct the API URL with the encoded filters
const sourceUrl = `https://api.mhaifafc.com/api/content/games-lobby?filters=${filtersParam}&start=0&limit=20&sortDirection=ASC&app=web&lang=he`;
// Get the authorization token from environment variables
const authorizationToken = process.env.HAIFA_API_AUTH_TOKEN;
// Set up the request headers
const headers = {
Accept: "*/*",
"Accept-Language": "en-US,en;q=0.7",
Authorization: `Bearer ${authorizationToken}`,
"User-Agent": "Mozilla/5.0",
Origin: "https://www.mhaifafc.com",
Referer: "https://www.mhaifafc.com/",
};
// Make the API request
const response = await axios_1.default.get(sourceUrl, {
headers,
responseType: "json",
responseEncoding: "utf8", // Ensure UTF-8 encoding
});
const parsedResult = (0, node_html_parser_1.parse)(result.data.toString());
const gameBoxs = parsedResult.querySelectorAll(".game-box");
// Extract the games data from the response
const gamesData = response.data.games.items;
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();
if (location === 'נדחה')
// Loop through each game and construct the GoogleCalendarEvent objects
for (const game of gamesData) {
const gameDetails = game.gameDetails;
const gameTime = gameDetails.gameTime; // ISO string
const isFinalGameDate = gameDetails.isFinalGameDate;
const gameLocation = gameDetails.gameLocation;
// Skip games without a game time
if (!gameTime)
continue;
if (lastGameForCalender)
break;
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 hostTeam = game.hostTeam;
const guestTeam = game.guestTeam;
// Get team names
const hostTeamName = hostTeam.teamName;
const guestTeamName = guestTeam.teamName;
const summary = `${hostTeamName} vs. ${guestTeamName}`;
// Include a note if the game date is not final
let description = `${hostTeamName} vs. ${guestTeamName}`;
if (!isFinalGameDate) {
description += " (Date and time are subject to change)";
}
// Calculate start and end times
const startDateTime = (0, moment_1.default)(gameTime).toISOString();
const endDateTime = (0, moment_1.default)(gameTime).add(2, "hours").toISOString();
// Add the event to the games array
games.push({
summary: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
location: headerSplit[headerSplit.length - 1].trim(),
description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
summary: summary,
location: gameLocation,
description: description,
start: {
dateTime: start,
dateTime: startDateTime,
timeZone: "Asia/Jerusalem",
},
end: {
dateTime: end,
dateTime: endDateTime,
timeZone: "Asia/Jerusalem",
},
});
}
// return [];
return games;
}
catch (error) {
console.error(error);
}
}
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;
}
else {
return 0;
return [];
}
}
}

5
dist/index.js vendored
View file

@ -15,12 +15,11 @@ class App {
this.googleCalendar = new GoogleCalendar_1.default();
}
async startCronJob() {
this.writeLog('START CRON JOB'); // Log when the cron job starts
console.log("START Haifa Reminder");
this.writeLog('START Haifa Reminder'); // Log when the cron job starts
const newGamesAdded = [];
await this.googleCalendar.init();
try {
const games = await this.gameSource.getGamesFromHaifa();
const games = await this.gameSource.getGamesFromHaifa(this.writeLog);
for (const game of games) {
const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent(game.start.dateTime, game.end.dateTime, game.summary);
console.log(game);

View file

@ -1,113 +1,106 @@
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
import moment from "moment";
export default class GameSource {
async getGamesFromHaifa(): Promise<GoogleCalendarEvent[]> {
const sourceUrl = `https://mhaifafc.com/games?lang=en`;
console.log('Trying to get games from Haifa...');
async getGamesFromHaifa(logger: Function): Promise<GoogleCalendarEvent[]> {
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',
}
// Get the current date and time in the required format
const currentDate = moment().format("DD/MM/YYYY HH:mm");
// Construct the filters object with the current date
const filters = {
date: {
startDate: currentDate,
endDate: "",
},
league: "",
session: "",
gamesDirection: "1",
};
// Encode the filters for the URL
const filtersParam = encodeURIComponent(JSON.stringify(filters));
// Construct the API URL with the encoded filters
const sourceUrl = `https://api.mhaifafc.com/api/content/games-lobby?filters=${filtersParam}&start=0&limit=20&sortDirection=ASC&app=web&lang=he`;
// Get the authorization token from environment variables
const authorizationToken = process.env.HAIFA_API_AUTH_TOKEN;
// Set up the request headers
const headers = {
Accept: "*/*",
"Accept-Language": "en-US,en;q=0.7",
Authorization: `Bearer ${authorizationToken}`,
"User-Agent": "Mozilla/5.0",
Origin: "https://www.mhaifafc.com",
Referer: "https://www.mhaifafc.com/",
};
// Make the API request
const response = await axios.get(sourceUrl, {
headers,
responseType: "json", // Ensure the response is parsed as JSON
responseEncoding: "utf8", // Ensure UTF-8 encoding
});
const parsedResult = parse(result.data.toString());
const gameBoxs = parsedResult.querySelectorAll(".game-box");
// Extract the games data from the response
const gamesData = response.data.games.items;
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(",");
// Loop through each game and construct the GoogleCalendarEvent objects
for (const game of gamesData) {
const gameDetails = game.gameDetails;
const gameTime = gameDetails.gameTime; // ISO string
const isFinalGameDate = gameDetails.isFinalGameDate;
const gameLocation = gameDetails.gameLocation;
// 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();
// Skip games without a game time
if (!gameTime) continue;
if (location === 'נדחה') continue;
if (lastGameForCalender) break;
const hostTeam = game.hostTeam;
const guestTeam = game.guestTeam;
const gameDate = this.findDate(headerSplit);
const gameTime = this.findTime(headerSplit);
// Get team names
const hostTeamName = hostTeam.teamName;
const guestTeamName = guestTeam.teamName;
const start = moment(
gameDate + gameTime,
"DD/MM/YYYYHH:mm"
).toISOString();
const end = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm")
.add(2, "hours")
.toISOString();
const summary = `${hostTeamName} vs. ${guestTeamName}`;
// Include a note if the game date is not final
let description = `${hostTeamName} vs. ${guestTeamName}`;
if (!isFinalGameDate) {
description += " (Date and time are subject to change)";
}
// Calculate start and end times
const startDateTime = moment(gameTime).toISOString();
const endDateTime = moment(gameTime).add(2, "hours").toISOString();
// Add the event to the games array
games.push({
summary: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
location: headerSplit[headerSplit.length - 1].trim(),
description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
summary: summary,
location: gameLocation,
description: description,
start: {
dateTime: start,
dateTime: startDateTime,
timeZone: "Asia/Jerusalem",
},
end: {
dateTime: end,
dateTime: endDateTime,
timeZone: "Asia/Jerusalem",
},
});
}
// return [];
return games;
}
catch (error) {
} 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;
return [];
}
}
}

View file

@ -17,13 +17,11 @@ class App {
}
async startCronJob() {
this.writeLog('START CRON JOB'); // Log when the cron job starts
console.log("START Haifa Reminder");
this.writeLog('START Haifa Reminder'); // Log when the cron job starts
const newGamesAdded = [];
await this.googleCalendar.init();
try {
const games = await this.gameSource.getGamesFromHaifa();
const games = await this.gameSource.getGamesFromHaifa(this.writeLog);
for (const game of games) {
const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent(
game.start.dateTime,