haifa-reminder/src/index.ts
2023-04-03 19:52:41 +03:00

67 lines
1.7 KiB
TypeScript

import GoogleCalendar from './GoogleCalendar';
import GameSource from './GameSource';
import fs from 'fs';
import Ics from './Ics';
import express from 'express'
import env from 'dotenv';
env.config();
class App {
googleCalendar: GoogleCalendar;
gameSource: GameSource;
ics: Ics;
googleToken: string;
constructor() {
this.googleCalendar = new GoogleCalendar();
this.gameSource = new GameSource();
this.ics = new Ics();
}
async init() {
console.log("INIT APP")
await this.googleCalendar.init();
}
async getNewGamesAndUpdateCalendar() {
console.log("GET NEW GAMES AND UPDATE CALENDAR")
const games = await this.gameSource.getGamesFromHaifa();
this.googleCalendar.updateNewEvent(games);
}
}
const app = new App();
console.log("Declaring Cron Job every day at 10:00")
const CronJob = require('cron').CronJob;
const job = new CronJob(
"0 10 * * *", // every day at 10:00,
async () => {
console.log("Staring a new job")
const outputFileLocation = 'public/maccabi-haifa-fc.ics';
console.log("Getting games from Haifa")
const games = await app.gameSource.getGamesFromHaifa();
console.log("Generating ICS file")
const icsEvents = app.ics.generateIcsOutputFromGames(games);
console.log("Writing ICS file to " + outputFileLocation)
fs.writeFileSync(outputFileLocation, icsEvents);
console.log("Done")
},
null,
true,
'Asia/Jerusalem'
);
const webServer = express();
webServer.use(express.static('public'))
webServer.listen(process.env.PORT, () => {
console.log(`Calender app listening on port ${process.env.PORT}!`)
})
webServer.use(function (req, res, next) {
res.status(404).send("Sorry can't find that!")
})