-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotion.js
More file actions
293 lines (251 loc) · 9.52 KB
/
Notion.js
File metadata and controls
293 lines (251 loc) · 9.52 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Cache keys and default location
const CACHE_KEY_LAST_UPDATED = 'last_updated';
const CACHE_KEY_LOCATION = 'location';
const DEFAULT_LOCATION = { latitude: 0, longitude: 0 };
// Font name and size
const FONT_NAME = 'Arial';
const TITLE_FONT_SIZE = 10;
const FONT_SIZE = 15;
// Colors
const COLORS = {
bg: '#191919',
title: '#D5D5D5',
body: '#D5D5D5',
};
// TODO: PLEASE SET THESE VALUES
const NAME = '';
const WEATHER_API_KEY = ''; // https://home.openweathermap.org/api_keys (account needed)
const WORK_CALENDAR_NAME = '';
const PERSONAL_CALENDAR_NAME = '';
/******************************************************************************
* Initial Setups
*****************************************************************************/
/**
* Convenience function to add days to a Date.
*
* @param {*} days The number of days to add
*/
// Import and setup Cache
const Cache = importModule('Cache');
const cache = new Cache('Notion');
// Fetch data and create widget
const data = await fetchData();
const widget = createWidget(data);
if (config.runsInApp) {
widget.presentMedium();
}
Script.setWidget(widget);
Script.complete();
/******************************************************************************
* Main Functions (Widget and Data-Fetching)
*****************************************************************************/
/**
* Main widget function.
*
* @param {} data The data for the widget to display
*/
function createWidget(data) {
console.log(`Creating widget with data: ${JSON.stringify(data)}`);
const widget = new ListWidget();
widget.backgroundColor = new Color(COLORS.bg);
widget.setPadding(1, 10, 1, 10);
const stack = widget.addStack();
stack.layoutVertically();
stack.spacing = 13;
stack.size = new Size(320, 0);
const stack1 = widget.addStack();
stack1.layoutHorizontally();
stack1.spacing = 13;
stack1.size = new Size(320, 0);
// Line 0 - Title
const titleLine = stack.addText(`MESSAGE`);
titleLine.textColor = new Color(COLORS.title);
titleLine.font = new Font('Arial-BoldMT', TITLE_FONT_SIZE);
titleLine.url = 'notion://'
// Line 1 - Next Work Calendar Event
const nextWorkCalendarEventLine = stack.addText(` 📌 ${getCalendarEventTitle(data.nextWorkEvent, true)}`);
nextWorkCalendarEventLine.textColor = new Color(COLORS.body);
nextWorkCalendarEventLine.font = new Font(FONT_NAME, FONT_SIZE);
nextWorkCalendarEventLine.url = "calshow://"
// Line 2 - Next Personal Calendar Event
const nextPersonalCalendarEventLine = stack.addText(` 📘 ${getCalendarEventTitle(data.nextPersonalEvent, false)}`);
nextPersonalCalendarEventLine.textColor = new Color(COLORS.body);
nextPersonalCalendarEventLine.font = new Font(FONT_NAME, FONT_SIZE);
nextPersonalCalendarEventLine.url = "calshow://"
// Line 3 - Weather
const weatherLine = stack.addText(` ${data.weather.icon} ${parseInt((data.weather.temperature - 32) / 1.8)}c Scope : ${parseInt((data.weather.high - 32) / 1.8)}c to ${parseInt((data.weather.low - 32) / 1.8)}c`);
weatherLine.textColor = new Color(COLORS.body);
weatherLine.font = new Font(FONT_NAME, FONT_SIZE);
weatherLine.url = "weather://"
// Add button to open documentation
let linkSymbol = SFSymbol.named("arrow.up.forward")
let footerStack = widget.addStack()
footerStack.addSpacer(290)
// Add link to documentation
let docsSymbol = SFSymbol.named("book")
let docsElement = footerStack.addImage(docsSymbol.image)
docsElement.imageSize = new Size(25, 15)
docsElement.tintColor = Color.white()
docsElement.imageOpacity = 1
docsElement.url = ""
return widget;
}
/**
* Fetch pieces of data for the widget.
*/
async function fetchData() {
// Get the weather data
const weather = await fetchWeather();
// Get next work/personal calendar events
const nextWorkEvent = await fetchNextCalendarEvent(WORK_CALENDAR_NAME);
const nextPersonalEvent = await fetchNextCalendarEvent(PERSONAL_CALENDAR_NAME);
return {
weather,
nextWorkEvent,
nextPersonalEvent,
};
}
/******************************************************************************
* Helper Functions
*****************************************************************************/
//-------------------------------------
// Weather Helper Functions
//-------------------------------------
/**
* Fetch the weather data from Open Weather Map
*/
async function fetchWeather() {
let location = await cache.read(CACHE_KEY_LOCATION);
if (!location) {
try {
Location.setAccuracyToThreeKilometers();
location = await Location.current();
}
catch (error) {
location = await cache.read(CACHE_KEY_LOCATION);
}
}
if (!location) {
location = DEFAULT_LOCATION;
}
const url = "https://api.openweathermap.org/data/2.5/onecall?lat=" + location.latitude + "&lon=" + location.longitude + "&exclude=minutely,hourly,alerts&units=imperial&lang=en&appid=" + WEATHER_API_KEY;
const address = await Location.reverseGeocode(location.latitude, location.longitude);
const data = await fetchJson(url);
const cityState = `${address[0].postalAddress.city}, ${address[0].postalAddress.state}`;
if (!data) {
return {
location: cityState,
icon: '❓',
description: 'Unknown',
temperature: '?',
wind: '?',
high: '?',
low: '?',
feelsLike: '?',
}
}
const currentTime = new Date().getTime() / 1000;
const isNight = currentTime >= data.current.sunset || currentTime <= data.current.sunrise
return {
location: cityState,
icon: getWeatherEmoji(data.current.weather[0].id, isNight),
description: data.current.weather[0].main,
temperature: Math.round(data.current.temp),
wind: Math.round(data.current.wind_speed),
high: Math.round(data.daily[0].temp.max),
low: Math.round(data.daily[0].temp.min),
feelsLike: Math.round(data.current.feels_like),
}
}
/**
* Given a weather code from Open Weather Map, determine the best emoji to show.
*
* @param {*} code Weather code from Open Weather Map
* @param {*} isNight Is `true` if it is after sunset and before sunrise
*/
function getWeatherEmoji(code, isNight) {
if (code >= 200 && code < 300 || code == 960 || code == 961) {
return "⛈"
} else if ((code >= 300 && code < 600) || code == 701) {
return "🌧"
} else if (code >= 600 && code < 700) {
return "❄️"
} else if (code == 711) {
return ""
} else if (code == 800) {
return isNight ? "🌕" : "️"
} else if (code == 801) {
return isNight ? "️" : "🌤"
} else if (code == 802) {
return isNight ? "️" : "⛅️"
} else if (code == 803) {
return isNight ? "️" : "🌥"
} else if (code == 804) {
return "️"
} else if (code == 900 || code == 962 || code == 781) {
return "🌪"
} else if (code >= 700 && code < 800) {
return "🌫"
} else if (code == 903) {
return "🥶"
} else if (code == 904) {
return "🥵"
} else if (code == 905 || code == 957) {
return ""
} else if (code == 906 || code == 958 || code == 959) {
return "🧊"
} else {
return "❓"
}
}
//-------------------------------------
// Calendar Helper Functions
//-------------------------------------
/**
* Fetch the next calendar event from the given calendar
*
* @param {*} calendarName The calendar to get events from
*/
async function fetchNextCalendarEvent(calendarName) {
const calendar = await Calendar.forEventsByTitle(calendarName);
const events = await CalendarEvent.today([calendar]);
const tomorrow = await CalendarEvent.tomorrow([calendar]);
console.log(`Got ${events.length} events for ${calendarName}`);
console.log(`Got ${tomorrow.length} events for ${calendarName} tomorrow`);
const upcomingEvents = events.concat(tomorrow).filter(e => (new Date(e.endDate)).getTime() >= (new Date()).getTime());
return upcomingEvents ? upcomingEvents[0] : null;
}
/**
* Given a calendar event, return the display text with title and time.
*
* @param {*} calendarEvent The calendar event
* @param {*} isWorkEvent Is this a work event?
*/
function getCalendarEventTitle(calendarEvent, isWorkEvent) {
if (!calendarEvent) {
return `No upcoming ${isWorkEvent ? 'lessons' : 'events'}`;
}
const timeFormatter = new DateFormatter();
timeFormatter.locale = 'it';
timeFormatter.useNoDateStyle();
timeFormatter.useShortTimeStyle();
const eventTime = new Date(calendarEvent.startDate);
return `${timeFormatter.string(eventTime)} ${calendarEvent.title}`;
}
/**
* Make a REST request and return the response
*
* @param {*} url URL to make the request to
* @param {*} headers Headers for the request
*/
async function fetchJson(url, headers) {
try {
console.log(`Fetching url: ${url}`);
const req = new Request(url);
req.headers = headers;
const resp = await req.loadJSON();
return resp;
} catch (error) {
console.error(`Error fetching from url: ${url}, error: ${JSON.stringify(error)}`);
}
}