forked from wayneashleyberry/wunderline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wunderline-export.js
executable file
·148 lines (129 loc) · 3.23 KB
/
wunderline-export.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
#!/usr/bin/env node
var app = require('commander')
var async = require('async')
var api = require('./lib/api')
var auth = require('./lib/auth')
app
.description('Export your data')
.option('--pretty', 'Pretty print output')
.parse(process.argv)
function showProgress () {
process.stderr.write('.')
}
function getUser (callback) {
api('/user', function (err, res, body) {
if (err) process.exit(1)
showProgress()
callback(null, {user: body})
})
}
function getLists (callback) {
api('/lists', function (err, res, body) {
if (err) process.exit(1)
showProgress()
callback(null, {lists: body})
})
}
function embedLists (data, callback) {
var lists = []
async.each(data.lists, function (list, complete) {
async.parallel([
function getTasks (cb) {
api({url: '/tasks', qs: {list_id: list.id}}, function (err, res, body) {
showProgress()
cb(err, body)
})
},
function getSubtasks (cb) {
api({url: '/subtasks', qs: {list_id: list.id}}, function (err, res, body) {
showProgress()
cb(err, body)
})
},
function getNotes (cb) {
api({url: '/notes', qs: {list_id: list.id}}, function (err, res, body) {
if (err) process.exit(1)
showProgress()
cb(err, body)
})
},
function getFiles (cb) {
api({url: '/files', qs: {list_id: list.id}}, function (err, res, body) {
showProgress()
cb(err, body)
})
}
], function (err, results) {
if (err) process.exit(1)
var tasks = results[0]
var subtasks = results[1]
var notes = results[2]
var files = results[3]
tasks.forEach(function (task, index) {
tasks[index].subtasks = []
tasks[index].notes = []
tasks[index].files = []
})
subtasks.forEach(function (subtask) {
tasks.forEach(function (task, index) {
if (task.id === subtask.task_id) {
tasks[index].subtasks.push(subtask)
}
})
})
notes.forEach(function (note) {
tasks.forEach(function (task, index) {
if (task.id === note.task_id) {
tasks[index].notes.push(note)
}
})
})
files.forEach(function (file) {
tasks.forEach(function (task, index) {
if (task.id === file.task_id) {
tasks[index].files.push(file)
}
})
})
list.tasks = tasks
lists.push(list)
complete()
})
}, function (err) {
if (err) process.exit(1)
data.lists = lists
callback(err, data)
})
}
function getEmbeddedLists (callback) {
async.waterfall([
getLists,
embedLists
], function (err, data) {
if (err) process.exit(1)
callback(err, data)
})
}
function main () {
async.parallel([
getUser,
getEmbeddedLists
], function (err, results) {
if (err) process.exit(1)
var data = {
exported_at: new Date(),
data: {
user: results[0].user,
lists: results[1].lists
}
}
var output
if (app.pretty) {
output = JSON.stringify(data, null, 2)
} else {
output = JSON.stringify(data)
}
process.stdout.write(output)
})
}
auth(main)