-
Notifications
You must be signed in to change notification settings - Fork 0
/
unFilterImages.js
117 lines (94 loc) · 3.44 KB
/
unFilterImages.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
require("dotenv").config();
const { default: connect, connection } = require("./functions/connect");
const AtomsModel = require("./model/Atoms.model");
const path = require("path");
const fs = require("fs");
const puppeteer = require("puppeteer-core");
const { setting, log } = require("./functions/logger");
setting({
root: path.join(__dirname, "assets"),
file: "download.log",
});
connect(process.env.MONGOURI).then(() => {
AtomsModel.find({}).then(async (Atoms) => {
log("Action started", "success");
setInterval(() => {
log("--> Changing...", "debug");
}, 10000);
const root = path.join("assets");
Atoms = Atoms.filter((Atom) => Atom.bohr_model_image !== null);
const edgePaths = await import("edge-paths");
const MSEDGE = edgePaths.getEdgePath();
const browser = await puppeteer.launch({ executablePath: MSEDGE });
const page = await browser.newPage();
const source = Atoms.map((Atom) => {
if (
Atom.bohr_model_image !== null &&
!Atom.bohr_model_image.includes("https://i.postimg.cc")
) {
const { id, name, bohr_model_image } = Atom;
return { id, name, bohr_model_image };
}
}).filter((value) => value !== undefined);
if (source.length > 0) {
fs.writeFileSync(
path.join(root, "sources", "unfilter.json"),
JSON.stringify(source)
);
await page.goto("https://postimages.org/web");
// Wait for the input field to be visible and enter the URLs
await page.waitForSelector("textarea#links");
const links = source.map((Atom) => Atom.bohr_model_image);
log("Links are fully saved", "success");
const linksText = links.join("\n");
await page.type("textarea#links", linksText);
// Click the "Upload" button
await page.click("a.btn");
// Wait for the page to redirect
await page.waitForNavigation({ timeout: 4 * 60 * 1000 });
if (Atoms.length > 1) {
// Select an option from the select tag
await page.select("select#embed_box", "code_direct");
}
const time = new Date(Date.now());
await page.screenshot({
path: path.join(
root,
"screenshots",
`${time.getFullYear()}-${time.getMonth()}-${time.getDay()}-${time.getTime()}.jpg`
),
});
// Get the value of the text area
const textAreaValue = await page.evaluate(() => {
/* eslint-disable no-undef */
const textArea =
document?.querySelector("textarea#code_box") ||
document?.querySelector("input#code_direct");
return textArea.value.split(/\n/gim);
/* eslint-enable no-undef */
});
textAreaValue.pop();
fs.writeFileSync(
path.join(root, "jsons", "unfilter-links.json"),
JSON.stringify(textAreaValue)
);
const AllAtoms = await AtomsModel.find({}).sort({ number: 1 });
textAreaValue.forEach((link) => {
const index = +link.slice(link.lastIndexOf("/")).split("-")[1] - 1;
AllAtoms[index].bohr_model_image = link;
});
log("Images links are unfilterd...", "success");
fs.writeFileSync(
path.join(root, "jsons", "unfilter.json"),
JSON.stringify(AllAtoms)
);
await AtomsModel.create(AllAtoms);
log("Images links are saved...", "success");
} else {
log("Datas are up-to-date", "info");
}
await browser.close();
connection.close();
process.exit(1);
});
});