forked from agrmohit/omnivore-epub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
184 lines (152 loc) · 5.31 KB
/
main.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { load } from "https://deno.land/[email protected]/dotenv/mod.ts";
import { Omnivore } from "npm:@omnivore-app/api";
import epub, { Chapter } from "npm:epub-gen-memory";
import sanitizeHtml from "npm:sanitize-html";
import config from "./config.json" with { type: "json" };
import { sendEmail } from "./email.ts";
const currentVersion = "v0.6.1";
console.log(`ℹ Omnivore EPUB ${currentVersion}`);
console.log("ℹ️ Homepage: https://github.com/agrmohit/omnivore-epub");
const env = await load();
config.token = env["TOKEN"];
config.emailUser = env["EMAIL_USER"];
config.emailPassword = env["EMAIL_PASSWORD"];
config.emailRecipient = env["EMAIL_RECIPIENT"];
if (!config.token) {
console.log("❌ Omnivore API token not set");
console.log(
"❌ Get a token following instructions on: https://docs.omnivore.app/integrations/api.html#getting-an-api-token",
);
console.log("❌ When you have a token, insert it as value for 'token' field in 'config.json' file");
Deno.exit(1);
}
async function checkForUpdates() {
let response;
try {
response = await fetch("https://api.github.com/repos/agrmohit/omnivore-epub/releases/latest");
} catch (error) {
console.error("🚫 Error: Unable to connect. Please check your internet connection");
console.error(`🚫 Error: ${error}`);
Deno.exit(1);
}
const latestRelease = await response.json();
const latestReleaseTagName = latestRelease.tag_name;
if (latestReleaseTagName !== currentVersion && latestReleaseTagName !== undefined) {
console.log("ℹ New update available");
console.log(`ℹ ${currentVersion} --> ${latestReleaseTagName}`);
if (config.showReleaseNotes) {
console.log("ℹ Release Notes:");
console.log(latestRelease.body);
}
console.log("🌐 View on Web: https://github.com/agrmohit/omnivore-epub/releases/latest");
}
}
function sanitizeContent(content: string | null) {
let allowedTags;
if (config.allowImages) {
allowedTags = sanitizeHtml.defaults.allowedTags.concat(["img"]);
} else {
allowedTags = sanitizeHtml.defaults.allowedTags.concat();
}
const sanitizedContent = sanitizeHtml(content, {
allowedTags: allowedTags,
});
return sanitizedContent;
}
function sleep(milliseconds: number) {
return new Promise((resolve) => {
setTimeout(() => {
resolve({});
}, milliseconds);
});
}
async function makeEbook() {
const omnivore = new Omnivore({
apiKey: config.token,
baseUrl: config.endpoint,
});
const ignoredLabelsQuery = `-label:${config.ignoredLabels.join(",")}`;
let endCursor = 0;
const chapters: Chapter[] = [];
const batchSize = 60;
let totalProcessed = 0;
let totalSkipped = 0;
let libraryTotal = 0;
while (endCursor < config.maxArticleCount) {
if (endCursor !== 0) {
console.log("💤 Sleeping for 1 minute");
await sleep(60_000);
console.log("🌅 Woke up from sleep");
}
const articlesToFetch = (config.maxArticleCount - endCursor > batchSize)
? batchSize
: config.maxArticleCount - endCursor;
console.log(`〰️Fetching ${articlesToFetch} articles`);
const articles = await omnivore.items.search({
first: articlesToFetch,
includeContent: true,
format: "html",
query: `${config.searchQuery} ${ignoredLabelsQuery}`,
after: endCursor,
});
console.log("🤖 done");
endCursor = Number(articles.pageInfo.endCursor);
for (const edge of articles.edges) {
const article = edge.node;
console.log(`🌐 Processing ${article.title}`);
let content = sanitizeContent(article.content);
if (
config.ignoredLinks.some((link) => article.url.includes(link))
) {
console.log("⚠️ Article skipped: Matched ignored link");
totalSkipped += 1;
continue;
}
if (article.labels?.length) {
if (config.addLabelsInContent) {
const labels = article.labels.map((label) => label.name);
content = `<b>Labels: ${labels.join(", ")}</b>` + content;
}
}
if (config.addArticleLinkInContent) {
content = `<a href="${article.url}">Link to Article</a><br><br>` + content;
}
chapters.push({
title: article.title,
author: article.author ?? "",
content: content,
filename: article.slug,
});
console.log(`✅ done`);
}
totalProcessed += articles.edges.length;
libraryTotal = Number(articles.pageInfo.totalCount);
if (!articles.pageInfo.hasNextPage) break;
}
console.log(`🤖 Processed ${totalProcessed} articles out of ${libraryTotal} in your library`);
console.log(`🤖 ${totalSkipped} skipped`);
console.log(`📚 Creating ebook (${config.outputFileName})`);
const fileBuffer = await epub.default(
{
title: config.title,
author: config.author,
cover: config.cover,
description: config.description,
ignoreFailedDownloads: true,
},
chapters,
);
await Deno.writeFile(config.outputFileName, fileBuffer);
console.log("📔 Successfully created ebook");
}
if (config.updateCheck) {
await checkForUpdates();
} else {
console.log("🌐 Update checks are disabled");
console.log("🌐 You can manually check for updates here: https://github.com/agrmohit/omnivore-epub/releases");
}
await makeEbook();
if (config.emailSupport) {
await sendEmail();
}
Deno.exit();