-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (130 loc) · 3.67 KB
/
index.js
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
const puppeteer = require("puppeteer");
const nconf = require("nconf");
const Jimp = require("jimp");
const fs = require("fs");
nconf.argv().env().file({
file: "./config.json",
});
const getPM25 = async (page, screenshotFileName) => {
console.log("Getting PM2.5");
await page.waitForTimeout(100000);
return await page.screenshot({
path: `${screenshotFileName}_pm2.5.png`,
});
};
const getPM10 = async (page, screenshotFileName) => {
console.log("Getting PM10");
let elem = await page.$(".select-selected");
let selector = await elem.boundingBox();
await page.mouse.click(selector.x + selector.width / 2, selector.y + selector.height / 2);
elem = await page.$("#select-item-PM10");
selector = await elem.boundingBox();
await page.mouse.click(selector.x + selector.width / 2, selector.y + selector.height / 2);
page.waitForTimeout(50000);
return await page.screenshot({
path: `${screenshotFileName}_pm10.png`,
});
};
const getMonth = {
// 1: "Января",
// 2: "Февраля",
// 3: "Марта",
// 4: "Апреля",
// 5: "Мая",
// 6: "Июня",
// 7: "Июля",
// 8: "Августа",
// 9: "Сентября",
// 10: "Октября",
// 11: "Ноября",
// 12: "Декабря",
1: "Jan",
2: "Feb",
3: "Mar",
4: "Apr",
5: "May",
6: "Jun",
7: "Jul",
8: "Aug",
9: "Sep",
10: "Oct",
11: "Nov",
12: "Dec",
};
const writeImage = async (screenshotFileName, name) => {
// добавление надписей
const image = await Jimp.read(`${screenshotFileName}`);
const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
let today = new Date();
let minutes = today.getMinutes() + "";
if (minutes.length <= 1) {
minutes = `0${minutes}`;
}
let hours =
today.getDate() +
"-" +
getMonth[today.getMonth() + 1] +
"-" +
today.getFullYear() +
" " +
today.getHours() +
":" +
minutes;
console.log(minutes);
image.print(font, 800, 10, name + " " + hours);
await image.write("./tmp/t.png");
// логотип гражданской науки
let watermark = await Jimp.read(nconf.get("logo"));
watermark = watermark.resize(100, 100); // Resizing watermark image
const image1 = await Jimp.read("./tmp/t.png");
watermark = await watermark;
image1.composite(watermark, 1948, 0, {
mode: Jimp.BLEND_SOURCE_OVER,
opacityDest: 1,
opacitySource: 0.5,
});
const font1 = await Jimp.loadFont(Jimp.FONT_SANS_12_BLACK);
image1.print(font1, nconf.get("logoText").x, nconf.get("logoText").y, nconf.get("logoText").text);
return await image1.writeAsync(`${screenshotFileName}.ready.png`);
};
const start = async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
process.on("SIGINT", function () {
browser.close();
process.exit(err ? 1 : 0);
});
// directory for current date
let today = new Date();
let dir =
nconf.get("screenshotsPath") +
today.getDate() +
"-" +
getMonth[today.getMonth() + 1] +
"-" +
today.getFullYear();
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {});
}
const screenshotFileName = new Date().getTime();
console.log(screenshotFileName);
await page.setViewport({
width: 2048,
height: 1080,
deviceScaleFactor: 1,
});
await page.goto(nconf.get("url"));
if (nconf.get("getPM2.5")) {
await getPM25(page, `${dir}/${screenshotFileName}`);
writeImage(`${dir}/${screenshotFileName}_pm2.5.png`, "PM 2.5");
}
if (nconf.get("getPM10")) {
await getPM10(page, `${dir}/${screenshotFileName}`);
writeImage(`${dir}/${screenshotFileName}_pm10.png`, "PM 10");
}
browser.close();
};
setInterval(() => {
start();
}, 120000);
start();