haifa-reminder/dist/GameSource.js

96 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-08-06 08:25:52 +00:00
"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();
2023-08-06 08:25:52 +00:00
const axios_1 = __importDefault(require("axios"));
2024-09-21 10:25:42 +00:00
const moment_1 = __importDefault(require("moment"));
2023-08-06 08:25:52 +00:00
class GameSource {
2024-07-31 18:53:04 +00:00
async getGamesFromHaifa(logger) {
2024-09-21 10:25:42 +00:00
console.log("Trying to get games from Haifa...");
2024-02-04 10:56:26 +00:00
try {
2024-09-21 10:25:42 +00:00
// 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
});
2024-09-21 10:25:42 +00:00
// Extract the games data from the response
const gamesData = response.data.games.items;
2024-02-04 10:56:26 +00:00
const games = [];
2024-09-21 10:25:42 +00:00
// 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)
2024-02-04 10:56:26 +00:00
continue;
2024-09-21 10:25:42 +00:00
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
2024-02-04 10:56:26 +00:00
games.push({
2024-09-21 10:25:42 +00:00
summary: summary,
location: gameLocation,
description: description,
2024-02-04 10:56:26 +00:00
start: {
2024-09-21 10:25:42 +00:00
dateTime: startDateTime,
2024-02-04 10:56:26 +00:00
timeZone: "Asia/Jerusalem",
},
end: {
2024-09-21 10:25:42 +00:00
dateTime: endDateTime,
2024-02-04 10:56:26 +00:00
timeZone: "Asia/Jerusalem",
},
});
}
return games;
}
catch (error) {
console.error(error);
2024-09-21 10:25:42 +00:00
return [];
2023-08-06 08:25:52 +00:00
}
}
}
exports.default = GameSource;