This repository has been archived by the owner on Jan 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.js
146 lines (120 loc) · 3.89 KB
/
convert.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
#!/usr/bin/env node
var fs=require("fs");
var path=require("path");
var icalToolkit=require("ical-toolkit");
var args=(function() {
return process.argv.slice(2).reduce(function(args, x) {
if (x[0]==="-") { var i=x.indexOf("="); args[x.substr(1, i>0?i-1:undefined)]=i>0?x.substr(i+1):true; }
else args.params.push(x);
return args;
}, { params: [] });
})();
function convert(wuPath, rtmPath, wuPath2) {
var icsText=fs.readFileSync(rtmPath, "utf8");
var data=JSON.parse(fs.readFileSync(wuPath, "utf8"));
var user=data.user;
var lists={};
data.data.lists.forEach(x=> {
lists[x.title.toLowerCase()]=x.id;
});
console.log("Converting the following tags to lists: "+Object.keys(lists).join(", "));
function mkTime(str) {
if (str) return str.substr(0, 4)+"-"+str.substr(4, 2)+"-"+str.substr(6, 2);
else return null;
}
var reminders=[], tasks=[], notes=[];
var id=1200000001, rid=80000001;
icalToolkit.parseToJSON(icsText, function (err, json) {
if (err) throw err;
var todo=json.VCALENDAR[0].VTODO;
todo.forEach(x => {
var title=x.SUMMARY;
var desc=x.DESCRIPTION; // 'Time estimate: none\\nTags: none\\nLocation: none\\n\\n',
var prio=x.PRIORITY; // 1-3
var due=x["DUE;VALUE=DATE"];
var repeat=x.RRULE; // FREQ=DAILY;INTERVAL=4
if (x.STATUS==="COMPLETED") return;
desc=desc.split("\\n");
var tags=desc[1].substr(6).split("\\, ");
var freq=(/FREQ=([^;]*)/.exec(repeat)||[0,""])[1];
var interval=(/INTERVAL=([^;]*)/.exec(repeat)||[0,1])[1];
var listId=lists["inbox"];
tags.forEach((x, i) => {
if (lists[x]) {
listId=lists[x];
tags.splice(i, 1);
}
});
tags.forEach((x) => { title+=" #"+x; });
var reminder;
var add={
"completed": false,
"created_by_id": user,
"id": id,
"list_id": listId,
"revision": 1,
"starred": false,
"title": title,
"type": "task"
};
if (due) {
add.due_date=mkTime(due);
reminder={
"date": mkTime(due)+"T07:00:00.000Z",
"id": rid++,
"revision": 1,
"task_id": id,
"type": "reminder",
};
}
// check dups
if (!args.nodup && data.data.tasks.find(x => x.title===add.title && x.due_date==add.due_date && x.list_id===add.list_id))
return;
id++;
if (freq) {
add.recurrence_count=parseInt(interval, 10);
switch (freq) {
case "YEARLY": add.recurrence_type="year"; break;
case "MONTHLY": add.recurrence_type="month"; break;
case "WEEKLY": add.recurrence_type="week"; break;
case "DAILY": add.recurrence_type="day"; break;
default: throw new Error(freq);
}
}
if (desc[4]==="---") {
console.log(desc);
desc=desc.slice(5).join("\n");
var note= {
"content": desc,
"id": rid++,
"revision": 1,
"task_id": id,
"type": "note"
};
notes.push(note);
}
tasks.push(add);
if (reminder) reminders.push(reminder);
});
data.data.tasks=tasks;
data.data.notes=notes;
data.data.reminders=reminders;
data.data.subtasks=[];
data.data.task_positions=[];
data.data.subtask_positions=[];
fs.writeFileSync(wuPath2, JSON.stringify(data), "utf8");
console.log("Found "+tasks.length+" open tasks");
Object.keys(lists).forEach(name=> {
var list=tasks.filter(x => x.list_id===lists[name]);
console.log(name+" ("+list.length+")");
list.forEach(x => console.log(" - "+x.title));
});
});
}
if (args.params.length<3) {
console.log("usage: wundermilk [options] WUNDERLIST-IN ICS-IN WUNDERLIST-OUT");
console.log("The options are as follows:");
console.log("-nodup do not check for duplicates");
} else {
convert.apply(null, args.params);
}