wip - got login token , by still can't insert new event
This commit is contained in:
parent
d6bf514d54
commit
4d0423ddce
6 changed files with 114 additions and 69 deletions
|
@ -5,7 +5,7 @@
|
||||||
"googleapis": "^113.0.0"
|
"googleapis": "^113.0.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "nodemon public/index.js"
|
"dev": "nodemon --watch insted --ignore tmp/ public/index.js"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.15.5"
|
"@types/node": "^18.15.5"
|
||||||
|
|
|
@ -4,14 +4,11 @@ import axios from "axios";
|
||||||
// search for upcomming games
|
// search for upcomming games
|
||||||
|
|
||||||
export default class GameSource {
|
export default class GameSource {
|
||||||
|
constructor() {}
|
||||||
|
|
||||||
constructor() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async getGames() {
|
async getGames() {
|
||||||
const sourceUrl = `https://serpapi.com/search.json?q=maccabi+haifa+next+games&api_key=${process.env.SERPAPI_KEY}&location=austin,+texas,+united+states`;
|
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)
|
const result = await axios.get(sourceUrl)
|
||||||
return result.data;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,52 +1,59 @@
|
||||||
|
import { JWT } from 'google-auth-library';
|
||||||
|
import { google } from 'googleapis';
|
||||||
|
import { GoogleCalendarEvent } from './types/index';
|
||||||
|
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const env = process.env;
|
const env = process.env;
|
||||||
import { google } from 'googleapis';
|
|
||||||
|
|
||||||
export default class GoogleCalendar {
|
export default class GoogleCalendar {
|
||||||
connected: boolean;
|
client_secret: string;
|
||||||
|
client_id: string;
|
||||||
|
calender_id: string;
|
||||||
calendar: any;
|
calendar: any;
|
||||||
googlePrivateKey = env.GOOGLE_PRIVATE_KEY
|
clientEmail: string;
|
||||||
googleClientEmail = env.GOOGLE_CLIENT_EMAIL
|
googlePrivateKey: string;
|
||||||
googleProjectNumber = env.GOOGLE_PROJECT_NUMBER
|
token: any;
|
||||||
googleCalendarId = env.GOOGLE_CALENDAR_ID
|
|
||||||
projectScope = 'https://www.googleapis.com/auth/calendar.readonly'
|
|
||||||
connection: any;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const SCOPES = this.projectScope;
|
this.client_secret = env.GOOGLE_CLIENT_SECRET;
|
||||||
const jwtClient = new google.auth.JWT(
|
this.client_id = env.GOOGLE_CLIENT_ID;
|
||||||
this.googleClientEmail,
|
this.calender_id = env.GOOGLE_CALENDAR_ID;
|
||||||
null,
|
this.clientEmail = env.GOOGLE_CLIENT_EMAIL
|
||||||
this.googlePrivateKey,
|
this.googlePrivateKey = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n');
|
||||||
SCOPES
|
// this.redirect_uris = env.GOOGLE_REDIRECT_URIS;
|
||||||
);
|
|
||||||
this.connection = jwtClient;
|
|
||||||
if(this.connection) {
|
|
||||||
this.connected = true;
|
|
||||||
} else {
|
|
||||||
this.connected = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
insert_new_event_in_calender(event: any) {
|
async init() {
|
||||||
const calendar = google.calendar({ version: 'v3', auth: this.connection });
|
const jwtClient = await this.authorize();
|
||||||
const options = {
|
this.calendar = google.calendar({ version: 'v3', auth: jwtClient });
|
||||||
auth: this.connection,
|
}
|
||||||
calendarId: this.googleCalendarId,
|
|
||||||
date: event.date,
|
|
||||||
// resource: event,
|
|
||||||
};
|
|
||||||
|
|
||||||
const callback = (err: any, res: any) => {
|
async authorize() {
|
||||||
|
const client = new JWT({
|
||||||
|
email: this.clientEmail,
|
||||||
|
key: this.googlePrivateKey,
|
||||||
|
scopes: 'https://www.googleapis.com/auth/calendar.events'
|
||||||
|
});
|
||||||
|
const { access_token } = await client.authorize();
|
||||||
|
this.token = access_token;
|
||||||
|
if(!this.token) {
|
||||||
|
throw new Error('Failed to connect to google calendar');
|
||||||
|
}
|
||||||
|
return 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) {
|
if (err) {
|
||||||
console.log(err);
|
console.log('There was an error contacting the Calendar service: ' + err);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
console.log(res);
|
console.log('Event created: %s', event.htmlLink);
|
||||||
}
|
});
|
||||||
calendar.events.insert(options, callback);
|
|
||||||
console.log("Successfully Inserted Event To Google Calendar")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
72
src/index.ts
72
src/index.ts
|
@ -1,35 +1,63 @@
|
||||||
import GameSource from "./GameSource";
|
import GoogleCalendar from './GoogleCalendar';
|
||||||
import GoogleCalendar from './GoogleCalendar'
|
import GameSource from './GameSource';
|
||||||
|
import { GoogleCalendarEvent } from './types/index';
|
||||||
|
|
||||||
const gameSource = new GameSource();
|
|
||||||
const googleCalendar = new GoogleCalendar();
|
|
||||||
|
|
||||||
if(googleCalendar.connected) {
|
// crete App class
|
||||||
console.log("Successfully Connected To Google API");
|
// app class will be the main class of our application
|
||||||
} else {
|
// it will be collecting macabi haifa events from GameSource class
|
||||||
console.log("Failed To Connect To Google API");
|
// it will be responsible for update GameSource events in google calendar
|
||||||
}
|
// it will be responsible for update GameSource events in local file
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class App {
|
||||||
|
googleCalendar: GoogleCalendar;
|
||||||
|
gameSource: any;
|
||||||
|
googleToken: any;
|
||||||
|
|
||||||
// create an app that scan the output gameSource and add events to google calendar
|
|
||||||
export default class App {
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.run();
|
this.googleCalendar = new GoogleCalendar();
|
||||||
|
this.gameSource = new GameSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
async run() {
|
async init() {
|
||||||
const games = await gameSource.getGames();
|
this.googleToken = this.googleCalendar.init();
|
||||||
this.dropOutputToTmpFIle(games);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dropOutputToTmpFIle(games: any) {
|
getNewGamesAndUpdateCalendar() {
|
||||||
const fs = require('fs');
|
const upcomingEvents: GoogleCalendarEvent[] = [];
|
||||||
fs.writeFile("tmp/output.json", JSON.stringify(games), function(err: any) {
|
this.gameSource.getGames().then((games: any) => {
|
||||||
if(err) {
|
const rootGames = games.data.sports_results;
|
||||||
return console.log(err);
|
upcomingEvents.push({
|
||||||
}
|
summary: rootGames.title,
|
||||||
console.log("The file was saved!");
|
location: rootGames.game_spotlight.stadium,
|
||||||
|
description: "Haifa vs. " + rootGames.game_spotlight.teams[this.getHaifaIndex(rootGames.game_spotlight.teams)].name,
|
||||||
|
start: {
|
||||||
|
dateTime: '2023-03-22T09:00:00-07:00',
|
||||||
|
timeZone: 'Israel'
|
||||||
|
},
|
||||||
|
end: {
|
||||||
|
dateTime: '2023-03-22T09:00:00-07:00',
|
||||||
|
timeZone: 'Israel'
|
||||||
|
},
|
||||||
|
recurrence: []
|
||||||
|
});
|
||||||
|
console.log("Updaing new event: " + upcomingEvents[0].summary)
|
||||||
|
console.log(upcomingEvents[0])
|
||||||
|
this.googleCalendar.updateNewEvent(upcomingEvents);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getHaifaIndex(teams: any) {
|
||||||
|
for (let i = 0; i < teams.length; i++) {
|
||||||
|
if (teams[i].name === "Maccabi Haifa") {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = new App();
|
const app = new App();
|
||||||
|
app.init();
|
||||||
|
app.getNewGamesAndUpdateCalendar();
|
||||||
|
|
14
src/types/index.ts
Normal file
14
src/types/index.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
export interface GoogleCalendarEvent {
|
||||||
|
summary: string;
|
||||||
|
location: string;
|
||||||
|
description: string;
|
||||||
|
start: {
|
||||||
|
dateTime: string;
|
||||||
|
timeZone: string;
|
||||||
|
};
|
||||||
|
end: {
|
||||||
|
dateTime: string;
|
||||||
|
timeZone: string;
|
||||||
|
};
|
||||||
|
recurrence: string[];
|
||||||
|
}
|
|
@ -3,9 +3,8 @@
|
||||||
"module": "CommonJS",
|
"module": "CommonJS",
|
||||||
"allowSyntheticDefaultImports": true,
|
"allowSyntheticDefaultImports": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"target": "ES6",
|
"target": "ES2019",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"sourceMap": true,
|
|
||||||
"outDir": "./public",
|
"outDir": "./public",
|
||||||
"rootDir": "./src"
|
"rootDir": "./src"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue