Compare commits

..

74 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
15932a0c9d changing the cron to every min 2024-02-04 15:31:58 +02:00
3fef736821 changing the cron to be every day at 10 am 2024-02-04 13:40:17 +02:00
486f334b4a changing the logs 2024-02-04 13:38:35 +02:00
e133b49ee9 adding more headers 2024-02-04 13:10:12 +02:00
59552698e9 adding headers to each request to haifa 2024-02-04 13:05:18 +02:00
3ad063cbc2 remove log file from git 2024-02-04 13:01:06 +02:00
55b7b99e48 ran tsc locally 2024-02-04 13:00:14 +02:00
ea7e224c9e ran tsc locally 2024-02-04 12:56:26 +02:00
d5408c359b added try catch to haifa 2024-02-04 12:55:47 +02:00
265a182d8f added try catch to haifa 2024-02-04 12:54:32 +02:00
dbdf76554b remove https 2024-02-04 12:52:15 +02:00
daea0c9cfb invoke cron every min 2024-02-04 11:45:59 +02:00
54483b96b6 adding logs outputs of handeling events 2024-01-23 11:53:52 +02:00
522d30d7dd remove unused code 2023-09-10 13:02:43 +03:00
dc57073067 dockerfile - add TZ env var + title has changed to 2 teams playing 2023-09-10 09:51:08 +03:00
0362566c2a remove dist from repo 2023-09-10 09:50:05 +03:00
b07d434bf3 fixing posponded games 2023-08-21 09:35:44 +03:00
5588c999a7 finding dynamiclly the date and time from the headers 2023-08-15 15:01:11 +03:00
fd7e2377e1 testing 2023-08-13 11:44:41 +03:00
427423d3e8 remove the Value from the comment 2023-08-06 15:17:41 +03:00
87582b72ee build inside repo the dist 2023-08-06 11:25:52 +03:00
1f59ccfb2c cleaning 2023-08-06 10:48:24 +03:00
3aeb79f054 clean up 2023-08-06 00:35:07 +03:00
92a256c784 remove node_modules 2023-07-26 15:16:04 +03:00
fd4b483a6c remove node_modules 2023-07-26 15:15:51 +03:00
f95ed86012 remove node_modules 2023-07-26 15:15:38 +03:00
95b2f4283b done manually 2023-07-26 15:14:58 +03:00
43bb990df5 adding catch 2023-07-26 14:31:24 +03:00
a1ae8c98d1 testing 2023-07-26 14:14:10 +03:00
bbf1865f1a adding node_modules + dist 2023-07-26 14:02:08 +03:00
7675d627d8 adding node_modules + dist 2023-07-26 14:01:58 +03:00
18c3fcb06a start modify 2023-07-26 13:31:35 +03:00
a022002c36 start modify 2023-07-26 13:31:16 +03:00
d408a385bb google calendar enabled 2023-07-26 13:17:32 +03:00
Kfir Dayan
90bb926341 removed google api + changing game info 2023-04-16 14:53:59 +03:00
Kfir Dayan
0fea123354 removed google api + changing game info 2023-04-16 14:53:39 +03:00
Kfir Dayan
d512ca8197 fixing uuid module not found error 2023-04-04 13:44:58 +03:00
Kfir Dayan
9547cedf4b remove_google_api_temporary 2023-04-04 13:03:10 +03:00
Kfir Dayan
b6455824e6 small change in .gitignore & mv the ec2 key into the project 2023-04-04 12:52:18 +03:00
Kfir Dayan
b90d9524ff creating ical file as well 2023-04-04 12:31:20 +03:00
Kfir Dayan
54aec5450f install ical 2023-04-04 11:41:17 +03:00
Kfir Dayan
332f363efc added bash script for build and push 2023-04-03 20:09:44 +03:00
Kfir Dayan
52c8516a1e changed the 404 msg 2023-04-03 20:06:02 +03:00
Kfir Dayan
3a7803ef2b add bash script 2023-04-03 20:05:25 +03:00
Kfir Dayan
7859110d97 dockerfile add tsc command 2023-04-03 19:52:41 +03:00
Kfir Dayan
cfc2d3bc9a 404 2023-04-02 17:03:31 +03:00
8b04c510a7 dockerizing_project (#3)
Co-authored-by: Kfir Dayan <kfirdayan@Kfirs-MacBook-Pro.local>
Reviewed-on: #3

added docker files
2023-04-02 13:43:22 +00:00
Kfir Dayan
09f43fefe6 re-unite the scripts 2023-04-02 15:36:11 +03:00
Kfir Dayan
377f641d49 cron for every min 2023-04-02 11:18:17 +03:00
Kfir Dayan
5e0e203fa3 adding cron 2023-04-02 11:13:43 +03:00
Kfir Dayan
831dfaaea0 every day at 10am the script runs 2023-04-02 11:10:44 +03:00
Kfir Dayan
4360499dd0 git ignoring dist 2023-04-02 10:15:25 +03:00
Kfir Dayan
d6d682a4b0 the web server returns a static files 2023-03-30 13:22:24 +03:00
Kfir Dayan
75c43c3443 install express + created a simple web-server 2023-03-30 09:41:14 +03:00
Kfir Dayan
a1b1c326f8 fix bug - same uuid for each event is a bug 2023-03-30 09:05:25 +03:00
Kfir Dayan
a1d8757453 remove ignore from .ics for testing 2023-03-29 10:47:36 +03:00
Kfir Dayan
b8d0a40315 done adding multipule events 2023-03-28 17:49:02 +03:00
Kfir Dayan
0b0a5efe5c add logs 2023-03-28 17:37:15 +03:00
Kfir Dayan
cc5ab4227a done to output.ics 2023-03-28 17:32:32 +03:00
Kfir Dayan
67babeac15 bug fixed passing the wrong attributes to create function of ics 2023-03-28 17:28:48 +03:00
Kfir Dayan
463573d5a6 removing start method + install ics 2023-03-28 10:50:38 +03:00
Kfir Dayan
8c764da185 cleaning 2023-03-23 21:39:40 +02:00
Kfir Dayan
8770451ebc cleaning 2023-03-23 21:38:18 +02:00
Kfir Dayan
647d746732 removed unused console.log 2023-03-23 21:27:00 +02:00
Kfir Dayan
4051a50387 .env.examle update 2023-03-23 21:19:39 +02:00
Kfir Dayan
e7efa713eb timeout each googleApi request 2023-03-23 21:10:44 +02:00
Kfir Dayan
2daf19e30a html parser had add + parsing new feed source by it 2023-03-23 20:39:29 +02:00
Kfir Dayan
f74b2294d0 install node-html-parser 2023-03-22 21:32:10 +02:00
Kfir Dayan
1a9ff74063 Merge branch 'master' of git.dayanhub.com:kfir/haifa-reminder 2023-03-22 21:25:33 +02:00
Kfir Dayan
654d9d0655 add moment + move the logic of GameResource Data inside 2023-03-22 21:25:20 +02:00
ae8df2b7ec Delete '.DS_Store' 2023-03-22 14:21:13 +00:00
Kfir Dayan
f42ce1e319 made my first insert WIP 2023-03-22 16:19:53 +02:00
18 changed files with 835 additions and 127 deletions

BIN
.DS_Store vendored

Binary file not shown.

3
.dockerignore Normal file
View file

@ -0,0 +1,3 @@
node_modules
npm-debug.log
tmp

View file

@ -1 +1,11 @@
SERPAPI_KEY={NEED_TO_HAVE_AN_ACCOUNT_WITH_SERPAPI_KEY}
# Google
SERPAPI_KEY
# Google
GOOGLE_CALENDAR_ID
GOOGLE_PROJECT_NUMBER
GOOGLE_CLIENT_EMAIL
GOOGLE_PRIVATE_KEY
GOOGLE_CLIENT_ID
GOOGLE_CLIENT_SECRET

14
.gitignore vendored
View file

@ -2,7 +2,8 @@
node_modules
#javascript build files #
public
# public/**/*
dist/**/*
# Tmp files #
tmp
@ -10,4 +11,13 @@ tmp
## ENV Vars ##
.env
config/client_google_auth.json
config/client_google_auth.json
## Docker ##
build_image.sh
push_dockerhub.sh
# keys #
keys/**/*
cron.log

16
Dockerfile Normal file
View file

@ -0,0 +1,16 @@
FROM node:16
ENV TZ=Asia/Jerusalem
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm install -g typescript
COPY . .
EXPOSE ${PORT}
RUN tsc
CMD [ "npm", "start" ]

95
dist/GameSource.js vendored Normal file
View file

@ -0,0 +1,95 @@
"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;

88
dist/GoogleCalendar.js vendored Normal file
View file

@ -0,0 +1,88 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const google_auth_library_1 = require("google-auth-library");
const googleapis_1 = require("googleapis");
require("dotenv").config();
const env = process.env;
class GoogleCalendar {
constructor() {
this.gamesMap = {};
this.clientSecret = env.GOOGLE_CLIENT_SECRET;
this.clientId = env.GOOGLE_CLIENT_ID;
this.calenderId = env.GOOGLE_CALENDAR_ID;
this.clientEmail = env.GOOGLE_CLIENT_EMAIL;
this.googlePrivateKey = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n");
}
async init() {
console.log("INIT GOOGLE CALENDAR");
const jwtClient = await this.authorize();
this.calendar = googleapis_1.google.calendar({ version: "v3", auth: jwtClient });
}
async authorize() {
console.log("AUTHORIZE GOOGLE CALENDAR");
this.JWT_client = new google_auth_library_1.JWT({
email: this.clientEmail,
key: this.googlePrivateKey,
scopes: ["https://www.googleapis.com/auth/calendar"],
});
const { access_token } = await this.JWT_client.authorize();
this.token = access_token;
if (!this.token) {
throw new Error("Failed to connect to google calendar");
}
console.log("GOOGLE CALENDAR AUTHORIZED SUCCESSFULLY");
return this.JWT_client;
}
async updateNewEvent(upcomingEvents) {
// console.log(upcomingEvents)
setTimeout(async () => {
upcomingEvents.forEach(async (event) => {
console.log("UPDATE NEW EVENT", upcomingEvents);
const options = {
auth: this.JWT_client,
calendarId: this.calenderId,
resource: {
summary: event.summary,
location: event.location,
description: event.description,
start: {
dateTime: event.start.dateTime,
timeZone: "Asia/Jerusalem",
},
end: { dateTime: event.end.dateTime, timeZone: "Asia/Jerusalem" },
sendNotifications: true,
},
};
await this.calendar.events.insert(options, function (err, event) {
if (err) {
console.log("There was an error contacting the Calendar service: " + err);
return;
}
console.log(event.description + " created");
});
});
}, 3000);
}
async isDuplicateEvent(startTime, endTime, title) {
if (this.gamesMap[startTime]) {
console.log("duplicate event");
return true;
}
this.gamesMap[startTime] = true;
console.log("checking for duplicate event");
try {
const response = await this.calendar.events.list({
calendarId: this.calenderId,
timeMin: startTime,
timeMax: endTime,
q: title, // Search for events with the same title
});
return response.data.items.length > 0;
}
catch (error) {
console.error(error.message);
return false;
}
}
}
exports.default = GoogleCalendar;

68
dist/index.js vendored Normal file
View file

@ -0,0 +1,68 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const GameSource_1 = __importDefault(require("./GameSource"));
const GoogleCalendar_1 = __importDefault(require("./GoogleCalendar"));
const dotenv_1 = __importDefault(require("dotenv"));
const node_cron_1 = __importDefault(require("node-cron"));
const fs_1 = __importDefault(require("fs")); // Importing fs for logging
dotenv_1.default.config();
class App {
constructor() {
this.gameSource = new GameSource_1.default();
this.googleCalendar = new GoogleCalendar_1.default();
}
async startCronJob() {
this.writeLog('START Haifa Reminder'); // Log when the cron job starts
const newGamesAdded = [];
await this.googleCalendar.init();
try {
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);
if (!isDuplicateEvent) {
newGamesAdded.push(game);
console.log("Event does not exist");
await this.googleCalendar.updateNewEvent([game]);
}
else {
console.log("Event already exists");
}
}
if (newGamesAdded.length > 0) {
console.log("New games added:", newGamesAdded);
}
else {
console.log("No new games were Added!");
}
this.writeLog('Successfully ran project');
}
catch (error) {
this.writeLog("Error in cron job:" + error.message);
}
finally {
this.writeLog('END CRON JOB'); // Log when the cron job ends
}
}
writeLog(message) {
const timestamp = new Date().toISOString();
const logMessage = `${timestamp} - ${message}\n`;
// Write to log file synchronously
fs_1.default.appendFileSync('cron.log', logMessage);
console.log(logMessage); // Optional: also log to console
}
}
const app = new App();
node_cron_1.default.schedule('* * * * *', () => {
console.log('Running startCronJob at 10:00 AM Jerusalem time');
app.startCronJob().catch((error) => {
console.error("Error in scheduled cron job:", error.message);
app.writeLog(`ERROR: ${error.message}`); // Log any errors
});
}, {
scheduled: true,
timezone: "Asia/Jerusalem"
});

2
dist/types/index.js vendored Normal file
View file

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

300
package-lock.json generated
View file

@ -7,18 +7,102 @@
"dependencies": {
"axios": "^1.3.4",
"dotenv": "^16.0.3",
"googleapis": "^113.0.0"
"googleapis": "^122.0.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"node-cron": "^3.0.3",
"node-html-parser": "^6.1.5",
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/node": "^18.15.5"
"@types/express": "^4.17.17",
"@types/node": "^18.15.5",
"@types/node-cron": "^3.0.11",
"typescript": "^5.0.3"
}
},
"node_modules/@types/body-parser": {
"version": "1.19.2",
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz",
"integrity": "sha512-ALYone6pm6QmwZoAgeyNksccT9Q4AWZQ6PvfwR37GT6r6FWUPguq6sUmNGSMV2Wr761oQoBxwGGa6DR5o1DC9g==",
"dev": true,
"dependencies": {
"@types/connect": "*",
"@types/node": "*"
}
},
"node_modules/@types/connect": {
"version": "3.4.35",
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz",
"integrity": "sha512-cdeYyv4KWoEgpBISTxWvqYsVy444DOqehiF3fM3ne10AmJ62RSyNkUnxMJXHQWRQQX2eR94m5y1IZyDwBjV9FQ==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/express": {
"version": "4.17.17",
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.17.tgz",
"integrity": "sha512-Q4FmmuLGBG58btUnfS1c1r/NQdlp3DMfGDGig8WhfpA2YRUtEkxAjkZb0yvplJGYdF1fsQ81iMDcH24sSCNC/Q==",
"dev": true,
"dependencies": {
"@types/body-parser": "*",
"@types/express-serve-static-core": "^4.17.33",
"@types/qs": "*",
"@types/serve-static": "*"
}
},
"node_modules/@types/express-serve-static-core": {
"version": "4.17.33",
"resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.33.tgz",
"integrity": "sha512-TPBqmR/HRYI3eC2E5hmiivIzv+bidAfXofM+sbonAGvyDhySGw9/PQZFt2BLOrjUUR++4eJVpx6KnLQK1Fk9tA==",
"dev": true,
"dependencies": {
"@types/node": "*",
"@types/qs": "*",
"@types/range-parser": "*"
}
},
"node_modules/@types/mime": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-3.0.1.tgz",
"integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==",
"dev": true
},
"node_modules/@types/node": {
"version": "18.15.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.5.tgz",
"integrity": "sha512-Ark2WDjjZO7GmvsyFFf81MXuGTA/d6oP38anyxWOL6EREyBKAxKoFHwBhaZxCfLRLpO8JgVXwqOwSwa7jRcjew==",
"dev": true
},
"node_modules/@types/node-cron": {
"version": "3.0.11",
"resolved": "https://registry.npmjs.org/@types/node-cron/-/node-cron-3.0.11.tgz",
"integrity": "sha512-0ikrnug3/IyneSHqCBeslAhlK2aBfYek1fGo4bP4QnZPmiqSGRK+Oy7ZMisLWkesffJvQ1cqAcBnJC+8+nxIAg==",
"dev": true
},
"node_modules/@types/qs": {
"version": "6.9.7",
"resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.7.tgz",
"integrity": "sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==",
"dev": true
},
"node_modules/@types/range-parser": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.4.tgz",
"integrity": "sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==",
"dev": true
},
"node_modules/@types/serve-static": {
"version": "1.15.1",
"resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.1.tgz",
"integrity": "sha512-NUo5XNiAdULrJENtJXZZ3fHtfMolzZwczzBbnAeBbqBwG+LaG6YaJtuwzwGSQZ2wsCrxjEhNNjAkKigy3n8teQ==",
"dev": true,
"dependencies": {
"@types/mime": "*",
"@types/node": "*"
}
},
"node_modules/agent-base": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz",
@ -80,6 +164,11 @@
"node": "*"
}
},
"node_modules/boolbase": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz",
"integrity": "sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww=="
},
"node_modules/buffer-equal-constant-time": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
@ -108,6 +197,32 @@
"node": ">= 0.8"
}
},
"node_modules/css-select": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/css-select/-/css-select-5.1.0.tgz",
"integrity": "sha512-nwoRF1rvRRnnCqqY7updORDsuqKzqYJ28+oSMaJMMgOauh3fvwHqMS7EZpIPqK8GL+g9mKxF1vP/ZjSeNjEVHg==",
"dependencies": {
"boolbase": "^1.0.0",
"css-what": "^6.1.0",
"domhandler": "^5.0.2",
"domutils": "^3.0.1",
"nth-check": "^2.0.1"
},
"funding": {
"url": "https://github.com/sponsors/fb55"
}
},
"node_modules/css-what": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/css-what/-/css-what-6.1.0.tgz",
"integrity": "sha512-HTUrgRJ7r4dsZKU6GjmpfRK1O76h97Z8MfS1G0FozR+oF2kG6Vfe8JE6zwrkbxigziPHinCJ+gCPjA9EaBDtRw==",
"engines": {
"node": ">= 6"
},
"funding": {
"url": "https://github.com/sponsors/fb55"
}
},
"node_modules/debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@ -132,6 +247,57 @@
"node": ">=0.4.0"
}
},
"node_modules/dom-serializer": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz",
"integrity": "sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==",
"dependencies": {
"domelementtype": "^2.3.0",
"domhandler": "^5.0.2",
"entities": "^4.2.0"
},
"funding": {
"url": "https://github.com/cheeriojs/dom-serializer?sponsor=1"
}
},
"node_modules/domelementtype": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.3.0.tgz",
"integrity": "sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/fb55"
}
]
},
"node_modules/domhandler": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-5.0.3.tgz",
"integrity": "sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==",
"dependencies": {
"domelementtype": "^2.3.0"
},
"engines": {
"node": ">= 4"
},
"funding": {
"url": "https://github.com/fb55/domhandler?sponsor=1"
}
},
"node_modules/domutils": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/domutils/-/domutils-3.0.1.tgz",
"integrity": "sha512-z08c1l761iKhDFtfXO04C7kTdPBLi41zwOZl00WS8b5eiaebNpY00HKbztwBq+e3vyqWNwWF3mP9YLUeqIrF+Q==",
"dependencies": {
"dom-serializer": "^2.0.0",
"domelementtype": "^2.3.0",
"domhandler": "^5.0.1"
},
"funding": {
"url": "https://github.com/fb55/domutils?sponsor=1"
}
},
"node_modules/dotenv": {
"version": "16.0.3",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz",
@ -148,6 +314,17 @@
"safe-buffer": "^5.0.1"
}
},
"node_modules/entities": {
"version": "4.4.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-4.4.0.tgz",
"integrity": "sha512-oYp7156SP8LkeGD0GF85ad1X9Ai79WtRsZ2gxJqtBuzH+98YUV6jkHEKlZkMbcrjJjIVJNIDP/3WL9wQkoPbWA==",
"engines": {
"node": ">=0.12"
},
"funding": {
"url": "https://github.com/fb55/entities?sponsor=1"
}
},
"node_modules/extend": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
@ -196,23 +373,23 @@
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
},
"node_modules/gaxios": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/gaxios/-/gaxios-5.1.0.tgz",
"integrity": "sha512-aezGIjb+/VfsJtIcHGcBSerNEDdfdHeMros+RbYbGpmonKWQCOVOes0LVZhn1lDtIgq55qq0HaxymIoae3Fl/A==",
"version": "5.1.3",
"resolved": "https://registry.npmjs.org/gaxios/-/gaxios-5.1.3.tgz",
"integrity": "sha512-95hVgBRgEIRQQQHIbnxBXeHbW4TqFk4ZDJW7wmVtvYar72FdhRIo1UGOLS2eRAKCPEdPBWu+M7+A33D9CdX9rA==",
"dependencies": {
"extend": "^3.0.2",
"https-proxy-agent": "^5.0.0",
"is-stream": "^2.0.0",
"node-fetch": "^2.6.7"
"node-fetch": "^2.6.9"
},
"engines": {
"node": ">=12"
}
},
"node_modules/gcp-metadata": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-5.2.0.tgz",
"integrity": "sha512-aFhhvvNycky2QyhG+dcfEdHBF0FRbYcf39s6WNHUDysKSrbJ5vuFbjydxBcmewtXeV248GP8dWT3ByPNxsyHCw==",
"version": "5.3.0",
"resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-5.3.0.tgz",
"integrity": "sha512-FNTkdNEnBdlqF2oatizolQqNANMrcqJt6AAYt99B3y1aLLC8Hc5IOBb+ZnnzllodEEf6xMBp6wRcBbc16fa65w==",
"dependencies": {
"gaxios": "^5.0.0",
"json-bigint": "^1.0.0"
@ -235,16 +412,16 @@
}
},
"node_modules/google-auth-library": {
"version": "8.7.0",
"resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-8.7.0.tgz",
"integrity": "sha512-1M0NG5VDIvJZEnstHbRdckLZESoJwguinwN8Dhae0j2ZKIQFIV63zxm6Fo6nM4xkgqUr2bbMtV5Dgo+Hy6oo0Q==",
"version": "8.9.0",
"resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-8.9.0.tgz",
"integrity": "sha512-f7aQCJODJFmYWN6PeNKzgvy9LI2tYmXnzpNDHEjG5sDNPgGb2FXQyTBnXeSH+PAtpKESFD+LmHw3Ox3mN7e1Fg==",
"dependencies": {
"arrify": "^2.0.0",
"base64-js": "^1.3.0",
"ecdsa-sig-formatter": "^1.0.11",
"fast-text-encoding": "^1.0.0",
"gaxios": "^5.0.0",
"gcp-metadata": "^5.0.0",
"gcp-metadata": "^5.3.0",
"gtoken": "^6.1.0",
"jws": "^4.0.0",
"lru-cache": "^6.0.0"
@ -268,9 +445,9 @@
}
},
"node_modules/googleapis": {
"version": "113.0.0",
"resolved": "https://registry.npmjs.org/googleapis/-/googleapis-113.0.0.tgz",
"integrity": "sha512-gsknEobaFuS+ACraCL3w22pAqeg1HselNhiSjcF5p2d2t2HB3QchbRDrFCfZ0wc4gKLYUBdmGzulCmRRL5qNXw==",
"version": "122.0.0",
"resolved": "https://registry.npmjs.org/googleapis/-/googleapis-122.0.0.tgz",
"integrity": "sha512-n8Gt7j9LzSkhQEGPOrcLBKxllTvW/0v6oILuwszL/zqgelNsGJYXVqPJllgJJ6RM7maJ6T35UBeYqI6GQ/IlJg==",
"dependencies": {
"google-auth-library": "^8.0.2",
"googleapis-common": "^6.0.0"
@ -330,6 +507,14 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/he": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
"bin": {
"he": "bin/he"
}
},
"node_modules/https-proxy-agent": {
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz",
@ -410,15 +595,53 @@
"node": ">= 0.6"
}
},
"node_modules/moment": {
"version": "2.29.4",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz",
"integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==",
"engines": {
"node": "*"
}
},
"node_modules/moment-timezone": {
"version": "0.5.43",
"resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.43.tgz",
"integrity": "sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ==",
"dependencies": {
"moment": "^2.29.4"
},
"engines": {
"node": "*"
}
},
"node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"node_modules/node-cron": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz",
"integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==",
"dependencies": {
"uuid": "8.3.2"
},
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/node-cron/node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/node-fetch": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.9.tgz",
"integrity": "sha512-DJm/CJkZkRjKKj4Zi4BsKVZh3ValV5IR5s7LVZnW+6YMh0W1BfNA8XSs6DLMGYlId5F3KnA70uu2qepcR08Qqg==",
"version": "2.6.12",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz",
"integrity": "sha512-C/fGU2E8ToujUivIO0H+tpQ6HWo4eEmchoPIoXtxCrVghxdKq+QOHqEZW7tuP3KlV3bC8FRMO5nMCC7Zm1VP6g==",
"dependencies": {
"whatwg-url": "^5.0.0"
},
@ -442,6 +665,26 @@
"node": ">= 6.13.0"
}
},
"node_modules/node-html-parser": {
"version": "6.1.5",
"resolved": "https://registry.npmjs.org/node-html-parser/-/node-html-parser-6.1.5.tgz",
"integrity": "sha512-fAaM511feX++/Chnhe475a0NHD8M7AxDInsqQpz6x63GRF7xYNdS8Vo5dKsIVPgsOvG7eioRRTZQnWBrhDHBSg==",
"dependencies": {
"css-select": "^5.1.0",
"he": "1.2.0"
}
},
"node_modules/nth-check": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/nth-check/-/nth-check-2.1.1.tgz",
"integrity": "sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w==",
"dependencies": {
"boolbase": "^1.0.0"
},
"funding": {
"url": "https://github.com/fb55/nth-check?sponsor=1"
}
},
"node_modules/object-inspect": {
"version": "1.12.3",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz",
@ -456,9 +699,9 @@
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/qs": {
"version": "6.11.1",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.1.tgz",
"integrity": "sha512-0wsrzgTz/kAVIeuxSjnpGC56rzYtr6JT/2BwEvMaPhFIoYa1aGO8LbzuU1R0uUYQkLpWBTOj0l/CLAJB64J6nQ==",
"version": "6.11.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.2.tgz",
"integrity": "sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==",
"dependencies": {
"side-channel": "^1.0.4"
},
@ -506,6 +749,19 @@
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="
},
"node_modules/typescript": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.0.3.tgz",
"integrity": "sha512-xv8mOEDnigb/tN9PSMTwSEqAnUvkoXMQlicOb0IUVDBSQCgBSaAAROUZYy2IcUy5qU6XajK5jjjO7TMWqBTKZA==",
"dev": true,
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=12.20"
}
},
"node_modules/url-template": {
"version": "2.0.8",
"resolved": "https://registry.npmjs.org/url-template/-/url-template-2.0.8.tgz",

View file

@ -2,12 +2,22 @@
"dependencies": {
"axios": "^1.3.4",
"dotenv": "^16.0.3",
"googleapis": "^113.0.0"
"googleapis": "^122.0.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"node-cron": "^3.0.3",
"node-html-parser": "^6.1.5",
"uuid": "^9.0.0"
},
"scripts": {
"dev": "nodemon --watch insted --ignore tmp/ public/index.js"
"dev": "nodemon dist/index.js",
"build": "npx tsc",
"start": "node dist/index.js"
},
"devDependencies": {
"@types/node": "^18.15.5"
"@types/express": "^4.17.17",
"@types/node": "^18.15.5",
"@types/node-cron": "^3.0.11",
"typescript": "^5.0.3"
}
}

4
restart.sh Normal file
View file

@ -0,0 +1,4 @@
#!/bin/bash
docker stop haifareminder
docker rm haifareminder
docker run -p 3000:3000 -d --restart=unless-stopped --name haifareminder docker.io/kfda89/haifareminder

View file

@ -1,14 +1,106 @@
require('dotenv').config();
require("dotenv").config();
import axios from "axios";
// This calss will be the game source.
// search for upcomming games
import { GoogleCalendarEvent } from "./types";
import moment from "moment";
export default class GameSource {
constructor() {}
async getGamesFromHaifa(logger: Function): Promise<GoogleCalendarEvent[]> {
console.log("Trying to get games from Haifa...");
async getGames() {
const sourceUrl = `https://serpapi.com/search.json?q=maccabi+haifa+next+games&api_key=${process.env.SERPAPI_KEY}&location=austin,+texas,+united+states`;
const result = await axios.get(sourceUrl)
return result;
try {
// 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
});
// Extract the games data from the response
const gamesData = response.data.games.items;
const games: GoogleCalendarEvent[] = [];
// 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 = moment(gameTime).toISOString();
const endDateTime = moment(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 [];
}
}
}
}

View file

@ -1,59 +1,97 @@
import { JWT } from 'google-auth-library';
import { google } from 'googleapis';
import { GoogleCalendarEvent } from './types/index';
import { JWT } from "google-auth-library";
import { google } from "googleapis";
import { GoogleCalendarEvent } from "./types/index";
require('dotenv').config();
require("dotenv").config();
const env = process.env;
export default class GoogleCalendar {
client_secret: string;
client_id: string;
calender_id: string;
gamesMap: any = {};
clientSecret: string = env.GOOGLE_CLIENT_SECRET;
clientId: string = env.GOOGLE_CLIENT_ID;
calenderId: string = env.GOOGLE_CALENDAR_ID;
calendar: any;
clientEmail: string;
googlePrivateKey: string;
clientEmail: string = env.GOOGLE_CLIENT_EMAIL;
googlePrivateKey: string = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n");
token: any;
constructor() {
this.client_secret = env.GOOGLE_CLIENT_SECRET;
this.client_id = env.GOOGLE_CLIENT_ID;
this.calender_id = env.GOOGLE_CALENDAR_ID;
this.clientEmail = env.GOOGLE_CLIENT_EMAIL
this.googlePrivateKey = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n');
// this.redirect_uris = env.GOOGLE_REDIRECT_URIS;
}
JWT_client: JWT;
async init() {
console.log("INIT GOOGLE CALENDAR");
const jwtClient = await this.authorize();
this.calendar = google.calendar({ version: 'v3', auth: jwtClient });
this.calendar = google.calendar({ version: "v3", auth: jwtClient });
}
async authorize() {
const client = new JWT({
console.log("AUTHORIZE GOOGLE CALENDAR");
this.JWT_client = new JWT({
email: this.clientEmail,
key: this.googlePrivateKey,
scopes: 'https://www.googleapis.com/auth/calendar.events'
scopes: ["https://www.googleapis.com/auth/calendar"],
});
const { access_token } = await client.authorize();
const { access_token } = await this.JWT_client.authorize();
this.token = access_token;
if(!this.token) {
throw new Error('Failed to connect to google calendar');
if (!this.token) {
throw new Error("Failed to connect to google calendar");
}
return client;
console.log("GOOGLE CALENDAR AUTHORIZED SUCCESSFULLY");
return this.JWT_client;
}
updateNewEvent(upcomingEvents: GoogleCalendarEvent[]) {
this.calendar.event.insert({
auth: this.token,
calendarId: this.calender_id,
resource: upcomingEvents[0],
}, function (err: any, event: any) {
if (err) {
console.log('There was an error contacting the Calendar service: ' + err);
return;
}
console.log('Event created: %s', event.htmlLink);
});
async updateNewEvent(upcomingEvents: GoogleCalendarEvent[]) {
// console.log(upcomingEvents)
setTimeout(async () => {
upcomingEvents.forEach(async (event: GoogleCalendarEvent) => {
console.log("UPDATE NEW EVENT", upcomingEvents);
const options = {
auth: this.JWT_client,
calendarId: this.calenderId,
resource: {
summary: event.summary,
location: event.location,
description: event.description,
start: {
dateTime: event.start.dateTime,
timeZone: "Asia/Jerusalem",
},
end: { dateTime: event.end.dateTime, timeZone: "Asia/Jerusalem" },
sendNotifications: true,
},
};
await this.calendar.events.insert(
options,
function (err: any, event: any) {
if (err) {
console.log(
"There was an error contacting the Calendar service: " + err
);
return;
}
console.log(event.description + " created");
}
);
});
}, 3000);
}
async isDuplicateEvent(startTime: string, endTime: string, title: string) {
if (this.gamesMap[startTime]) {
console.log("duplicate event");
return true;
}
this.gamesMap[startTime] = true;
console.log("checking for duplicate event");
try {
const response = await this.calendar.events.list({
calendarId: this.calenderId,
timeMin: startTime,
timeMax: endTime,
q: title, // Search for events with the same title
});
return response.data.items.length > 0;
} catch (error) {
console.error(error.message);
return false;
}
}
}

View file

@ -1,63 +1,74 @@
import GoogleCalendar from './GoogleCalendar';
import GameSource from './GameSource';
import { GoogleCalendarEvent } from './types/index';
// crete App class
// app class will be the main class of our application
// it will be collecting macabi haifa events from GameSource class
// it will be responsible for update GameSource events in google calendar
// it will be responsible for update GameSource events in local file
import GameSource from "./GameSource";
import GoogleCalendar from "./GoogleCalendar";
import env from "dotenv";
import cron from 'node-cron';
import fs from 'fs'; // Importing fs for logging
env.config();
class App {
gameSource: GameSource;
googleToken: string;
googleCalendar: GoogleCalendar;
gameSource: any;
googleToken: any;
constructor() {
this.googleCalendar = new GoogleCalendar();
this.gameSource = new GameSource();
this.googleCalendar = new GoogleCalendar();
}
async init() {
this.googleToken = this.googleCalendar.init();
}
getNewGamesAndUpdateCalendar() {
const upcomingEvents: GoogleCalendarEvent[] = [];
this.gameSource.getGames().then((games: any) => {
const rootGames = games.data.sports_results;
upcomingEvents.push({
summary: rootGames.title,
location: rootGames.game_spotlight.stadium,
description: "Haifa vs. " + rootGames.game_spotlight.teams[this.getHaifaIndex(rootGames.game_spotlight.teams)].name,
start: {
dateTime: '2023-03-22T09:00:00-07:00',
timeZone: 'Israel'
},
end: {
dateTime: '2023-03-22T09:00:00-07:00',
timeZone: 'Israel'
},
recurrence: []
});
console.log("Updaing new event: " + upcomingEvents[0].summary)
console.log(upcomingEvents[0])
this.googleCalendar.updateNewEvent(upcomingEvents);
});
}
getHaifaIndex(teams: any) {
for (let i = 0; i < teams.length; i++) {
if (teams[i].name === "Maccabi Haifa") {
return i;
async startCronJob() {
this.writeLog('START Haifa Reminder'); // Log when the cron job starts
const newGamesAdded = [];
await this.googleCalendar.init();
try {
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)
if (!isDuplicateEvent) {
newGamesAdded.push(game);
console.log("Event does not exist");
await this.googleCalendar.updateNewEvent([game]);
} else {
console.log("Event already exists");
}
}
if (newGamesAdded.length > 0) {
console.log("New games added:", newGamesAdded);
} else {
console.log("No new games were Added!");
}
this.writeLog('Successfully ran project');
} catch (error) {
this.writeLog("Error in cron job:" + error.message);
} finally {
this.writeLog('END CRON JOB'); // Log when the cron job ends
}
}
writeLog(message) {
const timestamp = new Date().toISOString();
const logMessage = `${timestamp} - ${message}\n`;
// Write to log file synchronously
fs.appendFileSync('cron.log', logMessage);
console.log(logMessage); // Optional: also log to console
}
}
const app = new App();
app.init();
app.getNewGamesAndUpdateCalendar();
cron.schedule('* * * * *', () => {
console.log('Running startCronJob at 10:00 AM Jerusalem time');
app.startCronJob().catch((error) => {
console.error("Error in scheduled cron job:", error.message);
app.writeLog(`ERROR: ${error.message}`); // Log any errors
});
}, {
scheduled: true,
timezone: "Asia/Jerusalem"
});

View file

@ -10,5 +10,4 @@ export interface GoogleCalendarEvent {
dateTime: string;
timeZone: string;
};
recurrence: string[];
}

3
start.sh Normal file
View file

@ -0,0 +1,3 @@
#!/bin/bash
docker run -p 3000:3000 -d --restart=unless-stopped --name haifareminder docker.io/kfda89/haifareminder

View file

@ -5,7 +5,10 @@
"esModuleInterop": true,
"target": "ES2019",
"moduleResolution": "node",
"outDir": "./public",
"outDir": "./dist",
"rootDir": "./src"
}
}
},
"include": [
"src/**/*"
]
}