Skip to content

Commit

Permalink
feat(scripts): add scripts to get bookmarks from readwise reader
Browse files Browse the repository at this point in the history
  • Loading branch information
noghartt committed Oct 31, 2024
1 parent 2493944 commit 15f56fc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "astro preview",
"astro": "astro",
"create:article": "tsx ./scripts/new-post.ts",
"generate:bookmarks": "tsx --env-file=./.env ./scripts/generate-bookmarks.ts"
"generate:bookmarks": "tsx --env-file=./.env ./scripts/generate-bookmarks-reader.ts"
},
"dependencies": {
"@resvg/resvg-js": "^2.6.2",
Expand Down
82 changes: 82 additions & 0 deletions scripts/generate-bookmarks-reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import fs from 'fs/promises';
import path from 'path';

const READWISE_API_URL = 'https://readwise.io/api/v3/list/';
const READWISE_READER_API_KEY = `Token ${process.env.READWISE_READER_API_KEY}`;

const fetchBookmarks = async ({ nextPage } = { nextPage: null }) => {
const queryParams = new URLSearchParams();
if (nextPage) {
queryParams.append('pageCursor', nextPage);
}

try {
const url = `${READWISE_API_URL}?${queryParams.toString()}`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: READWISE_READER_API_KEY,
},
});

if (!response.ok) {
return { error: response.statusText, data: null };
}

const data = await response.json();

const dataFiltered = data.results.filter(bookmark => {
if (!['article', 'video'].includes(bookmark.category)) {
return false;
}

if (bookmark.tags.newsletter) {
return false;
}

return true;
});
const nextPage = data.nextPageCursor;

return {
data: dataFiltered,
nextPage,
}
} catch (err) {
return { error: err, data: null };
}
}

const writeJsonFile = async (data: any) => {
const cwd = process.cwd();
const filepath = path.join(cwd, 'src', 'pages', 'bookmarks', '_bookmarks.json');
await fs.writeFile(filepath, JSON.stringify({ lastUpdate: new Date().toISOString(), data }, null, 2));
}

(async () => {
const bookmarks = [];
let nextPage = null;
do {
console.log('Fetching bookmarks from:', nextPage ?? 'start');
const response = await fetchBookmarks({ nextPage });
if (response.error) {
console.log('Error while fetching:', response.error);
break;
}

nextPage = response.nextPage;
bookmarks.push(...response.data);
} while (nextPage);

const mappedBookmarks = bookmarks.map(bookmark => ({
id: bookmark.id,
title: bookmark.title,
url: bookmark.source_url,
savedAt: bookmark.saved_at,
description: bookmark.summary || null,
tags: Object.values(bookmark.tags).map(tags => tags.name),
}));

await writeJsonFile(mappedBookmarks);
})();

0 comments on commit 15f56fc

Please sign in to comment.