timeout each googleApi request

This commit is contained in:
Kfir Dayan 2023-03-23 21:10:44 +02:00
parent 2daf19e30a
commit e7efa713eb
3 changed files with 28 additions and 21 deletions

View file

@ -30,12 +30,16 @@ export default class GameSource {
for (let gameBox of gameBoxs) {
const teamsPlaying = gameBox.querySelectorAll(".team-name").map((team: any) => team.text);
console.log("TEAMS PLAYING", teamsPlaying)
const regex = /[\r\n\s]+/g;
const gameHeader = gameBox.querySelector(".game-header").text.replace(regex, " ").trim();
console.log("GAME HEADER", gameHeader)
const headerSplit = gameHeader.split(",");
if (headerSplit.length < 4) break;
console.log("HEADER SPLIT", headerSplit)
// In data, if there is no time, it means it's the last game for the calender
const lastGameForCalender = headerSplit.length < 4;
if (lastGameForCalender) break;
const gameDate = headerSplit[2].trim();
const gameTime = headerSplit[3].trim();

View file

@ -41,25 +41,28 @@ export default class GoogleCalendar {
return this.JWT_client;
}
updateNewEvent(upcomingEvents: GoogleCalendarEvent[]) {
upcomingEvents.forEach((event: GoogleCalendarEvent) => {
console.log("UPDATE NEW EVENT", upcomingEvents)
async updateNewEvent(upcomingEvents: GoogleCalendarEvent[]) {
const options = {
auth: this.JWT_client,
calendarId: this.calenderId,
resource: event,
}
this.calendar.events.insert(options, function (err: any, event: any) {
if (err) {
console.log('There was an error contacting the Calendar service: ' + err);
return;
setTimeout(async () => {
upcomingEvents.forEach((event: GoogleCalendarEvent) => {
console.log("UPDATE NEW EVENT", upcomingEvents)
const options = {
auth: this.JWT_client,
calendarId: this.calenderId,
resource: event,
}
console.log(event.description + ' created');
});
})
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)
}
}

View file

@ -32,6 +32,7 @@ class App {
const games = await this.gameSource.getGamesFromHaifa();
// console.log(games)
this.googleCalendar.updateNewEvent(games);
}
}
@ -42,5 +43,4 @@ const start = async () => {
await app.init();
await app.getNewGamesAndUpdateCalendar();
}
start();
// console.log(moment("Sel, 4/4").format());
start();