diff --git a/src/Ics.ts b/src/Ics.ts index 548067e..d06d572 100644 --- a/src/Ics.ts +++ b/src/Ics.ts @@ -40,4 +40,9 @@ export default class Ics { }; return icsEvent; } + + convertIcsToIcal = (icsEvents: string) => { + const icalEvents = icsEvents.replace(/BEGIN:VEVENT/g, 'BEGIN:VEVENT\r\nUID:' + uuid); + return icalEvents; + } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 6bc650a..7ca829e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ class App { this.googleCalendar = new GoogleCalendar(); this.gameSource = new GameSource(); this.ics = new Ics(); - + } async init() { @@ -30,38 +30,56 @@ class App { const games = await this.gameSource.getGamesFromHaifa(); this.googleCalendar.updateNewEvent(games); } + + async startCronJob() { + console.log("START CRON JOB") + const CronJob = require('cron').CronJob; + const job = new CronJob( + "* * * * *", // 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 Ics file") + + console.log("converting ics file to ical file") + const outputIcalFileLocation = 'public/maccabi-haifa-fc.ical'; + const icalEvents = app.ics.convertIcsToIcal(icsEvents); + console.log("Writing Ical file to " + outputIcalFileLocation) + fs.writeFileSync(outputIcalFileLocation, icalEvents); + + }, + null, + true, + 'Asia/Jerusalem' + ); + + } + + async startWebServer() { + 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("This is not the page you are looking for...") + }) + } } 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' -); +app.startCronJob(); +app.startWebServer(); -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("This is not the page you are looking for...") -}) \ No newline at end of file