Skip to content

Commit 7810d01

Browse files
committed
Finalized import notes with errors command
1 parent 9222c42 commit 7810d01

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# TODO
22

3-
- Добавить команду повторного импорта заметок с ошибками
3+
- Use sqlite instead of PostgresQL

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getImportedWithErrorsNotes, getImportedNotesStatus, importNotes } from './src/notes_importer.js'
1+
import { getImportedWithErrorsNotes, getImportedNotesStatus, importNotes, importNotesWithErrors } from './src/notes_importer.js'
22
import { cleanDb } from './src/db.js'
33

44
await main()
@@ -15,7 +15,7 @@ async function main() {
1515
await importNotes(args[1])
1616
return
1717
case '--import-with-errors':
18-
await importNotes(args[1], true)
18+
await importNotesWithErrors(args[1])
1919
return
2020
case '--status':
2121
await printStatus(args[1])

src/db.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const pool = new pg.Pool({
99

1010
export async function loadImportStatuses() {
1111
const res = await pool.query({
12-
text: 'SELECT * FROM imported_notes'
12+
text: 'SELECT * FROM imported_notes ORDER BY id'
1313
})
1414
return res.rows
1515
}
@@ -21,6 +21,13 @@ export async function addImportStatus({ noteId, title, filePath, imported, error
2121
})
2222
}
2323

24+
export async function updateImportStatus({ noteId, title, imported, error }){
25+
await pool.query({
26+
text: 'UPDATE imported_notes SET "title" = $1, imported = $2, error = $3 WHERE "noteId" = $4',
27+
values: [title, imported, error, noteId]
28+
})
29+
}
30+
2431
export async function cleanDb() {
2532
// noinspection SqlWithoutWhere
2633
await pool.query({

src/notes_importer.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { lstatSync, readdirSync, readFileSync } from 'node:fs'
1+
import { lstatSync, readdirSync, readFileSync, existsSync } from 'node:fs'
22
import { markdownToBlocks } from '@tryfabric/martian'
3-
import { addBlocks, createPage } from './notion_api.js'
4-
import { addImportStatus, loadImportStatuses } from './db.js'
3+
import { addBlocks, archivePage, createPage } from './notion_api.js'
4+
import { addImportStatus, loadImportStatuses, updateImportStatus } from './db.js'
55

66

77
const importedNotes = {}
@@ -48,6 +48,57 @@ export async function importNotes(notesDir, onlyWithErrors = false) {
4848
console.info('\nDONE.')
4949
}
5050

51+
export async function importNotesWithErrors(notesDir) {
52+
const notes = (await loadImportStatuses()).filter(it => it.error)
53+
let fileIndex = 0
54+
55+
for (const noteInfo of notes) {
56+
const progress = Math.round(++fileIndex / notes.length * 100)
57+
console.log(`\n[${progress}%][${fileIndex}/${notes.length}] ${noteInfo.filePath}`)
58+
console.log('-'.repeat(100) + '\n')
59+
const filePath = `${notesDir}/${noteInfo.filePath}`
60+
61+
noteInfo.imported = true
62+
noteInfo.error = null
63+
64+
if (!existsSync(filePath)) {
65+
console.log(`File: "${filePath}" was deleted and will be archive in Notion`)
66+
try {
67+
await archivePage(noteInfo.noteId)
68+
} catch (e) {
69+
noteInfo.imported = false
70+
noteInfo.error = e.body || e
71+
console.error(`ERROR with note id: ${noteInfo.id}`)
72+
}
73+
74+
await updateImportStatus(noteInfo)
75+
continue
76+
}
77+
78+
noteInfo.data = ''
79+
try {
80+
noteInfo.data = readFileSync(filePath, 'utf8')
81+
} catch (e) {
82+
console.error('error in file: ', noteInfo.filePath, '\n', e)
83+
}
84+
85+
if (!noteInfo.data) {
86+
console.log(`File: "${filePath}" is empty`)
87+
continue
88+
}
89+
90+
const blocks = markdownToBlocks(noteInfo.data)
91+
try {
92+
await addBlocks({ parentId: noteInfo.noteId, blocks })
93+
} catch (e) {
94+
noteInfo.imported = false
95+
noteInfo.error = e.body || e
96+
console.error('ERROR')
97+
}
98+
await updateImportStatus(noteInfo)
99+
}
100+
}
101+
51102
async function createParentNotes(relFilePath) {
52103
let dirPath = ''
53104
let parentId = ''

src/notion_api.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,22 @@ export async function createPage({ title, pageId }) {
3030
})
3131
}
3232

33+
export async function archivePage(pageId) {
34+
await notion.pages.update({
35+
page_id: pageId,
36+
archived: true,
37+
})
38+
}
39+
3340
export async function addBlocks({ parentId, blocks }){
3441
return await notion.blocks.children.append({
3542
block_id: parentId,
3643
children: blocks
3744
})
3845
}
46+
47+
export async function deleteBlock(blockId) {
48+
return await notion.blocks.delete({
49+
block_id: blockId,
50+
})
51+
}

0 commit comments

Comments
 (0)