Skip to content

Instantly share code, notes, and snippets.

@rafaeldelboni
Last active October 26, 2017 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaeldelboni/51640f7e6949490ccb7700f25c0cfd36 to your computer and use it in GitHub Desktop.
Save rafaeldelboni/51640f7e6949490ccb7700f25c0cfd36 to your computer and use it in GitHub Desktop.
Query to check if my laptop is in stock
#!/usr/bin/env node
const requestPromise = require('request-promise-native')
const notifier = require('node-notifier')
const path = require('path')
async function searchStockLaptop ({name, url, search, shouldFind = true}) {
const pageBody = await requestPromise(url)
const isSearchFound = pageBody.indexOf(search) !== -1
if (isSearchFound === shouldFind) {
return {name, url, available: true}
} else {
return {name, available: false}
}
}
async function triggerNotification ({name, url}) {
return notifier.notify({
title: 'Its time to spend your money, bitch!',
message: `Store available: ${name}`,
icon: path.join(__dirname, 'corgi.jpg'),
sound: true,
wait: true,
open: url
})
}
const queryRequests = [
{
name: 'gentechpc',
url: 'https://www.gentechpc.com/ProductDetails.asp?ProductCode=AERO-15X-BK4-BUILD',
search: 'In Stock'
},
{
name: 'hidevolution',
url: 'http://www.hidevolution.com/gigabyte-aero-15x-bk4-1070.html',
search: 'In stock'
},
{
name: 'scan',
url: 'https://www.scan.co.uk/products/156-gigabyte-aero-15x-cf2-max-q-fhd-i7-7700hq-16gb-ddr4-512gb-m2-nvme-ssd-8gb-gtx-1070-thunderbolt3',
search: 'In stock'
},
{
name: 'notebook.de',
url: 'https://www.notebook.de/gigabyte-aero-15x-intel-core-i7-7700hq-280ghz-win10schwarz-p-119490',
search: 'Sofort lieferbar'
},
{
name: 'ldlc',
url: 'https://www.ldlc.com/fiche/PB00236256.html',
search: 'lir dispo d02'
},
{
name: 'xoticpc',
url: 'http://www.xoticpc.com/gigabyte-aero-15x-bk4.html',
search: 'BACKORDER',
shouldFind: false
}
]
let queries = queryRequests.map(request => searchStockLaptop(request))
Promise.all(queries)
.then(values => {
for (let value of values) {
if (value.available) {
triggerNotification(value)
.catch(err => console.error(err))
}
}
console.info({
date: new Date().toString(),
queries: values
})
})
.catch(err => console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment