-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ts
98 lines (86 loc) · 2.51 KB
/
script.ts
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
import "https://deno.land/x/dotenv@v3.2.0/load.ts";
import * as path from "https://deno.land/std@0.172.0/path/mod.ts";
import { Client } from "npm:@notionhq/client@2.2.3";
import { google } from "npm:googleapis@105";
import { authenticate } from "npm:@google-cloud/local-auth@2.1.0";
const SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"];
const TOKEN_PATH = path.join(Deno.cwd(), "token.json");
const CREDENTIALS_PATH = path.join(Deno.cwd(), "credentials.json");
async function loadSavedCredentialsIfExist() {
try {
const content = await Deno.readTextFile(TOKEN_PATH);
const credentials = JSON.parse(content);
return google.auth.fromJSON(credentials);
} catch (err) {
return null;
}
}
async function saveCredentials(client: any) {
const content = await Deno.readTextFile(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 Deno.writeTextFile(TOKEN_PATH, payload);
}
async function authorize() {
const client = await loadSavedCredentialsIfExist();
if (client) {
return client;
}
const OAuth2Client = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
if (OAuth2Client?.credentials) {
await saveCredentials(client);
}
return OAuth2Client;
}
async function listEvents(auth: any) {
const calendar = google.calendar({ version: "v3", auth });
const res = await calendar.events.list({
calendarId: "primary",
timeMin: new Date().toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: "startTime",
});
const events = res.data.items;
if (!events || events.length === 0) {
console.log("No upcoming events found.");
return;
}
console.log("Upcoming 10 events:");
events.map((event, i) => {
const start = event?.start?.dateTime || event?.start?.date;
console.log(`${start} - ${event.summary}`);
});
}
authorize().then(listEvents).catch(console.error);
const databaseId = Deno.env.get("NOTION_DATABASE_ID") ?? "";
const notion = new Client({
auth: Deno.env.get("NOTION_TOKEN"),
});
const createPage = await notion.pages.create({
parent: {
database_id: databaseId,
},
properties: {
title: [
{
text: {
content: "New Event",
},
},
],
Date: {
start: new Date().toISOString(),
},
},
});
console.log(createPage);