changing source of games
This commit is contained in:
parent
d3460f8871
commit
3d8306b225
4 changed files with 158 additions and 181 deletions
140
dist/GameSource.js
vendored
140
dist/GameSource.js
vendored
|
@ -5,62 +5,81 @@ 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(logger) {
|
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();
|
|
||||||
const gameDate = this.findDate(headerSplit);
|
|
||||||
const gameTime = this.findTime(headerSplit);
|
|
||||||
if (location === 'נדחה')
|
|
||||||
continue;
|
continue;
|
||||||
if (lastGameForCalender && gameDate !== '')
|
const hostTeam = game.hostTeam;
|
||||||
break;
|
const guestTeam = game.guestTeam;
|
||||||
const start = (0, moment_1.default)(gameDate + gameTime, "DD/MM/YYYYHH:mm").toISOString();
|
// Get team names
|
||||||
const end = (0, moment_1.default)(gameDate + gameTime, "DD/MM/YYYYHH:mm")
|
const hostTeamName = hostTeam.teamName;
|
||||||
.add(2, "hours")
|
const guestTeamName = guestTeam.teamName;
|
||||||
.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 = (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",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -69,36 +88,7 @@ class GameSource {
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// console.log("Cheking date: ", date, "By split: ", headerSplit)
|
|
||||||
return date.trim();
|
|
||||||
}
|
|
||||||
getOpponentIndexByStadium(stadium) {
|
|
||||||
if (stadium === "Sammy Ofer Stadium") {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
3
dist/index.js
vendored
3
dist/index.js
vendored
|
@ -15,8 +15,7 @@ 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 {
|
||||||
|
|
|
@ -1,116 +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
|
|
||||||
import fs from 'fs'; // Importing fs for logging
|
|
||||||
|
|
||||||
// This calss will be the game source.
|
|
||||||
// search for upcomming games
|
|
||||||
|
|
||||||
export default class GameSource {
|
export default class GameSource {
|
||||||
async getGamesFromHaifa(logger: Function): 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
|
||||||
|
for (const game of gamesData) {
|
||||||
|
const gameDetails = game.gameDetails;
|
||||||
|
const gameTime = gameDetails.gameTime; // ISO string
|
||||||
|
const isFinalGameDate = gameDetails.isFinalGameDate;
|
||||||
|
const gameLocation = gameDetails.gameLocation;
|
||||||
|
|
||||||
const teamsPlaying = gameBox
|
// Skip games without a game time
|
||||||
.querySelectorAll(".team-name")
|
if (!gameTime) continue;
|
||||||
.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 hostTeam = game.hostTeam;
|
||||||
const lastGameForCalender = headerSplit.length < 4;
|
const guestTeam = game.guestTeam;
|
||||||
const location = headerSplit[headerSplit.length - 1].trim();
|
|
||||||
|
|
||||||
const gameDate = this.findDate(headerSplit);
|
// Get team names
|
||||||
const gameTime = this.findTime(headerSplit);
|
const hostTeamName = hostTeam.teamName;
|
||||||
|
const guestTeamName = guestTeam.teamName;
|
||||||
|
|
||||||
if (location === 'נדחה') continue;
|
const summary = `${hostTeamName} vs. ${guestTeamName}`;
|
||||||
if (lastGameForCalender && gameDate !== '') break;
|
|
||||||
|
|
||||||
const start = moment(
|
// Include a note if the game date is not final
|
||||||
gameDate + gameTime,
|
let description = `${hostTeamName} vs. ${guestTeamName}`;
|
||||||
"DD/MM/YYYYHH:mm"
|
if (!isFinalGameDate) {
|
||||||
).toISOString();
|
description += " (Date and time are subject to change)";
|
||||||
const end = moment(gameDate + gameTime, "DD/MM/YYYYHH:mm")
|
}
|
||||||
.add(2, "hours")
|
|
||||||
.toISOString();
|
|
||||||
|
|
||||||
|
// 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 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;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// console.log("Cheking date: ", date, "By split: ", headerSplit)
|
|
||||||
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(this.writeLog);
|
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