Created
November 9, 2021 09:10
-
-
Save jgermade/b1da781f0d829bc1beeaf442f4fd198f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetch = require('node-fetch') | |
const fs = require('fs') | |
const qs = require('qs') | |
const { chromium } = require('playwright') | |
function randomIntFromInterval(min, max) { // min and max included | |
return Math.floor(Math.random() * (max - min + 1) + min) | |
} | |
const { | |
PUSHOVER_TOKEN, | |
PUSHOVER_USER, | |
} = process.env | |
async function notifyStock (title, message, url) { | |
return await fetch('https://api.pushover.net/1/messages.json', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
body: qs.stringify({ | |
token: PUSHOVER_TOKEN, | |
user: PUSHOVER_USER, | |
title, | |
message, | |
url, | |
sound: 'cashregister', | |
}) | |
}) | |
} | |
async function keepSearching () { | |
const browser = await chromium.launch({ | |
executablePath: '/snap/bin/chromium', | |
// executablePath: '/usr/bin/firefox', | |
}) | |
const page = await browser.newPage() | |
await page.goto('https://www.game.es/ps5-playstation5-reserva') | |
const missing_in_game = await page.waitForSelector('text=\'Temporalmente cerrado\'') | |
if (!missing_in_game) notifyStock( | |
'Reserva en GAME!!', | |
'No aparece \'Temporalmente cerrado\'', | |
'https://www.game.es/ps5-playstation5-reserva', | |
) | |
const eshops = [ | |
['GAME', 'Temporalmente cerrado', 'https://www.game.es/ps5-playstation5-reserva'], | |
['ECI', 'Agotado temporalmente', 'https://www.elcorteingles.es/videojuegos/A37046604/'], | |
['ECI-dualsense', 'Agotado temporalmente', 'https://www.elcorteingles.es/videojuegos/A39115651/'], | |
['FNAC', 'no disponible', 'https://www.fnac.es/Consola-PlayStation-5-Edicion-Digital-Videoconsola-Consola/a7724836'], | |
['Mediamarkt', 'no está disponible', 'https://www.mediamarkt.es/es/product/_consola-sony-ps5-825-gb-4k-hdr-blanco-1487016.html?tduid=98531e2bd4cfa3df3549974a845e5b4c&utm_source=tradedoubler&utm_medium=aff-content&utm_content=2337934&utm_campaign=affiliate_es_es_mm_conversion_online_mediamarkt_2021-01-01&utm_term=affiliate_es_es_mm_2021-01-01_conversion_tradedoubler_tradedoubler_aff-content_mediamarkt_all_multidevice_2337934_multisize_all_all_all'], | |
['Mediamarkt-spiderman-destruction-allstars', 'no está disponible', 'https://www.mediamarkt.es/es/product/_consola-sony-ps5-825-gb-4k-hdr-blanco-ps5-demon-s-souls-ps5-destruction-allstars-1507147.html?tduid=98531e2bd4cfa3df3549974a845e5b4c&utm_source=tradedoubler&utm_medium=aff-content&utm_content=2337934&utm_campaign=affiliate_es_es_mm_conversion_online_mediamarkt_2021-01-01&utm_term=affiliate_es_es_mm_2021-01-01_conversion_tradedoubler_tradedoubler_aff-content_mediamarkt_all_multidevice_2337934_multisize_all_all_all'], | |
['Worten-dualsense-spiderman', 'No disponible', 'https://www.worten.es/productos/consolas-juegos/playstation/consola/consola-ps5-825gb-7196053'], | |
['Carrefour-spiderman', 'Agotado temporalmente', 'https://www.carrefour.es/playstation-5-marvels-spider-man-miles-morales/VC4A-13751738/p?ic_source=nonfood&ic_medium=undefined&ic_content=cat9020002-consolas-y-videojuegos'], | |
['Carrefour-demonsouls', 'Agotado temporalmente', 'https://www.carrefour.es/playstation-5-demons-souls-remake/VC4A-13751737/p?ic_source=nonfood&ic_medium=undefined&ic_content=cat9020002-consolas-y-videojuegos'], | |
['TheShopGamer', 'Este artículo está agotado. ', 'https://www.ebay.es/itm/133580560834'], | |
['TheShopGamer-dualsense-destruction-allstars', 'Este artículo está agotado. ', 'https://www.ebay.es/itm/114786524897'], | |
['TheShopGamer-cod-black_ops', 'Este artículo está agotado. ', 'https://www.ebay.es/itm/124625637327'], | |
] | |
for (const eshop of eshops) { | |
let [name, missing_text, url, runCaptcha = async () => {}] = eshop | |
page && await page.goto(url, { | |
// waitUntil: 'networkidle', | |
}).then(async () => { | |
console.log('loaded:', name) | |
await runCaptcha(page) | |
let missing = await page.waitForSelector(`text='${missing_text}'`, { | |
timeout: 5000, | |
}).catch(() => false) | |
if (!missing) { | |
notifyStock( | |
`Reserva en ${name}!!`, | |
`No aparece '${missing_text}'`, | |
url, | |
) | |
await page.screenshot({ path: `screenshots/stock-${name}.png` }) | |
.catch(() => {}) | |
let html = await page.innerHTML('body') | |
await new Promise(resolve => { | |
fs.writeFile(`html/stock-${name}.html`, html, 'utf8', () => { | |
resolve() | |
}) | |
}) | |
} else { | |
console.log('missing in', url) | |
} | |
}) | |
.catch(() => {}) | |
} | |
// keep searching | |
const delay = randomIntFromInterval(30000, 60000) | |
console.log(`searching again in ${delay}ms...`) | |
setTimeout(() => { | |
console.log('searching again...') | |
keepSearching() | |
}, delay) | |
// await page.screenshot({ path: `screenshots/game-reserva.png` }) | |
await browser.close() | |
} | |
keepSearching() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment