-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseds_function.js
46 lines (40 loc) · 1.23 KB
/
baseds_function.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
const axios = require("axios");
const cheerio = require("cheerio");
const readline = require("readline");
const fs = require("fs").promises;
async function readFile(filePath) {
try {
const data = await fs.readFile(filePath);
return JSON.parse(data);
} catch (error) {
console.error(`Got an error trying to read the file: ${error.message}`);
}
}
async function writeFile(filePath, data) {
try {
await fs.writeFile(filePath, JSON.stringify(data), function (err) {
if (err) return console.log(err);
console.log(`Hello World > ${filePath}`);
});
} catch (error) {
console.error(`Got an error trying to read the file: ${error.message}`);
}
}
async function initial_cheerio(url) {
try {
const { data } = await axios.get(url);
return cheerio.load(data);
} catch (e) {
return undefined;
}
}
function clearStr(i) {
readline.moveCursor(process.stdout, 0, -i);
readline.clearScreenDown(process.stdout);
}
function getRandomArbitrary(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
module.exports = { initial_cheerio, clearStr, getRandomArbitrary, readFile, writeFile };