arde-packages 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of arde-packages might be problematic. Click here for more details.
- package/client.js +69 -0
- package/index.js +22 -0
- package/logs.js +75 -0
- package/package.json +14 -0
- package/supabase.js +76 -0
package/client.js
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
const express = require("express");
|
2
|
+
const axios = require("axios");
|
3
|
+
require("dotenv").config();
|
4
|
+
const logs = require("./logs");
|
5
|
+
let localUrl = null;
|
6
|
+
const type = process.env.SERVER_NAME;
|
7
|
+
|
8
|
+
const app = express();
|
9
|
+
|
10
|
+
app.get("/", (req, res) => {
|
11
|
+
res.send("Hello from " + process.env.SERVER_NAME);
|
12
|
+
});
|
13
|
+
|
14
|
+
app.get("/kill", (req, res) => {
|
15
|
+
setTimeout(() => {
|
16
|
+
process.exit(0);
|
17
|
+
}, 1000);
|
18
|
+
res.send("killed");
|
19
|
+
});
|
20
|
+
|
21
|
+
app.get("/restart", (req, res) => {
|
22
|
+
setTimeout(() => {
|
23
|
+
process.exit(1);
|
24
|
+
}, 1000);
|
25
|
+
res.send("restarted");
|
26
|
+
});
|
27
|
+
|
28
|
+
app.get("/ping", (req, res) => {
|
29
|
+
res.send("pong");
|
30
|
+
});
|
31
|
+
|
32
|
+
function addRoute(url, callback) {
|
33
|
+
app.get(url, callback);
|
34
|
+
}
|
35
|
+
|
36
|
+
async function getCurentUrl() {
|
37
|
+
localUrl = `http://localhost:${port}`;
|
38
|
+
let res = await axios.get("https://api.ipify.org?format=json");
|
39
|
+
if (process.env.ENV != "dev") {
|
40
|
+
localUrl = `http://${res.data.ip}:${port}`;
|
41
|
+
}
|
42
|
+
|
43
|
+
console.log(`Server url: http://${res.data.ip}:${port}`);
|
44
|
+
console.log(`Server url: http://localhost:${port}`);
|
45
|
+
logs(1, `Server ${process.env.SERVER_NAME} started on: http://${res.data.ip}:${port}`);
|
46
|
+
conectToServ();
|
47
|
+
return localUrl;
|
48
|
+
}
|
49
|
+
|
50
|
+
async function conectToServ() {
|
51
|
+
try {
|
52
|
+
await axios.post(process.env.SERVER_ADRESS + "/conect", {
|
53
|
+
url: localUrl,
|
54
|
+
type,
|
55
|
+
});
|
56
|
+
logs(3, "Conected to server");
|
57
|
+
} catch (error) {
|
58
|
+
logs(2, "error to conect with botManageur !!!", error);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
const port = process.env.PORT || 3000;
|
63
|
+
|
64
|
+
app.listen(port, async () => {
|
65
|
+
logs(2, "server started");
|
66
|
+
getCurentUrl();
|
67
|
+
});
|
68
|
+
|
69
|
+
module.exports = addRoute;
|
package/index.js
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
const logs = require("./logs");
|
2
|
+
const Supabase = require("./supabase");
|
3
|
+
const moment = require("moment");
|
4
|
+
const addRoute = require("./client");
|
5
|
+
|
6
|
+
async function wait(milliseconds) {
|
7
|
+
return new Promise((resolve) => {
|
8
|
+
setTimeout(() => {
|
9
|
+
resolve("secondes se sont écoulées");
|
10
|
+
}, milliseconds); // 10000 millisecondes équivalent à 10 secondes
|
11
|
+
});
|
12
|
+
}
|
13
|
+
|
14
|
+
function getCurentDate() {
|
15
|
+
const now = moment();
|
16
|
+
const utc = 2;
|
17
|
+
// YYYY-MM-DD
|
18
|
+
const formattedDate = now.utcOffset(60 * utc).format("YYYY-MM-DD");
|
19
|
+
return formattedDate;
|
20
|
+
}
|
21
|
+
|
22
|
+
module.exports = { logs, Supabase, wait, getCurentDate, addRoute };
|
package/logs.js
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
const moment = require("moment");
|
2
|
+
const { default: axios } = require("axios");
|
3
|
+
const dotenv = require("dotenv");
|
4
|
+
dotenv.config();
|
5
|
+
const TelegramBot = require("node-telegram-bot-api");
|
6
|
+
|
7
|
+
class Telegram {
|
8
|
+
constructor() {
|
9
|
+
this.chatId = 6070254981;
|
10
|
+
this.bot = new TelegramBot(process.env.TELEGRAM_KEY, { polling: false });
|
11
|
+
this.commandActionList = [];
|
12
|
+
}
|
13
|
+
async sendMessage(text, options) {
|
14
|
+
if (!text || text.length == 0) {
|
15
|
+
console.log("No text to send");
|
16
|
+
return;
|
17
|
+
}
|
18
|
+
this.bot.sendMessage(this.chatId, text, options);
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
const tl = new Telegram();
|
23
|
+
|
24
|
+
let localUrl = null;
|
25
|
+
let ipIsSet = false;
|
26
|
+
|
27
|
+
function logs(importance, message, error = "") {
|
28
|
+
const now = moment();
|
29
|
+
const utc = 2;
|
30
|
+
const formattedDate = now.utcOffset(60 * utc).format("DD/MM HH:mm:ss");
|
31
|
+
let errorText = error ? " : " + error : "";
|
32
|
+
let messageFormater = message.replace(/\n/g, "\n\t\t ");
|
33
|
+
|
34
|
+
if (typeof errorText == "object") {
|
35
|
+
errorText = JSON.stringify(errorText);
|
36
|
+
}
|
37
|
+
|
38
|
+
let log = formattedDate + " : " + messageFormater;
|
39
|
+
if (importance == 1) {
|
40
|
+
console.log("\x1b[31m" + log + errorText + "\x1b[0m");
|
41
|
+
if (process.env.ENV == "dev") {
|
42
|
+
console.log("\x1b[31m" + "No telegram message send du to local environement." + "\x1b[0m");
|
43
|
+
return;
|
44
|
+
}
|
45
|
+
try {
|
46
|
+
let message = `from : ${localUrl}\n${messageFormater}`;
|
47
|
+
// axios.post(process.env.SERVER_ADRESS + "/sendMessage", {
|
48
|
+
// message: messageFormater,
|
49
|
+
// url: localUrl,
|
50
|
+
// });
|
51
|
+
tl.sendMessage(message);
|
52
|
+
} catch (error) {
|
53
|
+
console.log("\x1b[31m" + formattedDate + " : error to send telegram message !!!" + "\x1b[0m");
|
54
|
+
}
|
55
|
+
}
|
56
|
+
if (importance == 2) {
|
57
|
+
console.log("\x1b[33m" + log + errorText + "\x1b[0m");
|
58
|
+
}
|
59
|
+
if (importance == 3) {
|
60
|
+
console.log(log + errorText);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
async function setLocalUrl() {
|
65
|
+
// localUrl = url;
|
66
|
+
if (ipIsSet) return;
|
67
|
+
ipIsSet = true;
|
68
|
+
let res = await axios.get("https://api.ipify.org?format=json");
|
69
|
+
const port = process.env.PORT || 3000;
|
70
|
+
localUrl = res.data.ip + ":" + port;
|
71
|
+
}
|
72
|
+
|
73
|
+
setLocalUrl();
|
74
|
+
|
75
|
+
module.exports = logs;
|
package/package.json
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
{
|
2
|
+
"name": "arde-packages",
|
3
|
+
"version": "0.0.2",
|
4
|
+
"main": "index.js",
|
5
|
+
"dependencies": {
|
6
|
+
"@supabase/supabase-js": "^2.33.1",
|
7
|
+
"axios": "^1.4.0",
|
8
|
+
"dotenv": "^16.3.1",
|
9
|
+
"express": "^4.18.2",
|
10
|
+
"moment": "^2.29.4",
|
11
|
+
"node-telegram-bot-api": "^0.61.0",
|
12
|
+
"pg": "^8.11.3"
|
13
|
+
}
|
14
|
+
}
|
package/supabase.js
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
const { createClient } = require("@supabase/supabase-js");
|
2
|
+
require("dotenv").config();
|
3
|
+
const { Pool } = require("pg");
|
4
|
+
const logs = require("./logs");
|
5
|
+
|
6
|
+
const pool = new Pool({
|
7
|
+
connectionString: process.env.POSTGRES_URL,
|
8
|
+
});
|
9
|
+
const supabaseUrl = process.env.SUPABASE_URL;
|
10
|
+
const supabaseKey = process.env.SUPABASE_KEY;
|
11
|
+
|
12
|
+
async function wait(second) {
|
13
|
+
return new Promise((resolve) => {
|
14
|
+
setTimeout(() => {
|
15
|
+
resolve("secondes se sont écoulées");
|
16
|
+
}, second * 1000);
|
17
|
+
});
|
18
|
+
}
|
19
|
+
class Supabase {
|
20
|
+
constructor() {
|
21
|
+
this.supabase = createClient(supabaseUrl, supabaseKey, {
|
22
|
+
auth: {
|
23
|
+
persistSession: false,
|
24
|
+
},
|
25
|
+
});
|
26
|
+
this.client = null;
|
27
|
+
this.haveCompanyWithUnscrapedUrl = true;
|
28
|
+
}
|
29
|
+
|
30
|
+
async init() {
|
31
|
+
this.client = await pool.connect();
|
32
|
+
await this.client.query("set statement_timeout to 60000000;");
|
33
|
+
}
|
34
|
+
|
35
|
+
async query(request) {
|
36
|
+
let res = null;
|
37
|
+
try {
|
38
|
+
res = await this.client.query(request);
|
39
|
+
} catch (err) {
|
40
|
+
logs(1, "Erreur lors de l'exécution de la requête :" + request, err.stack);
|
41
|
+
return null;
|
42
|
+
}
|
43
|
+
return res;
|
44
|
+
}
|
45
|
+
|
46
|
+
async upsert(table, data, errorNumber = 0) {
|
47
|
+
try {
|
48
|
+
let { error } = await this.supabase.from(table).upsert(data);
|
49
|
+
if (error) {
|
50
|
+
if (errorNumber < 3) {
|
51
|
+
wait(3);
|
52
|
+
logs(2, "error on upsert to the table : " + table, JSON.stringify(error));
|
53
|
+
console.error(error);
|
54
|
+
return await this.upsert(table, data, errorNumber + 1);
|
55
|
+
} else {
|
56
|
+
logs(1, "More than 3 error on upsert to the table : " + table, JSON.stringify(error));
|
57
|
+
logs(2, JSON.stringify(data));
|
58
|
+
}
|
59
|
+
}
|
60
|
+
} catch (error) {
|
61
|
+
if (errorNumber < 3) {
|
62
|
+
wait(3);
|
63
|
+
logs(2, "catch on upsert to the table : " + table, error);
|
64
|
+
return await this.upsert(table, data, errorNumber + 1);
|
65
|
+
} else {
|
66
|
+
logs(1, "More than 3 catch on upsert to the table : " + table, error);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
async destroy() {
|
72
|
+
this.client.end();
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
module.exports = Supabase;
|