forked from ubc/canvas-discussion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeToCSV.js
56 lines (48 loc) · 1.52 KB
/
writeToCSV.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
const fs = require('fs')
const path = require('path')
const fswrite = fs.writeFileSync
const fsappend = fs.appendFileSync
const writeHeader = (pathToFile, header) => fswrite(pathToFile, header + '\r\n')
const append = (pathToFile, row) => fsappend(pathToFile, row + '\r\n')
const escapeComment = comment => comment ? '"' + comment.replace(/"/g, "'") + '"' : ''
const writeToCSV = data => {
const csv = path.join(__dirname, 'output.csv')
const header = [
'author_id',
'post_id',
'post_parent_id',
'discussion_topic_title',
'discussion_topic_message',
'post_message',
'count_of_likes',
'timestamp'
]
writeHeader(csv, header)
data.forEach(discussion => {
append(csv, [ // write discussion to CSV
discussion.authorId,
discussion.id,
'', // discussion topics cannot have a parent ID
escapeComment(discussion.topicTitle),
escapeComment(discussion.topicMessage),
'',
'',
discussion.timestamp
])
discussion.replies.forEach(reply =>
reply.forEach(response => {
append(csv, [ // write replies to CSV
response.authorId,
response.id,
response.parentId,
escapeComment(discussion.topicTitle),
escapeComment(discussion.topicMessage),
escapeComment(response.message),
response.likes,
response.timestamp
])
})
)
})
}
module.exports = writeToCSV