-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
417 lines (328 loc) · 10.4 KB
/
app.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//important note the data.json must have some values beforehand. also project name on last value in data.json must be common in sheet.
const child_process = require('child_process');
const fs = require("fs");
const path = require("path")
const {google} = require("googleapis")
const { Client } = require("@notionhq/client");
const hljs = require("highlight.js/lib/common");
require("dotenv").config();
async function getProject(){
const auth =new google.auth.GoogleAuth({
keyFile : "cred.json",
scopes: "https://www.googleapis.com/auth/spreadsheets"
})
const client = await auth.getClient();
const googlesheets = google.sheets({version:"v4",auth: client})
const spreadsheetId = "1sQviPG1zp8vSmaeA0BfZAduqJb1bLAGlrijWFi0bJ8o"
const viewColumns = await googlesheets.spreadsheets.values.get({
auth,
spreadsheetId,
range:"projects",
majorDimension:"COLUMNS"
})
return viewColumns.data.values
}
function updateProjects(project_to_add){
let data = []
const len = project_to_add[0].length
console.log(len)
for(let i=len-1;i>=1;i--){
const writeObj =
{
"project_name":"",
"date":"",
"github":"",
"live":"",
"description":""
}
writeObj.project_name = project_to_add[0][i]
writeObj.date = project_to_add[1][i]
writeObj.github = project_to_add[2][i]
writeObj.live = project_to_add[3][i]
writeObj.description = project_to_add[4][i]
data.push(writeObj)
}
return data
}
getProject()
.then((res)=>{
const project_to_add = res
const filepath = `D:/pet_projects/Arpit078.github.io/data/projects_data.json`
const updatedData = updateProjects(project_to_add)
const writeData = JSON.stringify(updatedData)
console.log(updatedData)
fs.writeFileSync(filepath, writeData, err => {});
// child_process.exec('gitpush.bat', function(error, stdout, stderr) {
// console.log(error);
// });
})
//books
async function getBooks(){
const auth =new google.auth.GoogleAuth({
keyFile : "cred.json",
scopes: "https://www.googleapis.com/auth/spreadsheets"
})
const client = await auth.getClient();
const googlesheets = google.sheets({version:"v4",auth: client})
const spreadsheetId = "1sQviPG1zp8vSmaeA0BfZAduqJb1bLAGlrijWFi0bJ8o"
const viewColumns = await googlesheets.spreadsheets.values.get({
auth,
spreadsheetId,
range:"books",
majorDimension:"COLUMNS"
})
return viewColumns.data.values
}
function updateBooks(project_to_add){
let data = []
const len = project_to_add[0].length
console.log(len)
for(let i=len-1;i>=1;i--){
const writeObj =
{
"book_name":"",
"read_date":"",
"cover_url":"",
"description":""
}
writeObj.book_name = project_to_add[0][i]
writeObj.read_date = project_to_add[3][i]
writeObj.cover_url = project_to_add[2][i]
writeObj.description = project_to_add[1][i]
data.push(writeObj)
}
return data
}
getBooks()
.then((res)=>{
const project_to_add = res
const filepath = "D:/pet_projects/Arpit078.github.io/data/books_data.json"
const updatedData = updateBooks(project_to_add)
const writeData = JSON.stringify(updatedData)
console.log(updatedData)
fs.writeFileSync(filepath, writeData, err => {});
// child_process.exec('gitpush.bat', function(error, stdout, stderr) {
// console.log(error);
// });
})
//blogs
//---------------------------------------------------------------------------------
//getblogs
const notion = new Client({ auth: process.env.NOTION_API_KEY });
async function getBlocks(block_id) {
let { results: children } = await notion.blocks.children.list({ block_id });
for (const child of children) {
const grandchildren = await getBlocks(child.id);
child.children = grandchildren;
}
return children;
}
async function importPages() {
// Retrieve pages from the database.
let { results: pages } = await notion.databases.query({
database_id: process.env.NOTION_DATABASE_ID,
});
// Attach blocks to pages
for (const page of pages) {
const blocks = await getBlocks(page.id);
page.children = blocks;
}
// Write the result to file.
const outputFile = path.join(__dirname, "notion-export.json");
fs.writeFileSync(outputFile, JSON.stringify(pages, null, 2));
console.log(`Wrote ${pages.length} pages to ${outputFile}`);
}
importPages().then(
(res)=>{
const outputFile = path.join(
__dirname,
"./notion-export.json"
);
if (!fs.existsSync(outputFile)) {
console.error(`File not found: ${outputFile}`);
process.exit(1);
}
const pages = JSON.parse(fs.readFileSync(outputFile));
function resolveTabs(tabsBlocks) {
return tabsBlocks
.map((tab) => {
if (tab.type !== "callout") return null;
return {
label: tab.callout.rich_text
.map(({ plain_text }) => plain_text)
.join(""),
text: tab.children[0].paragraph.rich_text
.map(({ plain_text }) => plain_text)
.join(""),
};
})
.filter(Boolean);
}
const calloutMap = {
tabs: (block) => {
return {
component: "Tabs",
tabs: resolveTabs(block.children),
};
},
code: (block) => {
if (!block.children[0].code) {
console.log("Code component must be first child of code callout");
return null;
}
const code = block.children[0].code.rich_text
.map(({ plain_text }) => plain_text)
.join("");
// YOU MAY NEED A MAPPER FOR THIS
const language = block.children[0].code.language;
const highlightedCode = hljs.highlight(code, { language }).value;
return {
component: "CodeBlock",
filename: block.callout.rich_text
.map(({ plain_text }) => plain_text)
.join(""),
code: highlightedCode,
language: block.children[0].code.language,
};
},
};
function resolveCalloutComponent(block) {
if (block.callout?.icon?.type !== "external") {
console.log("ICON NOT SUPPORTED:", block.callout.icon);
return null;
}
const iconName = block.callout.icon.external.url
.split("/")
.pop()
.split(".")[0]
.split("_")[0];
if (!calloutMap[iconName]) {
console.log("EXTERNAL ICON NOT SUPPORTED:", iconName);
return null;
}
return calloutMap[iconName](block);
}
const blockMap = {
callout: (block) => {
const result = resolveCalloutComponent(block);
if (!result) return null;
return result;
},
paragraph: (block) => {
if (block.paragraph.rich_text.length === 0) return null;
return {
component: "paragraph",
text: block.paragraph.rich_text
.map(({ plain_text }) => plain_text)
.join(""),
};
},
bulleted_list_item : (block)=>{
if (block.bulleted_list_item.rich_text.length === 0) return null;
return {
component : "bulleted_list_item",
text: block.bulleted_list_item.rich_text
.map(({ plain_text }) => plain_text)
.join(""),
}
},
numbered_list_item : (block)=>{
if (block.numbered_list_item.rich_text.length === 0) return null;
return {
component : "numbered_list_item",
text: block.numbered_list_item.rich_text
.map(({ plain_text }) => plain_text)
.join(""),
}
},
code : (block)=>{
if (block.code.rich_text.length === 0) return null;
return {
component : "code",
text: block.code.rich_text
.map(({ plain_text }) => plain_text)
.join(""),
}
}
};
function transformBlocks(blocks) {
return blocks
.map((block) => {
if (blockMap[block.type]) {
return blockMap[block.type](block);
}
console.log("NOT SUPPORTED:", block.type);
})
.filter(Boolean);
}
// console.log(pages)
let output = pages.map((page) => {
const { properties, children, id } = page;
return {
id,
title: properties.Blog.title[0].text.content,
// urlPath: properties.Slug.rich_text[0].plain_text,
date:properties.Date.date.start,
blocks: transformBlocks(children),
};
});
console.log(output)
const transformedOutput = "D:/pet_projects/Arpit078.github.io/data/blogs_data.json";
fs.writeFile(transformedOutput, JSON.stringify(output, null, 2),err=>{console.err}
);
// child_process.exec('gitpush.bat', function(error, stdout, stderr) {
// console.log(error);
// });
console.log(`Transformed ${output.length} pages to ${transformedOutput}`);
}
);
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//transform pages
//---------------------------------------------------------------------------------
// async function getBlogs(){
// const auth =new google.auth.GoogleAuth({
// keyFile : "cred.json",
// scopes: "https://www.googleapis.com/auth/spreadsheets"
// })
// const client = await auth.getClient();
// const googlesheets = google.sheets({version:"v4",auth: client})
// const spreadsheetId = "1sQviPG1zp8vSmaeA0BfZAduqJb1bLAGlrijWFi0bJ8o"
// const viewColumns = await googlesheets.spreadsheets.values.get({
// auth,
// spreadsheetId,
// range:"blogs",
// majorDimension:"COLUMNS"
// })
// return viewColumns.data.values
// }
// function updateBlogs(project_to_add){
// let data = []
// const len = project_to_add[0].length
// console.log(len)
// for(let i=len-1;i>=1;i--){
// const writeObj =
// {
// "title":"",
// "date":"",
// "content":""
// }
// writeObj.title = project_to_add[0][i]
// writeObj.date = project_to_add[1][i]
// writeObj.content = project_to_add[2][i]
// data.push(writeObj)
// }
// return data
// }
// getBlogs()
// .then((res)=>{
// const project_to_add = res
// const filepath = "D:/pet_projects/Arpit078.github.io/data/blogs_data.json"
// const updatedData = updateBlogs(project_to_add)
// const writeData = JSON.stringify(updatedData)
// console.log(updatedData)
// fs.writeFileSync(filepath, writeData, err => {});
// child_process.exec('gitpush.bat', function(error, stdout, stderr) {
// console.log(error);
// });
// })