-
Notifications
You must be signed in to change notification settings - Fork 6
/
calendar.js
106 lines (93 loc) · 3.01 KB
/
calendar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const fs = require('fs').promises;
const path = require('path');
const process = require('process');
const {authenticate} = require('@google-cloud/local-auth');
const {google} = require('googleapis')
const ical = require('node-ical');
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = path.join(process.cwd(), 'token.json');
const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json');
async function loadSavedCredentialsIfExist() {
try {
const content = await fs.readFile(TOKEN_PATH);
const credentials = JSON.parse(content);
return google.auth.fromJSON(credentials);
} catch (err) {
return null;
}
}
async function saveCredentials(client) {
const content = await fs.readFile(CREDENTIALS_PATH);
const keys = JSON.parse(content);
const key = keys.installed || keys.web;
const payload = JSON.stringify({
type: 'authorized_user',
client_id: key.client_id,
client_secret: key.client_secret,
refresh_token: client.credentials.refresh_token,
});
await fs.writeFile(TOKEN_PATH, payload);
}
async function authorize() {
let client = await loadSavedCredentialsIfExist();
if (client) {
return client;
}
client = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
if (client.credentials) {
await saveCredentials(client);
}
return client;
}
async function getEvents(auth) {
const today = new Date();
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
const calendar = google.calendar({version: 'v3', auth});
const res = await calendar.events.list({
calendarId: process.env.CALENDAR_ID || 'primary',
timeMin: today.toISOString(),
timeMax: tomorrow.toISOString(),
maxResults: 100,
singleEvents: true,
orderBy: 'startTime',
});
const events = res.data.items;
return events;
}
function getICalEvents(files) {
const today = new Date();
today.setHours(0,0,0,0);
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
let events = [];
files.forEach(file => {
const icalBody = ical.sync.parseFile(file);
const icalEvents = Object.keys(icalBody)
.filter((key) => icalBody[key].type === 'VEVENT')
.map((key) => icalBody[key])
.filter((event) => event.start >= today && event.end <= tomorrow)
.map((event) => {
let attendees = [];
if (Array.isArray(event.attendee))
attendees = event.attendee.map(attendee => ({email: attendee.params.CN}));
else
attendees.push({email: event.attendee.params.CN});
return {
summary: event.summary,
description: event.description,
attendees
};
});
events = events.concat(icalEvents);
});
return events;
}
module.exports = { authorize, getEvents, getICalEvents };