-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeVotes.js
46 lines (40 loc) · 1.22 KB
/
makeVotes.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
// - create dir: data/:convoId/
// - fetch /api/v3/conversationStats.json
// - fetch /api/v3/conversations.json
// - fetch /api/v3/math/pca2.json
// - write summary.csv
// - write votes.csv
import fs from 'node:fs/promises'
import { formatCustomDate } from './lib/utils.js';
const args = process.argv.slice(2);
const convoId = args[0]
const CONVO_ID = convoId || "6bkf4ujff9"
const json = await fs.readFile(`data/${CONVO_ID}--votes.json`, "utf-8");
const unsorted = JSON.parse(json)
const sortByCommentIdAsc = (a, b) => {
return a.tid - b.tid || a.pid - b.pid
}
const sorted = unsorted.sort(sortByCommentIdAsc)
const refinedData = []
refinedData.push(["timestamp", "datetime", "comment-id", "voter-id", "vote"])
sorted.forEach(item => {
refinedData.push([
item.modified,
formatCustomDate(item.modified),
item.tid,
item.pid,
// Invert API response for some reason. Why?
-item.vote,
])
})
let csvContent = ''
refinedData.forEach(row => {
csvContent += row.join(',') + '\n'
})
const filePath = `outputs/${CONVO_ID}/votes.csv`
try {
await fs.writeFile(filePath, csvContent, 'utf-8')
console.log(`Successfully wrote to: ${filePath}`)
} catch (error) {
console.error(`Failed to write to: ${filePath}`, error)
}