creating ical file as well

This commit is contained in:
Kfir Dayan 2023-04-04 12:31:20 +03:00
parent 54aec5450f
commit b90d9524ff
2 changed files with 51 additions and 28 deletions

View file

@ -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;
}
}

View file

@ -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...")
})