-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
334 lines (250 loc) · 11.2 KB
/
index.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
require('dotenv').config();
// Script to access Fiverr "Buyer Requests" page
const puppeteerExtra = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const TelegramBot = require('node-telegram-bot-api');
const cron = require('node-cron');
const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, { polling : true });
let pollingError = false;
let running = false;
bot.on("polling_error", () => {
console.log("Polling error. The script will not work properly with 2 or more instances. Please run only 1 instance of the script.");
pollingError = true;
});
if(!pollingError) {
console.log("Script started");
cron.schedule('*/5 * * * *', () => {
console.log("Scrapping ...");
bot.sendMessage(process.env.CHAT_ID,"Scrapping ...");
bot.stopPolling();
(async() => {
// Start
running = true;
puppeteerExtra.use(StealthPlugin());
const browser = await puppeteerExtra.launch({ headless: false }); // headless: true -> don't show browser
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(0);
await page.setViewport({ width: 1366, height: 768});
await page.goto("https://www.fiverr.com/login?source=top_nav", { waitUntil: "domcontentloaded" });
await page.click(".icon-button.social-signing-button.google-signing-button");
// Logs in
// Only works if google 2fa is disabled
let pages = new Array(3).fill(null);
while(pages[2] == null) {
pages = await browser.pages();
await new Promise(r => setTimeout(r, 3000));
}
await new Promise(r => setTimeout(r, 6000));
await pages[2].keyboard.type(process.env.EMAIL, { delay: 50 });
await pages[2].click('#identifierNext');
await new Promise(r => setTimeout(r, 6000));
await pages[2].keyboard.type(process.env.PASSWORD, { delay: 50 });
await pages[2].click('#passwordNext');
await new Promise(r => setTimeout(r, 10000));
// Check if Log step was succesful. The page will close if authentication was sucessful.
// If couldn't log in, so the page is still opened
pages = await browser.pages();
if(pages[2]) {
console.log("An error occured while trying to login. I will try again in a few minutes.")
bot.sendMessage(process.env.CHAT_ID,"Couldn't Log In ... I'll try again later ... ");
browser.close();
running = false;
bot.startPolling();
return 0;
}
// After logged in, go to "Buyer Requests" page
let buyerRequestsURL = "https://www.fiverr.com/users/" + process.env.FIVERR_USERNAME + "/requests?source=header_nav";
await page.goto(buyerRequestsURL, { waitUntil: "domcontentloaded" });
// Grab requests, click on "See More" link, and return all requests opened
let myList = null;
while(!myList) {
// First, grab all requests which needs to be opened
await new Promise(r => setTimeout(r, 2000));
let grabRequests = await page.evaluate(() => {
let infos = document.querySelectorAll(".db-new-main-table.align-top.js-db-table table tbody tr .see-more .ellipsis.text-wrap span");
let listOfRequests = [];
infos.forEach((info) => {
listOfRequests.push(info.innerText)
});
return listOfRequests;
});
myList = grabRequests;
}
let done = null;
while(!done) {
// Then click on every single "See More"
let gigs = await page.$$(".db-new-main-table.align-top.js-db-table table tbody tr .see-more .ellipsis.text-wrap span a");
for (const requestIndex of myList.keys()) {
if(gigs[requestIndex]) {
await gigs[requestIndex].click();
done = true;
}
}
}
let myList2 = null;
while(!myList2) {
// Finally grab all the requests and other infos, now that everything is opened
let grabAllRequests = await page.evaluate(() => {
let dates = document.querySelectorAll(".db-new-main-table.align-top.js-db-table table tbody tr td:nth-child(1) span");
let infos = document.querySelectorAll(".db-new-main-table.align-top.js-db-table table tbody tr td:nth-child(3) span:nth-child(1)");
let prices = document.querySelectorAll(".db-new-main-table.align-top.js-db-table table tbody tr td:nth-child(6) .hover-hide span");
let prices2 = document.querySelectorAll(".db-new-main-table.align-top.js-db-table table tbody tr td:nth-child(6) .hover-show .budget");
let allDates = [];
let allRequests = [];
let allPrices = [];
let allPrices2 = [];
dates.forEach((date) => {
allDates.push(date.innerText)
});
infos.forEach((info) => {
allRequests.push(info.innerText)
});
prices.forEach((price) => {
allPrices.push(price.innerText)
});
prices2.forEach((price2) => {
allPrices2.push(price2.innerText)
});
// Object model (for first page - 15 requests)
const myData = [
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
},
{
date : "",
info : "",
price : ""
}
]
for (const index of myData.keys()) {
myData[index].date = allDates[index];
myData[index].info = allRequests[index];
myData[index].price = allPrices[index];
if(allPrices[index] == "") {
myData[index].price = allPrices2[index];
}
}
return myData;
});
myList2 = grabAllRequests;
}
// Send all requests from the first page
await new Promise(r => setTimeout(r, 6000));
const FileSystem = require("fs");
let oldData = FileSystem.readFileSync('file.json', "utf8");
let objOldData = JSON.parse(oldData)
let newRequest = false;
if(objOldData[0].info != myList2[0].info) {
console.log("New request available");
await bot.sendMessage(process.env.CHAT_ID,"NEW REQUEST AVAILABLE!");
newRequest = true;
} else {
console.log("No new request");
await bot.sendMessage(process.env.CHAT_ID,"NO NEW REQUEST!");
}
FileSystem.writeFile('file2.json', JSON.stringify(myList2), (error) => {
if (error) throw error;
});
/* maybe will be useful later when we compare the old json file with new to check for new inputs
for (const requestIndex of myList2.keys()) {
if(myList2[requestIndex]) {
await bot.sendMessage("1332870599", myList2[requestIndex].toString());
}
}
*/
// If there is new request, it will send the file and overwrites old file for new comparison
if(newRequest) {
await bot.sendDocument(process.env.CHAT_ID, 'file.json');
FileSystem.writeFile('file.json', JSON.stringify(myList2), (error) => {
if (error) throw error;
});
}
await browser.close();
console.log("Closing ...")
await bot.startPolling();
running = false;
})();
});
// You can check if the bot is working by sending a message
bot.on('message', (msg) => {
var Hi = "chatid"
if(msg.text.toString().toLowerCase().indexOf(Hi) === 0) {
bot.sendMessage(msg.chat.id,msg.chat.id);
}
else{
if(running == false) {
bot.sendMessage(msg.chat.id,"Wait, I'll start scrapping soon");
}
else {
bot.sendMessage(msg.chat.id,"Hold on, I'm scrapping");
}
}
});
}