-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathScreen.ts
More file actions
50 lines (45 loc) · 1.64 KB
/
Copy pathScreen.ts
File metadata and controls
50 lines (45 loc) · 1.64 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
import {prepareData, TemplateDataType} from "Data/PrepareData.js";
import {PNGto1BIT} from "./PNGto1BIT.js";
import {TEMPLATE_FOLDER} from "Config.js";
import App from "Template/JSX/App.js";
import {renderToImage} from "./RenderHTML.js";
import {buildLiquid} from "./BuildLiquid.js";
import {buildJSX} from "./BuildJSX.js";
import crypto from "crypto";
import {readFileSync} from "node:fs";
const headerHtml = readFileSync(TEMPLATE_FOLDER + '/Header.html', 'utf8');
const screens = [
// you can leave one or add more
(data: TemplateDataType) => buildJSX(App, data),
(data: TemplateDataType) => buildLiquid('HackerNews', data),
];
export async function buildScreen() {
const randomScreen = screens[Math.floor(Math.random() * screens.length)];
const templateData = await prepareData();
const html = await randomScreen(templateData);
const image = await renderToImage(headerHtml + html);
return PNGto1BIT(image);
}
export async function getScreenHash() {
const image = await buildScreen();
return crypto.createHash('sha256').update(image).digest('hex');
}
export async function checkImageUrl(url: string): Promise<boolean> {
let response;
try {
response = await fetch(url);
} catch (error: any) {
console.error(`Failed to check image ${url} - ${error.message}`);
return false;
}
if (!response.ok) {
console.error(`Failed to check image ${url} - got ${response.status} code`);
return false;
}
const data = await response.text();
if (data.length < 1000) {
console.error(`Failed to check image ${url} - no content`);
return false;
}
return true;
}