-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (71 loc) · 3.75 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
import { readFileSync, writeFileSync, existsSync } from 'fs';
import puppeteer from 'puppeteer';
import { WebhookClient, EmbedBuilder } from 'discord.js';
const config = JSON.parse(readFileSync('config.json', 'utf-8'));
if (!config.webhookUrl)
throw new Error('Missing webhook URL');
if (!existsSync('ids.txt'))
writeFileSync('ids.txt', '');
const oldIds = readFileSync('ids.txt', 'utf-8'), newIds = [];
const templateImage = readFileSync('image.txt', 'utf-8').substring(0, 50);
const webhookClient = new WebhookClient({ url: config.webhookUrl });
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.setJavaScriptEnabled(true);
let searchOptions = Object.entries(config.searchOptions).map(([key, value]) => key + '=' + value).join('&');
await page.goto('https://gsaauctions.gov/auctions/auctions-list?page=1&size=50&searchType=ALL_WORDS&advanced=true&' + searchOptions, { waitUntil: 'networkidle0' });
let pageNumber = 1;
const messages = [];
while (true) {
console.log('Page ' + pageNumber);
const elements = await page.$$('div.item-search-result-wrapper');
for (const element of elements) {
const title = await element.$('header > div > div > h3 > a');
const id = await title.evaluate(el => el.textContent);
newIds.push(id);
if (oldIds.includes(id)) {
console.log('Skipping ' + id);
continue;
}
console.log('Scraping ' + id);
const url = await title.evaluate(el => el.href);
let image = await (await element.$('div.usa-card__media > div.usa-card__img > img')).evaluate(el => el.src);
image = image.substring(image.indexOf(',') + 1);
const template = image.startsWith(templateImage);
if (!template)
writeFileSync('image.png', image, 'base64');
const descriptionElement = await element.$('div.usa-card__body > div > ul');
const name = (await (await descriptionElement.$('li.auction-name')).evaluate(el => el.textContent)).substring(10);
const location = (await (await descriptionElement.$('li.auction-location')).evaluate(el => el.textContent)).substring(10);
const bidders = (await (await descriptionElement.$('li:nth-child(4)')).evaluate(el => el.textContent)).substring(16);
const currentBid = (await (await descriptionElement.$('li:nth-child(5)')).evaluate(el => el.textContent)).substring(13);
let closing = (await (await descriptionElement.$('li:nth-child(3)')).evaluate(el => el.textContent)).substring(14);
closing = closing.substring(0, closing.lastIndexOf(' '));
closing = new Date(closing);
const embed = new EmbedBuilder()
.setTitle(name)
.setDescription(`Location: ${location}\nClosing: ${closing.getMonth() + 1}/${closing.getDate()}/${closing.getFullYear()}\nBidders: ${bidders}\nCurrent Bid: ${currentBid}`)
.setURL(url)
.setColor('#0099ff');
messages.push(webhookClient.send({
content: '',
username: config.username || 'GSA Auctions',
avatarURL: config.avatarURL || 'https://www.netizen.net/media/gsa.jpeg',
embeds: [embed],
files: template ? [] : ['./image.png']
}));
}
let showing = await (await page.$('div.show-count > span > strong')).evaluate(el => el.textContent);
showing = parseInt(showing.substring(showing.lastIndexOf(' ') + 1));
if (showing > pageNumber * 50) {
pageNumber++;
await (await page.$('button#next-page-button-top')).click();
await page.waitForSelector('div.usa-card__media > div.usa-card__img > img', { visible: true });
}
else
break;
}
await Promise.all(messages);
writeFileSync('ids.txt', newIds.join('\n'));
await page.close();
await browser.close();