-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (107 loc) · 3.04 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
105
106
107
108
109
110
111
112
113
114
115
116
import { Client } from '@notionhq/client';
import csv from 'csv-parser';
import fs from 'fs';
const notion = new Client({
auth: 'secret_MmOjLSmGJSCeYQZXApaO04K7FioJ4pS1a91i0azzW3A'
});
const databaseId = '5f75e6f065214229ad99e1ca8350ac71';
function OrganizeData() {
const arr = [];
fs.createReadStream('ratings.csv')
.pipe(csv(['Book Title', 'Reader', 'Rating']))
.on('data', (data) => {
data['Book Title'] = data['Book Title'].trim().toLowerCase();
arr.push(data);
})
.on('end', () => solution(arr));
const solution = (arr) => {
let books = {};
for (let i = 0; i < arr.length; i++) {
let entry = arr[i];
// create title object if it doesn't exist
let title = entry['Book Title'];
let reader = entry['Reader'];
let rating = Number(entry['Rating']);
let TOP_RATING = 5;
if (!books[title]) {
books[title] = {
favorites: 0,
ratingSum: 0,
avgRating: 0,
readers: { total: 0 }
};
}
// new reader
if (!books[title].readers[reader]) {
if (rating == TOP_RATING) books[title].favorites++;
books[title].readers[reader] = rating;
books[title].ratingSum += rating;
books[title].readers.total++;
books[title].avgRating = Math.round(
books[title].ratingSum / books[title].readers.total
).toPrecision(2);
continue;
}
// existing reader
let existingRating = books[title].readers[reader];
let diff = Math.abs(existingRating - rating);
// update favorites
if (rating == TOP_RATING && existingRating !== TOP_RATING)
books[title].favorites += 1;
// update ratingSum
if (existingRating == TOP_RATING && rating !== TOP_RATING)
books[title].favorites -= 1;
books[title].ratingSum += existingRating > rating ? -diff : diff;
// update reader rating
books[title].readers[reader] = rating;
books[title].avgRating = Math.round(
books[title].ratingSum / books[title].readers.total
).toPrecision(2);
}
const titles = Object.keys(books);
for (let i = 0; i < titles.length; i++) {
addRow(titles[i], books[titles[i]]);
}
};
}
async function addRow(title, props) {
try {
const response = await notion.pages.create({
parent: { database_id: databaseId },
properties: {
title: {
title: [
{
text: {
content: title
}
}
]
},
'Avg Rating': {
rich_text: [
{
text: {
content: String(props.avgRating)
}
}
]
},
'Number of Times Favorited': {
rich_text: [
{
text: {
content: String(props.favorites)
}
}
]
}
}
});
console.log(response);
console.log('Success! Entry added.');
} catch (error) {
console.error(error.body);
}
}
OrganizeData();