-
Notifications
You must be signed in to change notification settings - Fork 21
/
writeToCSV.js
62 lines (53 loc) · 1.92 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
57
58
59
60
61
62
const path = require('path')
const { escapeComment, stripHTML, writeHeader, appendRow, toPacificTimeString} = require('./util') // Adjust the path as necessary
const writeToCSV = (courseId, data) => {
console.log(`Writing discussion data for course: ${courseId}`);
const csvPath = path.join(__dirname, `output/${courseId}-discussion.csv`)
const headers =[
'topic_id',
'topic_title',
'topic_message',
'topic_author_id',
'topic_author_name',
'topic_created_at',
'topic_posted_at',
'post_author_id',
'post_author_name',
'post_id',
'post_parent_id',
'post_message',
'post_likes',
'post_timestamp'
]
// Write the headers to the CSV file
writeHeader(csvPath, headers)
data.forEach(discussion => {
const topicDetails = {
topic_id: discussion.topicId,
topic_title: stripHTML(escapeComment(discussion.topicTitle)),
topic_message: stripHTML(escapeComment(discussion.topicMessage)),
topic_author_id: discussion.topicAuthorId,
topic_author_name: escapeComment(discussion.topicAuthorName),
topic_created_at: toPacificTimeString(discussion.topicCreatedAt),
topic_posted_at: toPacificTimeString(discussion.topicPostedAt)
};
if (Array.isArray(discussion.replies) && discussion.replies.length > 0) {
discussion.replies.flat().forEach(post => {
const postDetails = {
...topicDetails,
post_author_id: post.postAuthorId,
post_author_name: escapeComment(post.postAuthorName),
post_id: post.postId,
post_parent_id: post.postParentId,
post_message: stripHTML(escapeComment(post.postMessage)),
post_likes: post.postLikes,
post_timestamp: toPacificTimeString(post.postTimestamp)
};
appendRow(csvPath, Object.values(postDetails))
})
} else {
appendRow(csvPath, Object.values(topicDetails))
}
})
}
module.exports = writeToCSV;