made my first insert WIP
This commit is contained in:
parent
0d6b530057
commit
f42ce1e319
4 changed files with 81 additions and 49 deletions
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
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) {
|
||||
upcomingEvents.forEach((event: GoogleCalendarEvent) => {
|
||||
console.log("UPDATE NEW EVENT", upcomingEvents)
|
||||
|
||||
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;
|
||||
}
|
||||
console.log('Event created: %s', event.htmlLink);
|
||||
console.log(event.description + ' created');
|
||||
});
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
|
65
src/index.ts
65
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();
|
||||
|
|
|
@ -10,5 +10,4 @@ export interface GoogleCalendarEvent {
|
|||
dateTime: string;
|
||||
timeZone: string;
|
||||
};
|
||||
recurrence: string[];
|
||||
}
|
Loading…
Reference in a new issue