haifa-reminder/dist/GameSource.js

95 lines
4.1 KiB
JavaScript

"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();
const axios_1 = __importDefault(require("axios"));
const moment_1 = __importDefault(require("moment"));
class GameSource {
async getGamesFromHaifa(logger) {
console.log("Trying to get games from Haifa...");
try {
// 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
});
// Extract the games data from the response
const gamesData = response.data.games.items;
const games = [];
// 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;
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: summary,
location: gameLocation,
description: description,
start: {
dateTime: startDateTime,
timeZone: "Asia/Jerusalem",
},
end: {
dateTime: endDateTime,
timeZone: "Asia/Jerusalem",
},
});
}
return games;
}
catch (error) {
console.error(error);
return [];
}
}
}
exports.default = GameSource;