-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.js
126 lines (118 loc) · 3.04 KB
/
helper.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
118
119
120
121
122
123
124
125
126
// Variables
const axios = require("axios");
const cheerio = require("cheerio");
const config = require("./config");
const Discord = require("discord.js");
const webhookClient = new Discord.WebhookClient(
config.webhookUrl.split("/")[5],
config.webhookUrl.split("/")[6]
);
completed = [];
if (config.cookie.length < 20) {
console.log(
`Scraping with a max of 100 pages as no cookie was set in config`
);
cookie = false;
} else {
cookie = config.cookie;
}
function getPostCount(account) {
console.log(` [Info] Account ${account}`);
axios
.get(account)
.then((response) => {
if (response.status === 200) {
const data = cheerio.load(response.data);
const t = data(".active").text();
newData = t.match(/\d+/g);
if (newData[1]) {
scrapePosts(
account,
Math.round((newData[0] + newData[1] - 32) / 20 + 2)
);
} else {
const calc = Math.round((newData[0] - 32) / 20) + 2;
if (calc < 1) {
scrapePosts(account, 1);
} else {
scrapePosts(account, calc);
}
}
}
})
.catch((e) => {
getPostCount(account);
});
}
function scrapePosts(result, maxPage) {
if (cookie === false && maxPage > 100) {
maxPage = 100;
}
if (cookie != false && maxPage > 1000) {
maxPage = 1000;
}
for (let i = 1; i < maxPage; i++) {
setTimeout(() => {
axios
.get(`${result}?page=${i}`, {
headers: {
Cookie: cookie ? `login_token=${cookie}` : null,
},
})
.then((resp) => {
console.log("\x1b[32m", `[Scraper] : Scraped ${resp.config.url}`);
const d = cheerio.load(resp.data);
d("a").each((_index, value) => {
if (d(value).attr("href").includes("entry"))
checkPost(d(value).attr("href"), result, resp.config.url);
});
})
.catch(() => {});
}, i * 1000);
}
}
function checkPost(url) {
axios
.get(`https://weheartit.com/${url}`)
.then((resp) => {
const data = cheerio.load(resp.data);
data(".cel img").each((_i, img) => {
if (
img.attribs.src.includes("avatar") ||
img.attribs.src.includes("weheartit") ||
img.attribs.src.includes("superthumb") ||
img.attribs.src.includes("ajax") ||
img.attribs.src.includes("pink_heart")
) {
return;
} else {
if (
completed.includes(
img.attribs.src.split("/images/")[1].split("/")[0]
)
) {
} else {
send(img.attribs.src);
completed.push(img.attribs.src.split("/images/")[1].split("/")[0]);
}
}
});
})
.catch(() => {
checkPost(url);
});
}
function send(url) {
webhookClient
.send(url, {
username: "icon",
avatarURL: "https://i.imgur.com/9aYk9Xz.png",
})
.catch(() => send(url));
}
setInterval(() => {
completed = [];
}, 120000);
module.exports = {
getPostCount,
};