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 pagination to the Readwise script #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
82 changes: 48 additions & 34 deletions readwise.mjs
Original file line number Diff line number Diff line change
@@ -1,50 +1,64 @@
import fetch from "isomorphic-fetch";
import { config } from "./config.mjs";

const HIGHLIGHTS_URL = "https://readwise.io/api/v2/export/";
const HIGHLIGHTS_URL = "https://readwise.io/api/v2/export/?";

const getItemsFromReadwise = async (daysToFetch = 1) => {
const dateOffset = 24 * 60 * 60 * 1000 * daysToFetch;
const updatedAfterDate = new Date();
updatedAfterDate.setTime(updatedAfterDate.getTime() - dateOffset);
let nextPageCursor = null;

const response = await fetch(
`${HIGHLIGHTS_URL}?updatedAfter=${updatedAfterDate.toISOString()}`,
{
while (true) {
const queryParams = new URLSearchParams();
const dateOffset = 24 * 60 * 60 * 1000 * daysToFetch;
const updatedAfterDate = new Date();
updatedAfterDate.setTime(updatedAfterDate.getTime() - dateOffset);

if (nextPageCursor) {
queryParams.append("pageCursor", nextPageCursor);
}
if (updatedAfterDate) {
queryParams.append("updatedAfter", updatedAfterDate.toISOString());
}
const response = await fetch(`${HIGHLIGHTS_URL + queryParams.toString()}`, {
headers: {
Authorization: `Token ${config.readwiseToken}`,
},
}
);
const data = await response.json();
data.results.forEach((book) => {
const isValidSourceURL = book.source_url?.startsWith("https://");
const hasHighlights = book.highlights?.length > 0;

console.log(`- ${book.title} #from-the-web`);
if (isValidSourceURL) {
console.log(` - URL:: ${book.source_url}`);
}
console.log(` - type:: ${book.category?.replace(/s$/, "")}`);
console.log(` - author:: ${book.author}`);
if (hasHighlights) {
console.log(` - Highlights`);
}
});

book.highlights.forEach((highlight) => {
const lines = highlight.text.split("\n");
lines.forEach((line) => {
const cleanedLine = line.replace(/•\s+/, "").trim();
if (cleanedLine.length > 0) {
console.log(` - ${cleanedLine}`);
}
});
const data = await response.json();
nextPageCursor = data.nextPageCursor;

data.results.forEach((book) => {
const isValidSourceURL = book.source_url?.startsWith("https://");
const hasHighlights = book.highlights?.length > 0;

if (highlight.note) {
console.log(` - ${highlight.note}`);
console.log(`- ${book.title} #from-the-web`);
if (isValidSourceURL) {
console.log(` - URL:: ${book.source_url}`);
}
console.log(` - type:: ${book.category?.replace(/s$/, "")}`);
console.log(` - author:: ${book.author}`);
if (hasHighlights) {
console.log(` - Highlights`);
}

book.highlights.forEach((highlight) => {
const lines = highlight.text.split("\n");
lines.forEach((line) => {
const cleanedLine = line.replace(/•\s+/, "").trim();
if (cleanedLine.length > 0) {
console.log(` - ${cleanedLine}`);
}
});

if (highlight.note) {
console.log(` - ${highlight.note}`);
}
});
});
});
if (nextPageCursor == null) {
break;
}
}
};

const daysToFetch = process.argv[2];
Expand Down