-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.js
More file actions
140 lines (126 loc) · 3.57 KB
/
Copy pathexport.js
File metadata and controls
140 lines (126 loc) · 3.57 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require('dotenv').config();
const nodemailer = require('nodemailer');
const readwiseToken = process.env.READWISE_API_KEY;
const senderEmail = process.env.SENDER_EMAIL;
const emailPassword = process.env.EMAIL_PASSWORD;
const recipientEmails = process.env.RECIPIENT_EMAILS;
const fetchFromExportApi = async (updatedAfter = null) => {
const queryParams = new URLSearchParams();
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 ${readwiseToken}`,
},
});
const responseJson = await response.json();
return responseJson.results;
};
const generateHtml = async () => {
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
const data = await fetchFromExportApi(oneWeekAgo.toISOString());
const htmlParts = [];
htmlParts.push(htmlHeader);
for (const book of data) {
htmlParts.push(renderBook(book));
}
htmlParts.push(htmlFooter);
return htmlParts.join('');
};
const sendEmail = async () => {
const html = await generateHtml();
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: senderEmail,
pass: emailPassword,
},
});
const mailOptions = {
from: senderEmail,
to: recipientEmails,
subject: 'Weekly Readwise Export',
html: html,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.error('Error sending email:', error);
}
console.log('Email sent successfully:', info.response);
});
};
const htmlHeader = `<!DOCTYPE html>
<html>
<head>
<title>Books and Highlights</title>
<style>
.book {
border: 1px solid #ccc;
padding: 15px;
margin-bottom: 15px;
border-radius: 4px;
display: flex;
flex-wrap: wrap;
}
.left-column {
flex: 0 0 200px;
margin-right: 15px;
}
.left-column img {
max-width: 100%;
height: auto;
display: block;
}
.right-column {
flex: 1;
}
.highlights {
margin-top: 10px;
}
.highlights ul {
list-style-type: disc;
padding-left: 20px;
}
.highlights li {
margin-bottom: 10px;
}
</style>
</head>
<body>`;
const renderBook = (book) => `
<div class="book">
<div class="left-column">
<img src="${book.cover_image_url}" alt="${book.readable_title} cover">
</div>
<div class="right-column">
<h2>${book.readable_title}</h2>
<p><strong>Author:</strong> ${book.author}</p>
<div class="highlights">
<h3>Highlights:</h3>
<ul>
${book.highlights.map(renderHighlight).join('')}
</ul>
</div>
</div>
</div>
`;
const renderHighlight = (highlight) => `
<li>
<p>
<strong>
${highlight.highlighted_at ? '⭐' : ''}
Text:
</strong>
${highlight.text}
</p>
${highlight.note.length ? `<p><strong>Note:</strong> ${highlight.note}</p>` : ''}
<p>
<strong>Readwise URL:</strong>
<a href="${highlight.readwise_url}">${highlight.readwise_url}</a>
</p>
</li>
`;
const htmlFooter = '</body></html>';
sendEmail();