-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReminderSync.js
337 lines (314 loc) · 10.3 KB
/
ReminderSync.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: list;
// Print reminders that are due for today to a file.
const key = "secret_EnterYourKeyHere";
const tasks_id = "TASK_PAGE_ID";
const proj_id = "PROJECT_PAGE_ID";
const json_template = {"Tomatos":{"id":"vIaw","type":"number","number":0},"Name":{"id":"title","type":"title","title":[{"annotations":{"code":false,"bold":false,"underline":false,"italic":false,"strikethrough":false,"color":"default"},"plain_text":" ","type":"text","href":null,"text":{"content":" ","link":null}}]},"Date":{"id":"%60D%3E%7B","type":"date","date":{"start":null,"end":null,"time_zone":null}},"Project":{"has_more":false,"id":"u%3D_%3D","relation":[],"type":"relation"},"Finish":{"id":"%40m%3Db","type":"checkbox","checkbox":true}};
class task{
constructor(reminder, proj=[], notion=false){
this.notion = notion;
this.update = false;
this.pair = false;
if (notion == false){
this.id = 'icloud';
this.name = reminder.title;
this.project = reminder.calendar.title;
if (reminder.notes != null) this.tomatos = reminder.notes.length/2;
else this.tomatos = 0;
if (reminder.dueDate != null) this.date = dateFormatter.string(reminder.dueDate);
else this.date = null;
this.date_start = this.date;
this.date_end = null;
this.finish = reminder.isCompleted;
this.json = JSON.parse(JSON.stringify(json_template));
};
if (notion == true){
this.id = reminder["id"];
let props = reminder["properties"];
this.json = props;
this.name = props["Name"]["title"][0]["text"]["content"];
this.project = null;
if (props["Project"]["relation"].length != 0) this.project = get_project_name(props["Project"]["relation"][0]["id"],proj);
this.tomatos = props["Tomatos"]["number"];
this.date = props["Date"]["date"];
this.finish = props["Finish"]["checkbox"];
if (this.date != null){
this.date_start = this.date["start"]
this.date_end = this.date["end"]
this.date = this.date_start;
}
}
}
initialize_json_icloud(proj){
this.json["Name"]["title"][0]["text"]["content"] = this.name;
if (this.project != null) {
var pro = get_project_id(this.project, proj);
if (pro != null) this.json["Project"]["relation"] = [{"id": get_project_id(this.project, proj)}];
}
this.json["Tomatos"]["number"] = this.tomatos;
if (this.date != null) {
this.json["Date"]["date"]["start"] = this.date_start;
this.json["Date"]["date"]["end"] = this.date_end;
}
else this.json["Date"] = {"id":"%60D%3E%7B","type":"date","date":null};
this.json["Finish"]["checkbox"] = this.finish;
}
update_json(){
this.json["Name"]["title"][0]["text"]["content"] = this.name;
this.json["Tomatos"]["number"] = this.tomatos;
if (this.date != null) {
this.json["Date"]["date"]["start"] = this.date;
this.json["Date"]["date"]["end"] = this.date_end;
}
else this.json["Date"] = {"id":"%60D%3E%7B","type":"date","date":null};
this.json["Finish"]["checkbox"] = this.finish;
}
async push_to_notion(){
const create_url = "https://api.notion.com/v1/pages";
const payload = {"parent": {"database_id": tasks_id}, "properties": this.json};
const db = new Request(create_url)
db.method = "POST"
db.headers = {
"Content-Type": "application/json",
"Notion-Version": "2022-06-28",
"Authorization": "Bearer " +key
};
db.body = JSON.stringify(payload);
var response = await db.loadJSON();
return response["results"];
}
async update_to_notion(){
if (this.update = false) return 0;
const url = "https://api.notion.com/v1/pages/"
const payload = {"properties": this.json}
const db = new Request(url+this.id)
db.method = "PATCH"
db.headers = {
"Content-Type": "application/json",
"Notion-Version": "2022-06-28",
"Authorization": "Bearer " +key
};
db.body = JSON.stringify(payload);
var response = await db.loadJSON();
return response["results"];
}
async delete_in_notion(){
const url = "https://api.notion.com/v1/pages/"
const payload = {"archived": true}
const db = new Request(url+this.id)
db.method = "PATCH"
db.headers = {
"Content-Type": "application/json",
"Notion-Version": "2022-06-28",
"Authorization": "Bearer " +key
};
db.body = JSON.stringify(payload);
var response = await db.loadJSON();
return response["results"];
}
async push_to_reminder(){
if (this.project == null){
if(this.date == null) this.project = 'Inbox';
else this.project = '@Context';
}
let parentCalendar = await Calendar.findOrCreateForReminders(this.project);
let newReminder = new Reminder();
newReminder.title = this.name
newReminder.calendar = parentCalendar;
newReminder.dueDate = dateIcloud.date(this.date+" 10:00");
newReminder.notes = tomatoStr(this.tomatos);
newReminder.isCompleted = this.finish;
newReminder.save();
return 0;
}
async update_to_reminder(){
if (this.update == false) return 0;
let parentCalendar = await Calendar.findOrCreateForReminders(this.project);
let remindersInCal = await Reminder.all([parentCalendar])
let iter = 0
while (iter < remindersInCal.length){
let iReminder = remindersInCal[iter];
++iter;
if (iReminder.title == this.name){
if (this.date == null) iReminder.dueDate == null;
else iReminder.dueDate = dateIcloud.date(this.date + " 10:00");
iReminder.notes = tomatoStr(this.tomatos);
iReminder.isCompleted = this.finish;
iReminder.save()
return 0;
}
continue
}
return 1;
}
async delete_in_reminder(){
this.finish = true;
this.update_to_reminder()
return 0;
}
isConsistent(secTask){
if(this.name != secTask.name) return false;
if(this.project == secTask.project) return true;
if((this.notion == true)&&(this.project == null)){
if (isProject(secTask.project,['Inbox', '@Context']) != true) return false;
}
if((this.notion == false)&&(isProject(this.project,['Inbox', '@Context']))){
if (secTask.project != null) return false;
}
if((this.date == null)&&(secTask.date == null)) return true;
if((this.date != null)){ if(this.date_start == secTask.date_start) return true;}
return false;
}
jsonString() {
let str = JSON.stringify(this.json);
return str;
}
}
//functions:
function iCloudTasks(reminders,proj) {
// Add reminders to the table.
task_list = [];
for (reminder of reminders) {
let itask = new task(reminder);
itask.initialize_json_icloud(proj);
task_list.push(itask);
}
return task_list;
}
async function get_pages(dbID){
const url = "https://api.notion.com/V1/databases/";
const db = new Request(url+dbID+"/query")
db.method = "POST"
db.headers = {
"Content-Type": "application/json",
"Notion-Version": "2022-06-28",
"Authorization": "Bearer " +key
};
var response = await db.loadJSON();
return response["results"];
};
function get_project_name(page_id, proj){
let projLen = proj.length;
let iter = 0;
while (iter < projLen){
if (page_id == proj[iter]["id"]){
return proj[iter]["properties"]["项目"]["title"][0]["plain_text"];
}
++iter;
}
return null;
};
function get_project_id(page_name,proj){
let projLen = proj.length;
let iter = 0;
while (iter < projLen){
if (page_name == proj[iter]["properties"]["项目"]["title"][0]["plain_text"]){
return proj[iter]["id"];
}
++iter;
}
return null;
}
function get_notion_tasks(pages, proj){
task_list = [];
let ind = 0;
while (ind < pages.length){
task_list.push(new task(pages[ind],proj,true))
++ind;
};
return task_list;
};
function tomatoStr(number) {
let str = "";
let iter = 0;
while (iter < number){
str += "🍅";
++ iter;
}
return str;
}
function isProject(project, projectList){
let iter = 0;
let length = projectList.length;
while (iter < length) {
if (project == projectList[iter]) return true;
else ++iter;
continue;
}
return false;
}
function updateTasks(task1, task2){
//task1 from iCloud, task2 from Notion
task1.pair = true;
task2.pair = true;
if (task1.tomatos != task2.tomatos){
task1.tomatos = task2.tomatos;
task1.update = true;
}
if (task1.date != task2.date){
if (task2.date == null) {
task1.date = task2.date;
task1.update = true;
}
else {task1.date = task2.date;
task1.update = true;}
}
if (task1.finish != task2.finish){
if (task1.finish == false) {
task1.finish = true;
task1.update = true;
}
else {
task2.finish = true;
task2.update = true;
}
}
if (task2.update) task2.update_json();
}
//Main Program
let dateFormatter = new DateFormatter();
dateFormatter.dateFormat="yyyy-MM-dd";
let dateIcloud = new DateFormatter();
dateIcloud.dateFormat="yyyy-MM-dd HH:mm";
//let output = FileManager.iCloud();
//let path = output.joinPath(output.documentsDirectory(),'remindersInfo.json');
//Collect Notion tasks:
let notionPages = await get_pages(tasks_id);
let notionProjs = await get_pages(proj_id);
let notionTaskList = await get_notion_tasks(notionPages, notionProjs)
//Collect iCloud Reminder tasks:
let calCateg = await Calendar.forReminders();
let icloudPages = await Reminder.all(calCateg);
let icloudTaskList = iCloudTasks(icloudPages,notionProjs);
//Compare iCloud tasks and Notion tasks:
let icloudIter = 0;
while (icloudIter < icloudTaskList.length){
let icloudTask = icloudTaskList[icloudIter];
++icloudIter;
if (isProject(icloudTask.project,["Someday / Maybe","Waiting for", "Daily / Weekly"])) continue;
let notionIter = 0;
while (notionIter < notionTaskList.length){
let notionTask = notionTaskList[notionIter];
++notionIter;
if(icloudTask.isConsistent(notionTask)){
updateTasks(icloudTask, notionTask);
icloudTask.update_to_reminder();
notionTask.update_to_notion();
break;
}
}
if ((!icloudTask.finish)&&(!icloudTask.pair)){
icloudTask.push_to_notion();
}
}
let notionIter = 0;
while (notionIter < notionTaskList.length){
let notionTask = notionTaskList[notionIter];
++notionIter;
if (notionTask.pair == true) continue;
if (notionTask.finish == true) continue;
notionTask.push_to_reminder();
}
Speech.speak('Finish Task Sync!')