mirror of
https://github.com/pawelmalak/flame.git
synced 2025-07-23 05:19:37 +02:00
Bookmark import.
This commit is contained in:
parent
1fff824957
commit
1aedb87bf4
17 changed files with 327 additions and 8 deletions
103
utils/importBookmark.js
Normal file
103
utils/importBookmark.js
Normal file
|
@ -0,0 +1,103 @@
|
|||
const parse = require('node-bookmarks-parser');
|
||||
var sqlite3 = require('sqlite3').verbose();
|
||||
const File = require('./File');
|
||||
const databaseFilePath = 'data/db.sqlite';
|
||||
|
||||
let bookmarks = [];
|
||||
|
||||
function saveBookmarks() {
|
||||
const db = new sqlite3.Database(databaseFilePath);
|
||||
const importDate = new Date().toISOString();
|
||||
|
||||
db.serialize(() => {
|
||||
db.all(`SELECT id, name FROM categories`, (err, data) => {
|
||||
if (err) {
|
||||
}
|
||||
|
||||
for (const bookmark of bookmarks) {
|
||||
// Found in db.
|
||||
let stmt = db.prepare(
|
||||
'INSERT INTO bookmarks (name, url, categoryId, icon, createdAt, updatedAt) VALUES(?,?,?,?,?,?)'
|
||||
);
|
||||
stmt.run(
|
||||
bookmark.title,
|
||||
bookmark.url,
|
||||
data.find(
|
||||
(r) => r.name.toLowerCase() === bookmark.category.toLowerCase()
|
||||
)?.id,
|
||||
bookmark.icon,
|
||||
importDate,
|
||||
importDate,
|
||||
(err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
);
|
||||
stmt.finalize();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function saveCategories() {
|
||||
let db = new sqlite3.Database(databaseFilePath);
|
||||
let uniqueCats = [...new Set(bookmarks.map((r) => r.category))];
|
||||
const importDate = new Date().toString();
|
||||
|
||||
db.serialize(() => {
|
||||
db.all(`SELECT * FROM categories`, (err, data) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
for (const newCaterory of uniqueCats) {
|
||||
for (const category of data) {
|
||||
if (category.name.toLowerCase() === newCaterory.toLowerCase()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let stmt = db.prepare(
|
||||
'INSERT INTO categories (name, createdAt, updatedAt) VALUES(?,?,?)'
|
||||
);
|
||||
stmt.run(newCaterory, importDate, importDate, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
stmt.finalize();
|
||||
}
|
||||
|
||||
saveBookmarks();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function crawlBookmarks(bookmark, category) {
|
||||
if (bookmark.type === 'bookmark') {
|
||||
bookmarks.push({
|
||||
title: bookmark.title,
|
||||
url: bookmark.url,
|
||||
icon: bookmark.icon,
|
||||
category: category,
|
||||
});
|
||||
}
|
||||
|
||||
if (bookmark.type === 'folder') {
|
||||
for (const child of bookmark.children) {
|
||||
crawlBookmarks(child, bookmark.title);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function importBookmark(path) {
|
||||
const fileContent = new File(path).read();
|
||||
const bookmarkData = parse(fileContent);
|
||||
|
||||
for (const bookmark of bookmarkData) {
|
||||
crawlBookmarks(bookmark);
|
||||
}
|
||||
|
||||
saveCategories();
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue