From f42ce1e319552275e1b7328be7ae62dc4fb443bb Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Wed, 22 Mar 2023 16:19:53 +0200 Subject: [PATCH] made my first insert WIP --- src/GameSource.ts | 1 + src/GoogleCalendar.ts | 63 ++++++++++++++++++++++------------------- src/index.ts | 65 ++++++++++++++++++++++++++++++------------- src/types/index.ts | 1 - 4 files changed, 81 insertions(+), 49 deletions(-) diff --git a/src/GameSource.ts b/src/GameSource.ts index ee7be65..fae007c 100644 --- a/src/GameSource.ts +++ b/src/GameSource.ts @@ -7,6 +7,7 @@ export default class GameSource { constructor() {} async getGames() { + console.log("GET GAMES") const sourceUrl = `https://serpapi.com/search.json?q=maccabi+haifa+next+games&api_key=${process.env.SERPAPI_KEY}&location=austin,+texas,+united+states`; const result = await axios.get(sourceUrl) return result; diff --git a/src/GoogleCalendar.ts b/src/GoogleCalendar.ts index 7496c78..bb8833f 100644 --- a/src/GoogleCalendar.ts +++ b/src/GoogleCalendar.ts @@ -7,53 +7,58 @@ const env = process.env; export default class GoogleCalendar { - client_secret: string; - client_id: string; - calender_id: string; + clientSecret: string = env.GOOGLE_CLIENT_SECRET; + clientId: string = env.GOOGLE_CLIENT_ID; + calenderId: string = env.GOOGLE_CALENDAR_ID; calendar: any; - clientEmail: string; - googlePrivateKey: string; + clientEmail: string = env.GOOGLE_CLIENT_EMAIL; + googlePrivateKey: string = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n'); token: any; - - constructor() { - this.client_secret = env.GOOGLE_CLIENT_SECRET; - this.client_id = env.GOOGLE_CLIENT_ID; - this.calender_id = env.GOOGLE_CALENDAR_ID; - this.clientEmail = env.GOOGLE_CLIENT_EMAIL - this.googlePrivateKey = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n'); - // this.redirect_uris = env.GOOGLE_REDIRECT_URIS; - } + JWT_client: JWT; async init() { + console.log("INIT GOOGLE CALENDAR") const jwtClient = await this.authorize(); this.calendar = google.calendar({ version: 'v3', auth: jwtClient }); + console.log("DONE INIT GOOGLE CALENDAR") } async authorize() { - const client = new JWT({ + console.log("AUTHORIZE GOOGLE CALENDAR") + this.JWT_client = new JWT({ email: this.clientEmail, key: this.googlePrivateKey, - scopes: 'https://www.googleapis.com/auth/calendar.events' + scopes: [ + 'https://www.googleapis.com/auth/calendar', + ] }); - const { access_token } = await client.authorize(); + const { access_token } = await this.JWT_client.authorize(); this.token = access_token; - if(!this.token) { + if (!this.token) { throw new Error('Failed to connect to google calendar'); } - return client; + console.log("GOOGLE CALENDAR AUTHORIZED SUCCESSFULLY") + return this.JWT_client; } updateNewEvent(upcomingEvents: GoogleCalendarEvent[]) { - this.calendar.event.insert({ - auth: this.token, - calendarId: this.calender_id, - resource: upcomingEvents[0], - }, function (err: any, event: any) { - if (err) { - console.log('There was an error contacting the Calendar service: ' + err); - return; + upcomingEvents.forEach((event: GoogleCalendarEvent) => { + console.log("UPDATE NEW EVENT", upcomingEvents) + + const options = { + auth: this.JWT_client, + calendarId: this.calenderId, + resource: event, } - console.log('Event created: %s', event.htmlLink); - }); + + 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'); + }); + }) + } } diff --git a/src/index.ts b/src/index.ts index 9e0979d..b121b69 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,42 +22,69 @@ class App { } async init() { - this.googleToken = this.googleCalendar.init(); + console.log("INIT APP") + await this.googleCalendar.init(); } - getNewGamesAndUpdateCalendar() { + async getNewGamesAndUpdateCalendar() { + console.log("GET NEW GAMES AND UPDATE CALENDAR") const upcomingEvents: GoogleCalendarEvent[] = []; this.gameSource.getGames().then((games: any) => { const rootGames = games.data.sports_results; + console.log("ROOT GAMES", rootGames.games) upcomingEvents.push({ summary: rootGames.title, location: rootGames.game_spotlight.stadium, - description: "Haifa vs. " + rootGames.game_spotlight.teams[this.getHaifaIndex(rootGames.game_spotlight.teams)].name, + description: "Haifa vs. " + rootGames.game_spotlight.teams[this.getOpponentIndexByStadium(rootGames.game_spotlight.stadium)].name, start: { - dateTime: '2023-03-22T09:00:00-07:00', - timeZone: 'Israel' + dateTime: `2023-03-22T09:00:00`, + timeZone: 'Asia/Jerusalem' }, end: { - dateTime: '2023-03-22T09:00:00-07:00', - timeZone: 'Israel' - }, - recurrence: [] + dateTime: `2023-03-22T11:00:00`, + timeZone: 'Asia/Jerusalem' + } }); - console.log("Updaing new event: " + upcomingEvents[0].summary) - console.log(upcomingEvents[0]) - this.googleCalendar.updateNewEvent(upcomingEvents); + + console.log(rootGames.game_spotlight) + // console.log("DONE WITH UPDATING NEW EVENTS") + + + // rootGames.games.forEach((game: any) => { + // console.log("GAME", game) + // upcomingEvents.push({ + // summary: 'Maccabi Haifa F.C.', + // location: game.stadium, + // description: "Haifa vs. " + game.teams[this.getOpponentIndexByStadium(game.stadium)].name, + // start: { + // dateTime: `${game.date} ${game.time}`, + // timeZone: 'Asia/Jerusalem' + // }, + // end: { + // dateTime: `${game.date} ${game.time}`, + // timeZone: 'Asia/Jerusalem' + // } + // }); + // }); + // console.log("UPCOMING EVENTS", upcomingEvents) + + // this.googleCalendar.updateNewEvent(upcomingEvents); }); } - getHaifaIndex(teams: any) { - for (let i = 0; i < teams.length; i++) { - if (teams[i].name === "Maccabi Haifa") { - return i; - } + getOpponentIndexByStadium(stadium: string) { + if (stadium === "Sammy Ofer Stadium") { + return 1; + } else { + return 0; } } } const app = new App(); -app.init(); -app.getNewGamesAndUpdateCalendar(); + +const start = async () => { + await app.init(); + await app.getNewGamesAndUpdateCalendar(); +} +start(); diff --git a/src/types/index.ts b/src/types/index.ts index 51b1b78..6111f8e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -10,5 +10,4 @@ export interface GoogleCalendarEvent { dateTime: string; timeZone: string; }; - recurrence: string[]; } \ No newline at end of file