forked from bmakuh/quip-mass-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (82 loc) · 2.98 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
const axios = require('axios')
const { curry, forEach } = require('lodash')
const fs = require('fs')
const toMarkdown = require('to-markdown')
const HtmlDocx = require('html-docx-js');
const headers = { Authorization: `Bearer ${process.argv[2]}` }
const fetch = (url, opts = {}) =>
axios(url, Object.assign({}, { headers }, opts))
const logErr = (err) => {
console.error(err)
process.exitCode = 1
return
}
// fetchPrivateFolder :: (number) => Promise<*>
const fetchPrivateFolder = (id) =>
fetch(`https://platform.quip.com/1/folders/${id}`)
// fetchDocs :: (Array<number>, string) => Promise<*>
const fetchDocs = (children, folderName = 'output') => {
fs.mkdir(folderName, 0o777, (err) => {
if (err) return logErr(`❌ Failed to create folder ${folderName}. ${err}`)
console.log(`🗂 ${folderName} created successfully`)
})
const ids = children
.filter(({ thread_id }) => !!thread_id)
.map(({ thread_id }) => thread_id)
.join(',')
const folderIds = children
.filter(({ folder_id }) => !!folder_id)
.map(({ folder_id }) => folder_id)
forEach(folderIds, (folderId) => fetchThreads(folderId, folderName))
return fetch(`https://platform.quip.com/1/threads/?ids=${ids}`)
.then(writeFiles(folderName))
}
// fetchThreads :: (number, string) => Promise<*>
const fetchThreads = (folderId, parentDir) => {
return fetch(`https://platform.quip.com/1/folders/${folderId}`)
.then(({ data }) => {
forEach(data, (folder) => {
if (!folder.title) return
fetchDocs(data.children, `${parentDir}/${folder.title}`)
})
})
}
// writeFiles :: Object => void
const writeFiles = curry((folderName, { data }) => {
forEach(data, (({ thread, html }) => {
const file = thread.title.replace(/\//g, '')
const fileName = `${folderName}/${file}`
// fs.writeFile(`${fileName}.html`, html, (err) => {
// if (err) return logErr(`❌ Failed to save ${fileName}.html. ${err}`)
// console.log(`✅ ${fileName}.html saved successfully`)
// })
// fs.writeFile(`${fileName}.md`, toMarkdown(html), (err) => {
// if (err) return logErr(`❌ Failed to save ${fileName}.md. ${err}`)
// console.log(`✅ ${fileName}.md saved successfully`)
// })
fs.writeFile(`${fileName}.docx`, HtmlDocx.asBlob(html), (err) => {
if (err) return logErr(`❌ Failed to save ${fileName}.docx. ${err}`)
console.log(`✅ ${fileName}.docx saved successfully`)
})
}))
})
// main :: () => void
const main = () => {
if (!process.argv[2]) {
console.log('❌ Please provide your Quip API token. Exiting.')
process.exitCode = 1
return
}
return fetch('https://platform.quip.com/1/users/current')
.then((res) => {
return new Promise((resolve, reject) => {
if (res.status !== 200) return reject(`❌ Error: ${res.statusText}`)
resolve(res.data.private_folder_id)
})
})
.then(fetchPrivateFolder)
.then(({ data: { children } }) => children)
.then(fetchDocs)
.catch(logErr)
}
main()