forked from tanainc/tana-paste-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadwise.mjs
executable file
·54 lines (45 loc) · 1.6 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
53
54
#!/usr/bin/env node
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Readwise
// @raycast.mode fullOutput
// Optional parameters:
// @raycast.icon ⚡️
// @raycast.argument1 { "type": "text", "placeholder": "Number of days" }
// @raycast.packageName Tana Paste
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) => {
console.log(` - ${highlight.text}`);
if (highlight.note) {
console.log(` - ${highlight.note}`);
}
});
});
};
const daysToFetch = process.argv[2];
console.log(`%%tana%%`);
getItemsFromReadwise(daysToFetch);