Compare commits
2 commits
15932a0c9d
...
3d8306b225
Author | SHA1 | Date | |
---|---|---|---|
3d8306b225 | |||
d3460f8871 |
4 changed files with 162 additions and 182 deletions
142
dist/GameSource.js
vendored
142
dist/GameSource.js
vendored
|
@ -5,100 +5,90 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
const axios_1 = __importDefault(require("axios"));
|
const axios_1 = __importDefault(require("axios"));
|
||||||
const node_html_parser_1 = require("node-html-parser");
|
const moment_1 = __importDefault(require("moment"));
|
||||||
const moment_1 = __importDefault(require("moment")); // require
|
|
||||||
// This calss will be the game source.
|
|
||||||
// search for upcomming games
|
|
||||||
class GameSource {
|
class GameSource {
|
||||||
async getGamesFromHaifa() {
|
async getGamesFromHaifa(logger) {
|
||||||
const sourceUrl = `https://mhaifafc.com/games?lang=en`;
|
console.log("Trying to get games from Haifa...");
|
||||||
console.log('Trying to get games from Haifa...');
|
|
||||||
try {
|
try {
|
||||||
const result = await axios_1.default.get(sourceUrl, {
|
// Get the current date and time in the required format
|
||||||
headers: {
|
const currentDate = (0, moment_1.default)().format("DD/MM/YYYY HH:mm");
|
||||||
'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',
|
// Construct the filters object with the current date
|
||||||
'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',
|
const filters = {
|
||||||
'Accept-Language': 'en-US,en;q=0.9',
|
date: {
|
||||||
'Accept-Encoding': 'gzip, deflate, br',
|
startDate: currentDate,
|
||||||
'DNT': '1',
|
endDate: "",
|
||||||
'Connection': 'keep-alive',
|
},
|
||||||
'Upgrade-Insecure-Requests': '1',
|
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());
|
// Extract the games data from the response
|
||||||
const gameBoxs = parsedResult.querySelectorAll(".game-box");
|
const gamesData = response.data.games.items;
|
||||||
const games = [];
|
const games = [];
|
||||||
for (let gameBox of gameBoxs) {
|
// Loop through each game and construct the GoogleCalendarEvent objects
|
||||||
const teamsPlaying = gameBox
|
for (const game of gamesData) {
|
||||||
.querySelectorAll(".team-name")
|
const gameDetails = game.gameDetails;
|
||||||
.map((team) => team.text);
|
const gameTime = gameDetails.gameTime; // ISO string
|
||||||
const regex = /[\r\n\s]+/g;
|
const isFinalGameDate = gameDetails.isFinalGameDate;
|
||||||
const gameHeader = gameBox
|
const gameLocation = gameDetails.gameLocation;
|
||||||
.querySelector(".game-header")
|
// Skip games without a game time
|
||||||
.text.replace(regex, " ")
|
if (!gameTime)
|
||||||
.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;
|
continue;
|
||||||
if (lastGameForCalender)
|
const hostTeam = game.hostTeam;
|
||||||
break;
|
const guestTeam = game.guestTeam;
|
||||||
const gameDate = this.findDate(headerSplit);
|
// Get team names
|
||||||
const gameTime = this.findTime(headerSplit);
|
const hostTeamName = hostTeam.teamName;
|
||||||
const start = (0, moment_1.default)(gameDate + gameTime, "DD/MM/YYYYHH:mm").toISOString();
|
const guestTeamName = guestTeam.teamName;
|
||||||
const end = (0, moment_1.default)(gameDate + gameTime, "DD/MM/YYYYHH:mm")
|
const summary = `${hostTeamName} vs. ${guestTeamName}`;
|
||||||
.add(2, "hours")
|
// Include a note if the game date is not final
|
||||||
.toISOString();
|
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({
|
games.push({
|
||||||
summary: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
|
summary: summary,
|
||||||
location: headerSplit[headerSplit.length - 1].trim(),
|
location: gameLocation,
|
||||||
description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
|
description: description,
|
||||||
start: {
|
start: {
|
||||||
dateTime: start,
|
dateTime: startDateTime,
|
||||||
timeZone: "Asia/Jerusalem",
|
timeZone: "Asia/Jerusalem",
|
||||||
},
|
},
|
||||||
end: {
|
end: {
|
||||||
dateTime: end,
|
dateTime: endDateTime,
|
||||||
timeZone: "Asia/Jerusalem",
|
timeZone: "Asia/Jerusalem",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// return [];
|
|
||||||
return games;
|
return games;
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
return [];
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
5
dist/index.js
vendored
5
dist/index.js
vendored
|
@ -15,12 +15,11 @@ class App {
|
||||||
this.googleCalendar = new GoogleCalendar_1.default();
|
this.googleCalendar = new GoogleCalendar_1.default();
|
||||||
}
|
}
|
||||||
async startCronJob() {
|
async startCronJob() {
|
||||||
this.writeLog('START CRON JOB'); // Log when the cron job starts
|
this.writeLog('START Haifa Reminder'); // Log when the cron job starts
|
||||||
console.log("START Haifa Reminder");
|
|
||||||
const newGamesAdded = [];
|
const newGamesAdded = [];
|
||||||
await this.googleCalendar.init();
|
await this.googleCalendar.init();
|
||||||
try {
|
try {
|
||||||
const games = await this.gameSource.getGamesFromHaifa();
|
const games = await this.gameSource.getGamesFromHaifa(this.writeLog);
|
||||||
for (const game of games) {
|
for (const game of games) {
|
||||||
const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent(game.start.dateTime, game.end.dateTime, game.summary);
|
const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent(game.start.dateTime, game.end.dateTime, game.summary);
|
||||||
console.log(game);
|
console.log(game);
|
||||||
|
|
|
@ -1,113 +1,106 @@
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { GoogleCalendarEvent } from "./types";
|
import { GoogleCalendarEvent } from "./types";
|
||||||
import { parse } from "node-html-parser";
|
import moment from "moment";
|
||||||
import moment from "moment"; // require
|
|
||||||
|
|
||||||
// This calss will be the game source.
|
|
||||||
// search for upcomming games
|
|
||||||
|
|
||||||
export default class GameSource {
|
export default class GameSource {
|
||||||
async getGamesFromHaifa(): Promise<GoogleCalendarEvent[]> {
|
async getGamesFromHaifa(logger: Function): Promise<GoogleCalendarEvent[]> {
|
||||||
const sourceUrl = `https://mhaifafc.com/games?lang=en`;
|
console.log("Trying to get games from Haifa...");
|
||||||
console.log('Trying to get games from Haifa...');
|
|
||||||
try {
|
try {
|
||||||
const result = await axios.get(sourceUrl,
|
// Get the current date and time in the required format
|
||||||
{
|
const currentDate = moment().format("DD/MM/YYYY HH:mm");
|
||||||
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',
|
// Construct the filters object with the current date
|
||||||
'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',
|
const filters = {
|
||||||
'Accept-Language': 'en-US,en;q=0.9',
|
date: {
|
||||||
'Accept-Encoding': 'gzip, deflate, br',
|
startDate: currentDate,
|
||||||
'DNT': '1',
|
endDate: "",
|
||||||
'Connection': 'keep-alive',
|
},
|
||||||
'Upgrade-Insecure-Requests': '1',
|
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());
|
// Extract the games data from the response
|
||||||
const gameBoxs = parsedResult.querySelectorAll(".game-box");
|
const gamesData = response.data.games.items;
|
||||||
|
|
||||||
const games: GoogleCalendarEvent[] = [];
|
const games: GoogleCalendarEvent[] = [];
|
||||||
|
|
||||||
for (let gameBox of gameBoxs) {
|
// Loop through each game and construct the GoogleCalendarEvent objects
|
||||||
const teamsPlaying = gameBox
|
for (const game of gamesData) {
|
||||||
.querySelectorAll(".team-name")
|
const gameDetails = game.gameDetails;
|
||||||
.map((team: any) => team.text);
|
const gameTime = gameDetails.gameTime; // ISO string
|
||||||
const regex = /[\r\n\s]+/g;
|
const isFinalGameDate = gameDetails.isFinalGameDate;
|
||||||
const gameHeader = gameBox
|
const gameLocation = gameDetails.gameLocation;
|
||||||
.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
|
// Skip games without a game time
|
||||||
const lastGameForCalender = headerSplit.length < 4;
|
if (!gameTime) continue;
|
||||||
const location = headerSplit[headerSplit.length - 1].trim();
|
|
||||||
|
|
||||||
if (location === 'נדחה') continue;
|
const hostTeam = game.hostTeam;
|
||||||
if (lastGameForCalender) break;
|
const guestTeam = game.guestTeam;
|
||||||
|
|
||||||
const gameDate = this.findDate(headerSplit);
|
// Get team names
|
||||||
const gameTime = this.findTime(headerSplit);
|
const hostTeamName = hostTeam.teamName;
|
||||||
|
const guestTeamName = guestTeam.teamName;
|
||||||
|
|
||||||
const start = moment(
|
const summary = `${hostTeamName} vs. ${guestTeamName}`;
|
||||||
gameDate + gameTime,
|
|
||||||
"DD/MM/YYYYHH:mm"
|
|
||||||
).toISOString();
|
|
||||||
const end = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm")
|
|
||||||
.add(2, "hours")
|
|
||||||
.toISOString();
|
|
||||||
|
|
||||||
|
// 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({
|
games.push({
|
||||||
summary: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
|
summary: summary,
|
||||||
location: headerSplit[headerSplit.length - 1].trim(),
|
location: gameLocation,
|
||||||
description: `${teamsPlaying[0]} vs. ${teamsPlaying[1]}`,
|
description: description,
|
||||||
start: {
|
start: {
|
||||||
dateTime: start,
|
dateTime: startDateTime,
|
||||||
timeZone: "Asia/Jerusalem",
|
timeZone: "Asia/Jerusalem",
|
||||||
},
|
},
|
||||||
end: {
|
end: {
|
||||||
dateTime: end,
|
dateTime: endDateTime,
|
||||||
timeZone: "Asia/Jerusalem",
|
timeZone: "Asia/Jerusalem",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// return [];
|
|
||||||
return games;
|
return games;
|
||||||
}
|
} catch (error) {
|
||||||
catch (error) {
|
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
return [];
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,13 +17,11 @@ class App {
|
||||||
}
|
}
|
||||||
|
|
||||||
async startCronJob() {
|
async startCronJob() {
|
||||||
this.writeLog('START CRON JOB'); // Log when the cron job starts
|
this.writeLog('START Haifa Reminder'); // Log when the cron job starts
|
||||||
console.log("START Haifa Reminder");
|
|
||||||
const newGamesAdded = [];
|
const newGamesAdded = [];
|
||||||
await this.googleCalendar.init();
|
await this.googleCalendar.init();
|
||||||
try {
|
try {
|
||||||
const games = await this.gameSource.getGamesFromHaifa();
|
const games = await this.gameSource.getGamesFromHaifa(this.writeLog);
|
||||||
|
|
||||||
for (const game of games) {
|
for (const game of games) {
|
||||||
const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent(
|
const isDuplicateEvent = await this.googleCalendar.isDuplicateEvent(
|
||||||
game.start.dateTime,
|
game.start.dateTime,
|
||||||
|
|
Loading…
Reference in a new issue