made my first insert WIP

This commit is contained in:
Kfir Dayan 2023-03-22 16:19:53 +02:00
parent 0d6b530057
commit f42ce1e319
4 changed files with 81 additions and 49 deletions

View file

@ -7,6 +7,7 @@ export default class GameSource {
constructor() {} constructor() {}
async getGames() { 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 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; return result;

View file

@ -7,53 +7,58 @@ const env = process.env;
export default class GoogleCalendar { export default class GoogleCalendar {
client_secret: string; clientSecret: string = env.GOOGLE_CLIENT_SECRET;
client_id: string; clientId: string = env.GOOGLE_CLIENT_ID;
calender_id: string; calenderId: string = env.GOOGLE_CALENDAR_ID;
calendar: any; calendar: any;
clientEmail: string; clientEmail: string = env.GOOGLE_CLIENT_EMAIL;
googlePrivateKey: string; googlePrivateKey: string = env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, '\n');
token: any; token: any;
JWT_client: JWT;
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;
}
async init() { async init() {
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")
} }
async authorize() { async authorize() {
const client = new JWT({ console.log("AUTHORIZE GOOGLE CALENDAR")
this.JWT_client = new JWT({
email: this.clientEmail, email: this.clientEmail,
key: this.googlePrivateKey, 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; 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');
} }
return client; console.log("GOOGLE CALENDAR AUTHORIZED SUCCESSFULLY")
return this.JWT_client;
} }
updateNewEvent(upcomingEvents: GoogleCalendarEvent[]) { updateNewEvent(upcomingEvents: GoogleCalendarEvent[]) {
this.calendar.event.insert({ upcomingEvents.forEach((event: GoogleCalendarEvent) => {
auth: this.token, console.log("UPDATE NEW EVENT", upcomingEvents)
calendarId: this.calender_id,
resource: upcomingEvents[0], const options = {
}, function (err: any, event: any) { auth: this.JWT_client,
calendarId: this.calenderId,
resource: event,
}
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 created: %s', event.htmlLink); console.log(event.description + ' created');
}); });
})
} }
} }

View file

@ -22,42 +22,69 @@ class App {
} }
async init() { 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[] = []; const upcomingEvents: GoogleCalendarEvent[] = [];
this.gameSource.getGames().then((games: any) => { this.gameSource.getGames().then((games: any) => {
const rootGames = games.data.sports_results; const rootGames = games.data.sports_results;
console.log("ROOT GAMES", rootGames.games)
upcomingEvents.push({ upcomingEvents.push({
summary: rootGames.title, summary: rootGames.title,
location: rootGames.game_spotlight.stadium, 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: { start: {
dateTime: '2023-03-22T09:00:00-07:00', dateTime: `2023-03-22T09:00:00`,
timeZone: 'Israel' timeZone: 'Asia/Jerusalem'
}, },
end: { end: {
dateTime: '2023-03-22T09:00:00-07:00', dateTime: `2023-03-22T11:00:00`,
timeZone: 'Israel' timeZone: 'Asia/Jerusalem'
}, }
recurrence: []
}); });
console.log("Updaing new event: " + upcomingEvents[0].summary)
console.log(upcomingEvents[0]) console.log(rootGames.game_spotlight)
this.googleCalendar.updateNewEvent(upcomingEvents); // 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) { getOpponentIndexByStadium(stadium: string) {
for (let i = 0; i < teams.length; i++) { if (stadium === "Sammy Ofer Stadium") {
if (teams[i].name === "Maccabi Haifa") { return 1;
return i; } else {
} return 0;
} }
} }
} }
const app = new App(); const app = new App();
app.init();
app.getNewGamesAndUpdateCalendar(); const start = async () => {
await app.init();
await app.getNewGamesAndUpdateCalendar();
}
start();

View file

@ -10,5 +10,4 @@ export interface GoogleCalendarEvent {
dateTime: string; dateTime: string;
timeZone: string; timeZone: string;
}; };
recurrence: string[];
} }