-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapper.js
110 lines (98 loc) · 2.81 KB
/
scrapper.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
const puppeteer = require("puppeteer");
const { db } = require("./config");
//Bot test pass
const preparePageForTests = async (page) => {
const userAgent =
"Mozilla/5.0 (X11; Linux x86_64)" +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36";
await page.setUserAgent(userAgent);
};
async function runScrapper(address, body) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await preparePageForTests(page);
await page.goto(address);
//get token balance in USD
const tokenBalanceUSD = await page.evaluate(() => {
const docSelector = document.querySelector("#availableBalanceDropdown");
if (!docSelector) {
return 0;
}
const docText = docSelector.innerText;
//sanitate the string
const pureNum = docText.split(" ")[0];
const num = pureNum
.replaceAll("$", "")
.replaceAll(">", "")
.replaceAll(",", "");
return Number(num);
});
//get eth balance in USD
const ethBalanceUSD = await page.evaluate(() => {
const docSelector = document.querySelector(
"#ContentPlaceHolder1_divSummary > div.row.mb-4 > div.col-md-6.mb-3.mb-md-0 > div > div.card-body > div:nth-child(3) > div.col-md-8"
);
if (!docSelector) {
return 0;
}
const docText = docSelector.innerText;
const pureNum = docText.split(" ")[0];
const num = pureNum
.replaceAll("$", "")
.replaceAll(">", "")
.replaceAll(",", "");
return Number(num);
});
//get tokens count
const tokensCount = await page.evaluate(() => {
const docSelector = document.querySelector(
"#availableBalanceDropdown > span"
);
if (!docSelector) {
return 0;
}
const docText = docSelector.innerText;
const num = docText
.replaceAll("$", "")
.replaceAll(">", "")
.replaceAll(",", "");
return Number(num);
});
//get trx count
const trxCount = await page.evaluate(() => {
const docSelector = document.querySelector(
"#transactions > div.d-md-flex.align-items-center.mb-3 > p > a:nth-child(2)"
);
if (!docSelector) {
return 0;
}
const docText = docSelector.innerText;
const num = docText
.replaceAll("$", "")
.replaceAll(">", "")
.replaceAll(",", "");
return Number(num);
});
const balance = tokenBalanceUSD + ethBalanceUSD;
const tokens = tokensCount;
const trx = trxCount;
//const lastin = "";
//const lastout = "";
//check data validity
if (balance + tokens + trx > 0) {
//send to firestore
const userJson = {
balance,
tokens,
trx,
};
db.collection("wallets").doc(body.address).set(userJson);
console.log(body);
}
if (balance + tokens + trx < 1) {
//throw error
console.log("nothin here");
}
await browser.close();
}
module.exports = runScrapper;