Skip to content

Instantly share code, notes, and snippets.

@wannli
Last active January 9, 2023 20:07
Show Gist options
  • Save wannli/6f83c3c1288e97edba8190c240135a73 to your computer and use it in GitHub Desktop.
Save wannli/6f83c3c1288e97edba8190c240135a73 to your computer and use it in GitHub Desktop.
import fetch from "isomorphic-fetch";
const token = ""; // use your access token here
const time = "2023-01-04T18:00:00.000Z";
const debug = false;
const fetchFromExportApi = async (updatedAfter = null) => {
let fullData = [];
let nextPageCursor = null;
while (true) {
const queryParams = new URLSearchParams();
if (nextPageCursor) {
queryParams.append("pageCursor", nextPageCursor);
}
if (updatedAfter) {
queryParams.append("updatedAfter", updatedAfter);
}
// console.log('Making export api request with params ' + queryParams.toString());
const response = await fetch(
"https://readwise.io/api/v2/export/?" + queryParams.toString(),
{
method: "GET",
headers: {
Authorization: `Token ${token}`,
},
}
);
const responseJson = await response.json();
fullData.push(...responseJson["results"]);
nextPageCursor = responseJson["nextPageCursor"];
if (!nextPageCursor) {
break;
}
}
return fullData;
};
// Get all of a user's books/highlights from all time
const input = await fetchFromExportApi();
// Later, if you want to get new highlights updated since your last fetch of allData, do this.
// const lastFetchWasAt = new Date(Date.now() - 15 * 24 * 60 * 60 * 1000); // use your own stored date
// const newData = await fetchFromExportApi(lastFetchWasAt.toISOString());
// console.log(JSON.stringify(newData))
function removeLineBreaks(text) {
// remove tabs
return text.replace(/(\n)+/g, " / ");
}
// copy from tana helpers
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const nth = function (d) {
if (d > 3 && d < 21) return "th";
switch (d % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
};
const formatDate = (dateString) => {
const d = new Date(dateString);
const year = d.getFullYear();
const date = d.getDate();
const month = months[d.getMonth()];
const nthStr = nth(date);
return `${month} ${date}${nthStr}, ${year}`;
};
// for each element in array 'input', return all highlights.
// for each highlight, return the text, location, and the book title
function getHighlights() {
var highlights = [];
input.forEach((source) => {
source.highlights.forEach((highlight) => {
highlights.push({
text: removeLineBreaks(highlight.text),
// set category to book.category without the last character if it is an 's'
category:
source.category.slice(-1) === "s"
? source.category.slice(0, -1)
: source.category,
location: highlight.location,
book: source.title,
url: highlight.url,
tags: highlight.tags,
date: formatDate(highlight.highlighted_at),
highlighted_at: highlight.highlighted_at,
});
});
});
return highlights;
}
const highlights = getHighlights();
console.log("%%tana%%");
input.forEach((source) => {
const highlights = source.highlights;
const someBefore = highlights?.some((highlight) => {
return new Date(highlight.highlighted_at) < new Date(time);
});
if (highlights.length === 0) return;
if (someBefore) {
if (debug) {
console.log(
`- [[${source.title} #${
source.category.slice(-1) === "s"
? source.category.slice(0, -1)
: source.category
}-r]]`
);
}
} else {
console.log(
`- ${source.title} #${
source.category.slice(-1) === "s"
? source.category.slice(0, -1)
: source.category
}-r
- Author:: [[${source.author} #person-r]]`
);
// if source has a unique_url, print it
if (source.unique_url) console.log(` - URL:: ${source.unique_url}`);
// if source has book_tags, print them with a line break
if (source.book_tags.length > 0) {
console.log(" - Topics::");
source.book_tags.forEach((tag) => {
console.log(` - [[${tag.name} #tag-r]]`);
});
}
}
});
highlights.forEach((highlight) => {
if (new Date(highlight.highlighted_at) < new Date(time)) return;
console.log(
`- ${highlight.text} #highlight
- Source:: [[${highlight.book} #${highlight.category}-r]]`);
if (highlight.location < 1000) {
console.log(` - Location:: ${highlight.location} (page)`);
}
if (highlight.url) console.log(` - URL:: ${highlight.url}`);
console.log(` - Date:: [[${highlight.date}]]`
);
if (highlight.tags?.length > 0) {
console.log(" - Tags::");
highlight.tags.forEach((tag) => {
console.log(` - [[${tag.name} #tag-r]]`);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment