-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.js
150 lines (130 loc) · 4.16 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// TODO: share some thoughts from calendar recordings, maybe a color/emotion or a new calendar for software ideas
const fs = require('fs')
const path = require('path')
const assert = require('assert');
const util = require('util');
const { google } = require('googleapis');
const { authorize } = require('./authorize.js')
const ical = require('node-ical');
let calendarList = [], lastCalendar;
const CALENDAR_NAMES = [
'Diet', 'Emotions', 'General', 'Iga',
'Predictions', 'Revelation', 'Robot do'
]
const PUBLIC_CALENDARS = [
'Diet', 'General', 'Revelation', 'Robot do', 'Predictions'
]
const HOMEPATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
async function filterCalendar(options) {
let rexexp = new RegExp(options.calendarId, 'ig');
let matches = calendarList
.filter(c => c.id == options.calendarId);
if (matches.length == 0) {
matches = calendarList
.filter(c => c.summary.match(rexexp));
}
assert(matches.length > 0, 'something is seriously wrong with the calendarId ' + JSON.stringify(options, null, 4) + JSON.stringify(calendarList, null, 4));
if (lastCalendar !== matches[0].summary) {
lastCalendar = matches[0].summary;
console.log('Using calendar: ' + matches[0].summary
+ ' - ' + matches[0].id);
}
options.calendarId = matches[0].id;
return options
}
let calendar
async function correctCalendarId(options) {
if (typeof options.calendarId === 'undefined' || options.calendarId === 'primary') {
return Promise.resolve(Object.assign(options, {
calendarId: 'primary'
}))
}
if (calendarList.length > 0) {
return filterCalendar(options);
}
let r
if (calendarList.length == 0) {
if (!calendar) {
let client = authorize()
calendar = google.calendar({ version: 'v3', auth: client })
}
r = util.promisify(calendar.calendarList.list.bind(calendar))()
} else {
r = calendarList
}
calendarList = (r.data || {}).items || []
return await filterCalendar(options)
}
async function searchCalendar(search, calendarId) {
const options = {}
if (calendarId) {
options.calendarId = calendarId;
}
if (!options.auth) {
options.auth = await authorize(options.scopes)
}
calendar = google.calendar({ version: 'v3', auth: options.auth })
await correctCalendarId(options)
let searchTerms = search.split('|')
let events = []
for (let i = 0; i < searchTerms.length; i++) {
let r = await listEvents({
auth: options.auth,
calendarId: options.calendarId,
q: searchTerms[i]
})
console.log(term)
console.log(r.map(e => e.event.summary).slice(0, 10))
events.push.apply(events, r)
}
return events
}
async function listCalendar() {
//let events = await searchCalendar('brainstorm', 'primary')
//console.log(events)
// TODO: scan events from .ical calendar export type, instead of relying on Google calendar services
// find latest takeout items
const takeouts = fs.readdirSync(path.join(HOMEPATH, 'Downloads'))
.filter(dir => {
return dir && dir.startsWith('Takeout')
&& fs.existsSync(path.join(HOMEPATH, 'Downloads', dir, 'Calendar')) // /General.ics
})
.map(dir => path.join(HOMEPATH, 'Downloads', dir, 'Calendar'))
takeouts.sort((a, b) => fs.statSync(b).mtime - fs.statSync(a).mtime)
const calendarList = CALENDAR_NAMES.reduce((obj, key) => {
obj[key] = []
return obj
}, {})
let eventId = 1
//
console.log(takeouts[0])
let calendarFiles = fs.readdirSync(takeouts[0])
.filter(fname => {
return CALENDAR_NAMES.filter(ical => fname.startsWith(ical)
&& fname.endsWith('.ics')).length > 0
})
.map(fname => path.join(takeouts[0], fname))
let calendarEvents = calendarFiles.map((fname, id) => {
if (!fs.existsSync(fname)) {
return
}
let publicName = path.basename(fname).trim().replace(/(_.*)?\.ics$/ig, '')
const events = Object.values(ical.sync.parseFile(fname));
let eventsList = []
for (const event of events) {
if(PUBLIC_CALENDARS.includes(publicName)) {
event.content = (event.summary || '') + '\n' + (event.description || '')
} else {
event.content = publicName
}
event.name = publicName.replace(/[^a-z]/ig, '_')
eventsList[eventsList.length] = event
}
return eventsList
})
return calendarEvents
}
module.exports = {
searchCalendar,
listCalendar
}