-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapeStubhub.js
90 lines (75 loc) · 2.43 KB
/
scrapeStubhub.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
require("dotenv").config();
const sendSMS = require("./sendSMS");
const puppeteer = require("puppeteer-extra");
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
puppeteer.use(StealthPlugin());
async function scrapeStubHub(url) {
console.log("scrapeStubHub()");
console.log("starting puppeteer");
const browser = await puppeteer.launch({
args: ["--ignore-certificate-errors"],
headless: true,
});
console.log("creating new page");
const page = await browser.newPage();
console.log("visiting page", url);
await page.goto(url, { timeout: 500000 });
console.log("page loaded");
page.on("console", (msg) => {
console.log("Browser console:", msg.text());
});
const ticket = await page.evaluate(() => {
const element = document.querySelector("[data-listing-id]");
if (!element) {
return null;
}
const listingId = element.getAttribute("data-listing-id");
const findPriceElement = (node) => {
if (
node.nodeType === Node.TEXT_NODE &&
node.textContent.trim().startsWith("$")
) {
return node.parentNode;
}
for (const child of node.childNodes) {
const result = findPriceElement(child);
if (result) {
return result;
}
}
return null;
};
const priceElement = findPriceElement(element);
if (priceElement) {
const priceText = priceElement.textContent.trim();
const price = parseFloat(priceText.replace("$", "").replace(",", ""));
return {
listingId,
price,
};
}
return null;
});
console.log("closing browser");
await browser.close();
console.log("returning ticket");
return ticket;
}
const scrapeAndAlert = async (url) => {
const ticket = await scrapeStubHub(url);
console.log("Stubhub Ticket:", ticket);
if (ticket && ticket.price < process.env.PRICE_ALERT_THRESHOLD) {
await sendSMS(ticket.price, url);
}
};
(async () => {
const url1 = `https://www.stubhub.com/taylor-swift-denver-tickets-7-14-2023/event/151214616/?quantity=${process.env.NUMBER_OF_SEATS}&estimatedFees=true&selectedEventsList=151214616,150593672`;
const scrapeInterval = async () => {
console.log("running scrape");
scrapeAndAlert(url1);
};
// Set the interval to run the scrapeStubHub function every X milliseconds
// (e.g., 300000 milliseconds = 5 minutes)
const scrapeFrequency = 60_000;
setInterval(scrapeInterval, scrapeFrequency);
})();