-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (85 loc) · 2.54 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
import puppeteer from 'puppeteer-extra'
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
import RecaptchaPlugin from 'puppeteer-extra-plugin-recaptcha'
import dotenv from 'dotenv'
import twilio from 'twilio'
import cron from 'node-cron'
dotenv.config()
const twilioClient = twilio(process.env.TWILIO_SID, process.env.TWILIO_AUTH)
puppeteer.use(StealthPlugin)
puppeteer.use(RecaptchaPlugin({
provider: {
id: '2captcha',
token: process.env.TWOCAPTCHA
}
}))
const newPage = async (browser) => {
const page = await browser.newPage()
await page.setViewport({width: 1200, height: 800})
return page
}
const browser = await puppeteer.launch({headless: true})
const microsoftPage = await newPage(browser)
const targetPage = await newPage(browser)
const checkStock = async () => {
try {
await checkMicrosoft(microsoftPage)
await checkTarget(targetPage)
} catch (err) {
console.log('ERROR: ')
console.log(err)
}
}
//const checkWalmart = async (browser) => {
// const url = 'https://www.walmart.com/ip/XB1-Xbox-Series-X/443574645'
// await page.goto(url, {'waitUntil': 'networkidle2'})
// const { captchas } = await page.solveRecaptchas()
//
// if (captchas && captchas.length > 0) {
// await page.waitForNavigation()
// await page.waitForSelector('.prod-ProductTitle prod-productTitle-buyBox')
// }
//
// const result = await page.$('.prod-ProductOffer-oosMsg')
// if (!result) {
// notify(url)
// }
//}
const checkTarget = async (page) => {
const url = 'https://www.target.com/p/xbox-series-x-console/-/A-80790841'
await page.goto(url, {'waitUntil': 'networkidle2'})
const result = await page.$('[data-test=shipItButton]')
if (result) {
log(true, url)
await notify(url)
} else {
log(false, url)
}
}
const checkMicrosoft = async (page) => {
const url = 'https://www.xbox.com/en-US/consoles/xbox-series-x#purchase'
try {
await page.goto(url, {'waitUntil': 'networkidle2'})
const result = await page.waitForSelector('[data-bi-name="buy now"]', {timeout: 5000})
if (result) {
log(true, url)
await notify(url)
}
} catch (err) {
log (false, url)
}
}
const log = (inStock, url) => {
const date = new Date().toString()
const stockStr = inStock ? 'INSTOCK' : 'OOS'
console.log(`${date} ${stockStr} ${url}`)
}
const notify = (url) => {
return twilioClient.messages.create({
body: `INSTOCK: ${url}`,
from: process.env.TWILIO_FROM,
to: process.env.TWILIO_TO
})
}
const task = cron.schedule('*/2 * * * *', checkStock, {scheduled: false})
task.start()