-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
70 lines (57 loc) · 2.18 KB
/
index.mjs
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
import Apify from "apify";
import { parseHTML } from "linkedom";
import fetch from "node-fetch";
import postmark from "postmark";
Apify.main(async () => {
const { postmarkToken, toEmail } = (await Apify.getInput()) ?? {};
const requestList = await Apify.openRequestList(null, [
{ url: "https://www.dji.com/cz/downloads/djiapp/dji-fly" },
]);
const kvStore = await Apify.openKeyValueStore("dji-fly-version");
const prevVersion = await kvStore.getValue("current");
const crawler = new Apify.BasicCrawler({
requestList,
async handleRequestFunction({ request, session, crawler }) {
const resp = await fetch(request.url);
const { document } = parseHTML(await resp.text());
const currentVersion = document.querySelector(
"[data-ga-label=android]+.method-title"
).textContent;
if (currentVersion === prevVersion) return;
Apify.utils.log.info("New version found", { currentVersion });
const promises = [];
promises.push(kvStore.setValue("current", currentVersion));
const downloadURL = document
.querySelector("a[data-ga-label=android]")
.getAttribute("href");
const betaURL = Array.from(document.querySelectorAll("a[href]"))
.filter((x) => x.textContent === "Android 12 Beta")
.map((x) => x.getAttribute("href"))
.pop();
const client = new postmark.ServerClient(postmarkToken);
promises.push(
client.sendEmail({
From: "[email protected]",
To: toEmail,
Subject: "New version of DJI Fly app",
TextBody: `New version ${currentVersion} available at ${request.url}`,
HtmlBody: `Hello,
<p>There is a new version of DJI Fly app ${currentVersion}. You can check it on ${
request.url
} or directly
<a href="${downloadURL}">download APK</a>.
${
betaURL
? `<p>If you are looking for an Android 12 Beta build <a href="${betaURL}">download APK</a>`
: ""
}
<p>Have a nice day
`,
MessageStream: "outbound",
})
);
await Promise.allSettled(promises);
},
});
await crawler.run();
});