Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add csv export for bookmarks #118

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"chalk": "^5.2.0",
"connect-sqlite3": "^0.9.13",
"cors": "^2.8.5",
"csv-stringify": "^6.4.2",
"dotenv": "^16.0.3",
"es6-promisify": "^7.0.0",
"express": "^4.18.2",
Expand Down
17 changes: 17 additions & 0 deletions src/bookmarks-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,23 @@ export async function getBookmarks(limit = 10, offset = 0) {
return undefined;
}

export async function getBookmarksForCSVExport() {
// We use a try catch block in case of db errors
try {
const headers = ['title', 'url', 'description', 'tags', 'created_at', 'updated_at'];
const selectHeaders = headers.join(',');
// This will create an object where the keys and values match. This will
// allow the csv stringifier to interpret this as a header row.
const columnTitles = Object.fromEntries(headers.map((header) => [header, header]));
const results = await db.all(`SELECT ${selectHeaders} from bookmarks`);
return [columnTitles].concat(results);
} catch (dbError) {
// Database connection error
console.error(dbError);
}
return undefined;
}

export async function getBookmarkCountForTags(tags) {
const tagClauses = tags.map(() => `(tags like ? OR tags like ?)`).join(' AND ');
const tagParams = tags.map((tag) => [`%${tag}% `, `%${tag}%`]).flat();
Expand Down
9 changes: 6 additions & 3 deletions src/pages/admin/data.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<h3>
Download bookmarks
</h3>
<p>
You can download your bookmarks sqlite db
<a href="/admin/bookmarks.db">here</a>.
<p style="margin: 0;">
You can download your bookmarks as:
</p>
<ul>
<li><a href="/admin/bookmarks.db">sqlite database</a></li>
<li><a href="/admin/bookmarks.csv">csv</a></li>
</ul>
<p>
It's less likely you'll need this, but you can also download your
activitypub.db
Expand Down
13 changes: 13 additions & 0 deletions src/routes/admin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import express from 'express';
// eslint-disable-next-line import/no-unresolved, node/no-missing-import
import { stringify as csvStringify } from 'csv-stringify/sync'; // https://github.com/adaltas/node-csv/issues/323
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These eslint issues seem like a known issue. I tried using the csv package instead, but that didn't work either 😞

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a similar issue with string-strip-html here-- if anyone reading this knows why this happens, I'd love to know!

import { domain, actorInfo, parseJSON } from '../util.js';
import { isAuthenticated } from '../session-auth.js';
import { lookupActorInfo, createFollowMessage, createUnfollowMessage, signAndSend, getInboxFromActorProfile } from '../activitypub.js';
Expand Down Expand Up @@ -91,6 +93,17 @@ router.get('/bookmarks.db', isAuthenticated, async (req, res) => {
res.download(filePath);
});

router.get('/bookmarks.csv', isAuthenticated, async (req, res) => {
const bookmarksDb = req.app.get('bookmarksDb');
const bookmarks = await bookmarksDb.getBookmarksForCSVExport();
const result = csvStringify(bookmarks, { quoted: true });

res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename="bookmarks.csv"');

res.send(result);
});

router.get('/activitypub.db', isAuthenticated, async (req, res) => {
const filePath = `${DATA_PATH}/activitypub.db`;

Expand Down