-
Notifications
You must be signed in to change notification settings - Fork 19
/
readwise.mjs
52 lines (45 loc) · 1.54 KB
/
readwise.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import fetch from "isomorphic-fetch";
import { config } from "./config.mjs";
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);
const response = await fetch(
`${HIGHLIGHTS_URL}?updatedAfter=${updatedAfterDate.toISOString()}`,
{
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}`);
}
});
if (highlight.note) {
console.log(` - ${highlight.note}`);
}
});
});
};
const daysToFetch = process.argv[2];
console.log(`%%tana%%`);
getItemsFromReadwise(daysToFetch);