From e7efa713ebea8b028ea941bc844cc348eeb610d3 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Thu, 23 Mar 2023 21:10:44 +0200 Subject: [PATCH] timeout each googleApi request --- src/GameSource.ts | 10 +++++++--- src/GoogleCalendar.ts | 35 +++++++++++++++++++---------------- src/index.ts | 4 ++-- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/GameSource.ts b/src/GameSource.ts index f44f1c4..defa4ea 100644 --- a/src/GameSource.ts +++ b/src/GameSource.ts @@ -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(); diff --git a/src/GoogleCalendar.ts b/src/GoogleCalendar.ts index fad9642..2a8e9f2 100644 --- a/src/GoogleCalendar.ts +++ b/src/GoogleCalendar.ts @@ -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) } } diff --git a/src/index.ts b/src/index.ts index b96e7c6..70599e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(); \ No newline at end of file