forked from sibbl/hass-lovelace-kindle-screensaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
183 lines (161 loc) · 4.52 KB
/
index.js
File metadata and controls
183 lines (161 loc) · 4.52 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
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
const config = require("./config");
const path = require("path");
const http = require("http");
const { promises: fs } = require("fs");
const fsExtra = require("fs-extra");
const puppeteer = require("puppeteer");
const { CronJob } = require("cron");
const gm = require("gm");
(async () => {
if (config.rotation % 90 > 0) {
console.error("Invalid rotation value: " + config.rotation);
return;
}
const outputDir = path.dirname(config.outputPath);
await fsExtra.ensureDir(outputDir);
console.log("Starting browser...");
let browser = await puppeteer.launch({
args: [
"--disable-dev-shm-usage",
"--no-sandbox",
`--lang=${config.language}`,
],
headless: config.debug !== true,
});
console.log("Adding authentication entry to browser's local storage...");
let page = await browser.newPage();
await page.goto(config.baseUrl, {
timeout: config.renderingTimeout,
});
const hassTokens = {
hassUrl: config.baseUrl,
access_token: config.accessToken,
token_type: "Bearer",
};
await page.evaluate(
(hassTokens, selectedLanguage) => {
localStorage.setItem("hassTokens", hassTokens);
localStorage.setItem("selectedLanguage", selectedLanguage);
},
JSON.stringify(hassTokens),
JSON.stringify(config.language)
);
page.close();
if (config.debug) {
console.log(
"Debug mode active, will only render once in non-headless model and keep page open"
);
renderAndConvertAsync(browser);
} else {
console.log("Starting rendering cronjob...");
new CronJob({
cronTime: config.cronJob,
onTick: () => renderAndConvertAsync(browser),
start: true,
});
}
const httpServer = http.createServer(async (_, response) => {
try {
const data = await fs.readFile(config.outputPath);
response.writeHead(200, { "Content-Type": "image/png" });
response.end(data);
} catch (e) {
console.error(e);
response.writeHead(404);
response.end("Image not found");
}
});
const port = config.port || 5000;
httpServer.listen(port, () => {
console.log(`Server is running at ${port}`);
});
})();
async function renderAndConvertAsync(browser) {
const url = `${config.baseUrl}${config.screenShotUrl}`;
const outputPath = config.outputPath;
const tempPath = outputPath + ".temp";
console.log(`Rendering ${url} to image...`);
await renderUrlToImageAsync(browser, url, tempPath);
console.log(`Converting rendered screenshot of ${url} to grayscale png...`);
await convertImageToKindleCompatiblePngAsync(tempPath, outputPath);
fs.unlink(tempPath);
console.log(`Finished ${url}`);
}
async function renderUrlToImageAsync(browser, url, path) {
let page;
try {
page = await browser.newPage();
await page.emulateMediaFeatures([
{
name: "prefers-color-scheme",
value: "light",
},
]);
let size = {
width: Number(config.renderingScreenSize.width),
height: Number(config.renderingScreenSize.height)
};
if (config.rotation % 180 > 0) {
size = {
width: size.height,
height: size.width,
};
}
await page.setViewport(size);
await page.goto(url, {
waitUntil: ["domcontentloaded", "load", "networkidle0"],
timeout: config.renderingTimeout,
});
await page.addStyleTag({
content: `
body {
width: calc(${config.renderingScreenSize.width}px / ${config.scaling});
height: calc(${config.renderingScreenSize.height}px / ${config.scaling});
transform-origin: 0 0;
transform: scale(${config.scaling});
overflow: hidden;
}`,
});
if (config.renderingDelay > 0) {
await delay(config.renderingDelay);
}
await page.screenshot({
path,
type: "png",
clip: {
x: 0,
y: 0,
...size,
},
});
} catch (e) {
console.error("Failed to render", e);
} finally {
if (config.debug === false) {
await page.close();
}
}
}
function convertImageToKindleCompatiblePngAsync(inputPath, outputPath) {
return new Promise((resolve, reject) => {
gm(inputPath)
.options({
imageMagick: config.useImageMagick === true,
})
.rotate("white", config.rotation)
.type("GrayScale")
.bitdepth(config.grayscaleDepth)
.write(outputPath, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
function delay(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}