-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (144 loc) · 5.06 KB
/
index.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Calendar Combiner and Uploader
*
* This script performs the following operations:
* 1. Fetches events from multiple iCalendar (.ics) sources.
* 2. Combines these events into a single calendar.
* 3. Handles recurring events, expanding them for a one-year period.
* 4. Adjusts event times to the specified timezone.
* 5. Generates a new .ics file with the combined events.
* 6. Uploads the resulting .ics file to a DigitalOcean Space and makes it publicly accessible.
*
* The script uses:
* - ical-generator for creating the new calendar
* - axios for fetching calendar data
* - node-ical for parsing .ics files
* - rrule for handling recurring events
* - moment-timezone for timezone conversions
* - @aws-sdk/client-s3 for uploading to DigitalOcean Spaces
*
* The resulting .ics file is named using a URL-safe version of the calendar name
* and is made publicly accessible in the specified DigitalOcean Space.
*/
const ical = require("ical-generator").default;
const axios = require("axios");
const nodeIcal = require("node-ical");
const { RRule } = require("rrule");
const moment = require("moment-timezone");
const { S3, PutObjectCommand } = require("@aws-sdk/client-s3");
// Calendar configuration
const calendarName = "JohnDoe is EXTRAORDINARILY BUSY"; // This will be the name of the calendar in the .ics file
const calendarTimezone = "Europe/Madrid"; // https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
const calendarInputUrls = [
yepcode.env.PERSONAL_CALENDAR_URL,
yepcode.env.WORK_CALENDAR_URL,
];
// Fetch calendar data from a given URL
async function fetchCalendar(url) {
const response = await axios.get(url);
return nodeIcal.async.parseICS(response.data);
}
// Adjust date to the specified timezone
function adjustDate(date, timezone) {
return moment.tz(date, timezone).toDate();
}
// Expand recurring events within a given date range
function expandRecurringEvent(event, start, end) {
if (!event.rrule) return [event];
const rruleOptions = RRule.parseString(event.rrule.toString());
rruleOptions.dtstart = moment(event.start).toDate();
const rule = new RRule(rruleOptions);
const dates = rule.between(start, end);
const duration = moment(event.end).diff(moment(event.start));
return dates.map((date) => {
const adjustedStart = moment(date);
return {
...event,
start: adjustedStart.toDate(),
end: adjustedStart.add(duration, "milliseconds").toDate(),
};
});
}
// Function to create a URL-safe string
function createUrlSafeString(str) {
return str
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
}
// Main function to combine calendars
async function combineCalendars() {
// Fetch all calendars
const calendarEventsArray = await Promise.all(
calendarInputUrls.map(async (url) => fetchCalendar(url))
);
// Create a new calendar
const cal = ical({ name: calendarName });
// Set date range for events (1 year from now)
const start = moment().tz(calendarTimezone).startOf("day").toDate();
const end = moment()
.tz(calendarTimezone)
.add(1, "year")
.endOf("day")
.toDate();
// Process all events from all calendars
calendarEventsArray
.map((events) => Object.values(events))
.flat()
.forEach((event) => {
if (event.type === "VEVENT") {
const expandedEvents = expandRecurringEvent(event, start, end);
expandedEvents.forEach((expandedEvent) => {
const adjustedStart = adjustDate(
expandedEvent.start,
event.start.tz || calendarTimezone
);
const adjustedEnd = adjustDate(
expandedEvent.end,
event.end.tz || calendarTimezone
);
cal.createEvent({
start: adjustedStart,
end: adjustedEnd,
summary: expandedEvent.summary,
description: expandedEvent.description,
location: expandedEvent.location,
});
});
}
});
return cal.toString();
}
// Configure S3 client for DigitalOcean Spaces
const s3Client = new S3({
forcePathStyle: false,
endpoint: yepcode.env.DIGITALOCEAN_SPACES_ENDPOINT,
region: yepcode.env.DIGITALOCEAN_SPACES_REGION,
credentials: {
accessKeyId: yepcode.env.DIGITALOCEAN_SPACES_KEY,
secretAccessKey: yepcode.env.DIGITALOCEAN_SPACES_SECRET,
},
});
// Main execution
(async () => {
try {
// Combine calendars
const combinedCalendar = await combineCalendars();
// Create URL-safe key from calendar name
const urlSafeKey = `${createUrlSafeString(calendarName)}.ics`;
// Prepare the upload command
const putObjectCommand = new PutObjectCommand({
Bucket: yepcode.env.DIGITALOCEAN_SPACES_BUCKET,
Key: urlSafeKey,
Body: combinedCalendar,
ACL: "public-read",
ContentType: "text/calendar",
});
// Upload to DigitalOcean Spaces
const result = await s3Client.send(putObjectCommand);
console.log("File uploaded successfully:", result);
console.log("File key:", urlSafeKey);
} catch (error) {
console.error("Error in calendar merging or uploading:", error);
}
})();