Runs every 15 min
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
import { fetchReadwiseList } from "https://esm.town/v/ofalvai/fetchReadwiseList?v=3";
import { blob } from "https://esm.town/v/std/blob";
import { set } from "https://esm.town/v/std/set?v=11";
import { addDays } from "https://esm.town/v/vlad/addDays";
import { formatDateToRoam } from "https://esm.town/v/vlad/formatDateToRoam";
import { sendMatrixChatRoomTextMessage } from "https://esm.town/v/vlad/sendMatrixChatRoomTextMessage";
import process from "node:process";
export const watchReaderAndSendToMatrix = async () => {
// plausibly this should be a roam plugin for better results =\
// or I should actually do the thing where message threads become hierarchies of block :p
//
const lastProcessedReadwiseItemDate = (await blob.getJSON("lastProcessedReadwiseItemDate")).date;
console.log({ lastProcessedReadwiseItemDate });
const itemList = await fetchReadwiseList(
process.env.readwise,
"new",
);
const lastSavedIdx = itemList.results.findIndex((it) =>
new Date(it.created_at)
<= new Date(lastProcessedReadwiseItemDate)
);
const newItemsToSave: Array<any> = itemList.results.slice(0, lastSavedIdx);
console.log(`saving ${newItemsToSave.length} articles`, newItemsToSave);
//
const tomorrow = await formatDateToRoam(
addDays(new Date(), 1),
);
//
const messageFromReadwiseItem = (item) =>
`[[${item.title}]] by ${item.author} \n url: ${item.url} \nvia ${item.source_url}
\n ${item.summary}
\n [[to/read]] [[${tomorrow}]]`;
//
for (const it of newItemsToSave) {
await sendMatrixChatRoomTextMessage(
process.env.matrixOpenWhisperer,
process.env.matrixRoamSyncRoomId,
messageFromReadwiseItem(it),
);
}
if (newItemsToSave[0]) {
await blob.setJSON("lastProcessedReadwiseItemDate", { date: newItemsToSave[0].created_at });
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 29, 2024