remove_google_api_temporary

This commit is contained in:
Kfir Dayan 2023-04-04 13:03:10 +03:00
parent b6455824e6
commit 9547cedf4b
3 changed files with 69 additions and 70 deletions

View file

@ -4,7 +4,6 @@
"cron": "^2.3.0", "cron": "^2.3.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
"googleapis": "^113.0.0",
"ical": "^0.8.0", "ical": "^0.8.0",
"ics": "^3.1.0", "ics": "^3.1.0",
"moment": "^2.29.4", "moment": "^2.29.4",

View file

@ -1,65 +1,65 @@
import { JWT } from 'google-auth-library'; // import { JWT } from 'google-auth-library';
import { google } from 'googleapis'; // import { google } from 'googleapis';
import { GoogleCalendarEvent } from './types/index'; // import { GoogleCalendarEvent } from './types/index';
require('dotenv').config(); // require('dotenv').config();
const env = process.env; // const env = process.env;
export default class GoogleCalendar { // export default class GoogleCalendar {
clientSecret: string = env.GOOGLE_CLIENT_SECRET; // clientSecret: string = env.GOOGLE_CLIENT_SECRET;
clientId: string = env.GOOGLE_CLIENT_ID; // clientId: string = env.GOOGLE_CLIENT_ID;
calenderId: string = env.GOOGLE_CALENDAR_ID; // calenderId: string = env.GOOGLE_CALENDAR_ID;
calendar: any; // calendar: any;
clientEmail: string = env.GOOGLE_CLIENT_EMAIL; // clientEmail: string = env.GOOGLE_CLIENT_EMAIL;
googlePrivateKey: string = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n'); // googlePrivateKey: string = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n');
token: any; // token: any;
JWT_client: JWT; // JWT_client: JWT;
async init() { // async init() {
console.log("INIT GOOGLE CALENDAR") // console.log("INIT GOOGLE CALENDAR")
const jwtClient = await this.authorize(); // const jwtClient = await this.authorize();
this.calendar = google.calendar({ version: 'v3', auth: jwtClient }); // this.calendar = google.calendar({ version: 'v3', auth: jwtClient });
console.log("DONE INIT GOOGLE CALENDAR") // console.log("DONE INIT GOOGLE CALENDAR")
} // }
async authorize() { // async authorize() {
console.log("AUTHORIZE GOOGLE CALENDAR") // console.log("AUTHORIZE GOOGLE CALENDAR")
this.JWT_client = new JWT({ // this.JWT_client = new JWT({
email: this.clientEmail, // email: this.clientEmail,
key: this.googlePrivateKey, // key: this.googlePrivateKey,
scopes: [ // scopes: [
'https://www.googleapis.com/auth/calendar', // 'https://www.googleapis.com/auth/calendar',
] // ]
}); // });
const { access_token } = await this.JWT_client.authorize(); // const { access_token } = await this.JWT_client.authorize();
this.token = access_token; // this.token = access_token;
if (!this.token) { // if (!this.token) {
throw new Error('Failed to connect to google calendar'); // throw new Error('Failed to connect to google calendar');
} // }
console.log("GOOGLE CALENDAR AUTHORIZED SUCCESSFULLY") // console.log("GOOGLE CALENDAR AUTHORIZED SUCCESSFULLY")
return this.JWT_client; // return this.JWT_client;
} // }
async updateNewEvent(upcomingEvents: GoogleCalendarEvent[]) { // async updateNewEvent(upcomingEvents: GoogleCalendarEvent[]) {
setTimeout(async () => { // setTimeout(async () => {
upcomingEvents.forEach((event: GoogleCalendarEvent) => { // upcomingEvents.forEach((event: GoogleCalendarEvent) => {
console.log("UPDATE NEW EVENT", upcomingEvents) // console.log("UPDATE NEW EVENT", upcomingEvents)
const options = { // const options = {
auth: this.JWT_client, // auth: this.JWT_client,
calendarId: this.calenderId, // calendarId: this.calenderId,
resource: event, // resource: event,
} // }
this.calendar.events.insert(options, function (err: any, event: any) { // this.calendar.events.insert(options, function (err: any, event: any) {
if (err) { // if (err) {
console.log('There was an error contacting the Calendar service: ' + err); // console.log('There was an error contacting the Calendar service: ' + err);
return; // return;
} // }
console.log(event.description + ' created'); // console.log(event.description + ' created');
}); // });
}) // })
}, 3000) // }, 3000)
} // }
} // }

View file

@ -1,4 +1,4 @@
import GoogleCalendar from './GoogleCalendar'; // import GoogleCalendar from './GoogleCalendar';
import GameSource from './GameSource'; import GameSource from './GameSource';
import fs from 'fs'; import fs from 'fs';
import Ics from './Ics'; import Ics from './Ics';
@ -8,28 +8,28 @@ import env from 'dotenv';
env.config(); env.config();
class App { class App {
googleCalendar: GoogleCalendar; // googleCalendar: GoogleCalendar;
gameSource: GameSource; gameSource: GameSource;
ics: Ics; ics: Ics;
googleToken: string; googleToken: string;
constructor() { constructor() {
this.googleCalendar = new GoogleCalendar(); // this.googleCalendar = new GoogleCalendar();
this.gameSource = new GameSource(); this.gameSource = new GameSource();
this.ics = new Ics(); this.ics = new Ics();
} }
async init() { // async init() {
console.log("INIT APP") // console.log("INIT APP")
await this.googleCalendar.init(); // await this.googleCalendar.init();
} // }
async getNewGamesAndUpdateCalendar() { // async getNewGamesAndUpdateCalendar() {
console.log("GET NEW GAMES AND UPDATE CALENDAR") // console.log("GET NEW GAMES AND UPDATE CALENDAR")
const games = await this.gameSource.getGamesFromHaifa(); // const games = await this.gameSource.getGamesFromHaifa();
this.googleCalendar.updateNewEvent(games); // this.googleCalendar.updateNewEvent(games);
} // }
async startCronJob() { async startCronJob() {
console.log("START CRON JOB") console.log("START CRON JOB")